3 # creates a new apache website for a specified domain.
5 # auto-find the scripts, since we might want to run this as sudo.
6 export WORKDIR="$( \cd "$(\dirname "$0")" && /bin/pwd )" # obtain the script's working directory.
7 echo WORKDIR is $WORKDIR
8 source "$WORKDIR/../core/launch_feisty_meow.sh"
10 # some convenient defaults for our current usage.
12 BASE_PATH="$HOME/apps"
13 STORAGE_SUFFIX="/public"
15 # this function writes out the new configuration file for the site.
16 function write_apache_config()
18 local appname="$1"; shift
19 local sitename="$1"; shift
20 local site_config="/etc/apache2/sites-available/${sitename}.conf"
22 # check if config file already exists and bail if so.
23 if [ -f "$site_config" ]; then
24 echo "The apache configuration file already exists at:"
26 echo "Please remove this file before proceeding, if it is junk. For example:"
27 echo " sudo rm $site_config"
31 echo "Creating a new apache2 site for $sitename with config file:"
34 local full_path="${BASE_PATH}/${appname}${STORAGE_SUFFIX}"
35 echo really full path is $full_path
37 #hmmm: the code below is just getting bigger. it would be nice to create the chunks of permission stuff
38 # via iteration rather than hardcoding.
41 # we have to enable some directory access through the user's folders.
42 # this is probably going to end up repeated in multiple apache files, but
43 # hopefully that's not a problem.
44 #hmmm: fix above note if it's not a problem.
46 # set permissions on the root folders.
48 Options -ExecCGI +Indexes +FollowSymLinks +Includes
52 # set permissions on the root of the home folders.
54 Options -ExecCGI +Indexes +FollowSymLinks +Includes
58 # set permissions on the user's home folder.
60 Options -ExecCGI +Indexes +FollowSymLinks +Includes
64 # set permissions on the user's app storage folder.
65 <Directory \"$BASE_PATH\">
66 Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews
70 # set permissions on the actual app folder.
71 <Directory \"$full_path\">
72 Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews
77 ServerName ${sitename}
78 # ServerAlias ${sitename} *.${sitename}
79 DocumentRoot ${full_path}
80 ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
81 CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
82 Include /etc/apache2/conf-library/basic-options.conf
83 Include /etc/apache2/conf-library/rewrite-enabling.conf
88 # turns on the config file we create above for apache.
89 function enable_site()
91 local sitename="$1"; shift
92 local site_config="/etc/apache2/sites-available/${sitename}.conf"
94 outfile="$TMP/apacheout.$RANDOM"
95 a2ensite "$(basename $site_config)" &>$outfile
97 # an error happened, so we show the command's output at least.
100 echo "There was a problem enabling the apache config file in:"
102 echo "Please consult the apache error logs for more details."
108 # restarts the apache2 service.
109 function restart_apache()
111 service apache2 restart
112 if [ $? -ne 0 ]; then
113 echo "There was a problem restarting the apache2 service."
114 echo "Please consult the apache error logs for more details."
119 # chown folder to group www-data. can be done without setting a user, right?
121 # sets up the serverpilot storage location for a user hosted web site.
122 function maybe_create_site_storage()
124 local our_app="$1"; shift
125 # make sure the base path for storage of all the apps for this user exists.
126 local full_path="$BASE_PATH/$our_app"
127 echo full path is $full_path
128 if [ ! -d "$full_path" ]; then
130 check_result "The app storage path could not be created.\n Path in question is: $full_path"
132 # now give the web server some access to the folder. this is crucial since the folders
133 # can be hosted in any user folder, and the group permissions will usually be only for the user.
134 chown -R $(logname):www-data "$BASE_PATH"
135 check_result "Failed to set www-data as the owner on the path: $full_path"
136 # note that web serving will also hose up unless the path to the folder is writable. so we walk backwards
137 # and make sure group access is available.
138 local chow_path="$full_path"
139 while [[ $chow_path != $HOME ]]; do
140 echo chow path is now $chow_path
141 chmod -R g+rx "$chow_path"
142 check_result "Failed to add group permissions for www-data on the path: $chow_path"
143 # reassert the user's ownership of any directories we might have just created.
144 chown $(logname) "$chow_path"
145 check_result "changing ownership to user failed on the path: $chow_path"
146 echo just chowned $chow_path for user $(logname)
147 chow_path="$(dirname "$chow_path")"
151 # main body of script.
153 if (( $EUID != 0 )); then
154 echo "This script must be run as root or sudo."
161 if [ -z "$appname" -o -z "$site" ]; then
162 echo "This script needs to know (1) the appname (application name) for the new"
163 echo "site and (2) the DNS name for the apache virtual host."
164 echo "The appname should work as a file-system compatible folder name."
168 maybe_create_site_storage "$appname"
169 write_apache_config "$appname" "$site"