argh, closer but no cookie
[feisty_meow.git] / scripts / system / add_apache_site.sh
1 #!/bin/bash
2
3 # creates a new apache website for a specified domain.
4
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"
9
10 # some convenient defaults for our current usage.
11
12 BASE_PATH="$HOME/apps"
13 STORAGE_SUFFIX="/public"
14
15 # this function writes out the new configuration file for the site.
16 function write_apache_config()
17 {
18   local appname="$1"; shift
19   local sitename="$1"; shift
20   local site_config="/etc/apache2/sites-available/${sitename}.conf"
21
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:"
25     echo "  $site_config"
26     echo "Please remove this file before proceeding, if it is junk.  For example:"
27     echo "  sudo rm $site_config"
28     exit 1
29   fi
30
31   echo "Creating a new apache2 site for $sitename with config file:"
32   echo "  $site_config"
33
34   local full_path="${BASE_PATH}/${appname}${STORAGE_SUFFIX}"
35 echo really full path is $full_path
36
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.
39
40 echo "
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.
45 #
46 # set permissions on the root folders.
47 <Directory \"/\">
48   Options -ExecCGI +Indexes +FollowSymLinks +Includes
49   Order allow,deny
50   Allow from all
51 </Directory>
52 # set permissions on the root of the home folders.
53 <Directory \"/home\">
54   Options -ExecCGI +Indexes +FollowSymLinks +Includes
55   Order allow,deny
56   Allow from all
57 </Directory>
58 # set permissions on the user's home folder.
59 <Directory \"$HOME\">
60   Options -ExecCGI +Indexes +FollowSymLinks +Includes
61   Order allow,deny
62   Allow from all
63 </Directory>
64 # set permissions on the user's app storage folder.
65 <Directory \"$BASE_PATH\">
66   Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
67   Order allow,deny
68   Allow from all
69 </Directory>
70 # set permissions on the actual app folder.
71 <Directory \"$full_path\">
72   Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
73   Order allow,deny
74   Allow from all
75 </Directory>
76 <VirtualHost *:80>
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
84 </VirtualHost>
85 " >"$site_config" 
86 }
87
88 # turns on the config file we create above for apache.
89 function enable_site()
90 {
91   local sitename="$1"; shift
92   local site_config="/etc/apache2/sites-available/${sitename}.conf"
93
94   outfile="$TMP/apacheout.$RANDOM"
95   a2ensite "$(basename $site_config)" &>$outfile
96   if [ $? -ne 0 ]; then
97     # an error happened, so we show the command's output at least.
98     cat $outfile
99     echo
100     echo "There was a problem enabling the apache config file in:"
101     echo "  $site_config"
102     echo "Please consult the apache error logs for more details."
103     exit 1
104   fi
105   rm "$outfile"
106 }
107
108 # restarts the apache2 service.
109 function restart_apache()
110 {
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."
115     exit 1
116   fi
117 }
118
119 # chown folder to group www-data.  can be done without setting a user, right?
120
121 # sets up the serverpilot storage location for a user hosted web site.
122 function maybe_create_site_storage()
123 {
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
129     mkdir -p $full_path
130     check_result "The app storage path could not be created.\n  Path in question is: $full_path"
131   fi
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")"
148   done
149 }
150
151 # main body of script.
152
153 if (( $EUID != 0 )); then
154   echo "This script must be run as root or sudo."
155   exit 1
156 fi
157
158 appname="$1"; shift
159 site="$1"; shift
160
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."
165   exit 1
166 fi
167
168 maybe_create_site_storage "$appname"
169 write_apache_config "$appname" "$site"
170 enable_site "$site"
171 restart_apache
172