345b96cdfd7de22426ca25984f790e96f4803ab6
[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 source "$WORKDIR/../core/launch_feisty_meow.sh"
8
9 # some convenient defaults for our current usage.
10
11 BASE_PATH="$HOME/apps"
12 STORAGE_SUFFIX="/public"
13
14 # this function writes out the new configuration file for the site.
15 function write_apache_config()
16 {
17   local appname="$1"; shift
18   local sitename="$1"; shift
19   local site_config="/etc/apache2/sites-available/${sitename}.conf"
20
21   # check if config file already exists and bail if so.
22   if [ -f "$site_config" ]; then
23     echo "The apache configuration file already exists at:"
24     echo "  $site_config"
25     echo "Please remove this file before proceeding, if it is junk.  For example:"
26     echo "  sudo rm $site_config"
27     exit 1
28   fi
29
30   echo "Creating a new apache2 site for $sitename with config file:"
31   echo "  $site_config"
32
33   # path where site gets checked out, in some arcane manner, and which happens to be
34   # above the path where we put webroot (in the storage suffix, if defined).
35   local path_above="${BASE_PATH}/${appname}"
36   # no slash between appname and suffix, in case suffix is empty.
37   local full_path="${BASE_PATH}/${appname}${STORAGE_SUFFIX}"
38 #echo really full path is $full_path
39
40 #hmmm: the code below is just getting bigger.  it would be nice to create the chunks of permission stuff
41 # via iteration rather than hardcoding.
42
43   echo "
44 # we have to enable some directory access through the user's folders.
45 # this is probably going to end up repeated in multiple apache files, but
46 # hopefully that's not a problem.
47 #hmmm: fix above note if it's not a problem.
48
49
50 # set up the user's web folder as an apache user web directory.
51 UserDir apps
52 #above didn't help either.
53
54 #
55 #all of below might be borked.  trying different approach above.
56 # set permissions on the root folders.
57 ###<Directory \"/\">
58 ###  Options -ExecCGI +Indexes +FollowSymLinks +Includes
59 ###  Order allow,deny
60 ###  Allow from all
61 ###</Directory>
62 #### set permissions on the root of the home folders.
63 ###<Directory \"/home\">
64 ###  Options -ExecCGI +Indexes +FollowSymLinks +Includes
65 ###  Order allow,deny
66 ###  Allow from all
67 ###</Directory>
68 #### set permissions on the user's home folder.
69 ###<Directory \"$HOME\">
70 ###  Options -ExecCGI +Indexes +FollowSymLinks +Includes
71 ###  Order allow,deny
72 ###  Allow from all
73 ###</Directory>
74 #### set permissions on the user's storage folder for all apps.
75 ###<Directory \"$BASE_PATH\">
76 ###  Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
77 ###  Order allow,deny
78 ###  Allow from all
79 ###</Directory>
80 #### set permissions on the actual app folder.
81 ###<Directory \"$path_above\">
82 ###  Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
83 ###  Order allow,deny
84 ###  Allow from all
85 ###</Directory>
86
87 # set permissions on the actual app folder.
88 <Directory \"$full_path\">
89   Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
90 #  Order allow,deny
91 #  Allow from all
92   Require all granted
93 </Directory>
94
95 <VirtualHost *:80>
96     ServerName ${sitename}
97 #    ServerAlias ${sitename} *.${sitename}
98     DocumentRoot ${full_path}
99     ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
100     CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
101     Include /etc/apache2/conf-library/basic-options.conf
102     Include /etc/apache2/conf-library/rewrite-enabling.conf
103 </VirtualHost>
104 " >"$site_config" 
105 }
106
107 # turns on the config file we create above for apache.
108 function enable_site()
109 {
110   local sitename="$1"; shift
111   local site_config="/etc/apache2/sites-available/${sitename}.conf"
112
113   outfile="$TMP/apacheout.$RANDOM"
114   a2ensite "$(basename $site_config)" &>$outfile
115   if [ $? -ne 0 ]; then
116     # an error happened, so we show the command's output at least.
117     cat $outfile
118     echo
119     echo "There was a problem enabling the apache config file in:"
120     echo "  $site_config"
121     echo "Please consult the apache error logs for more details."
122     exit 1
123   fi
124   \rm "$outfile"
125 }
126
127 # restarts the apache2 service.
128 function restart_apache()
129 {
130   service apache2 restart
131   if [ $? -ne 0 ]; then
132     echo "There was a problem restarting the apache2 service."
133     echo "Please consult the apache error logs for more details."
134     exit 1
135   fi
136 }
137
138 # chown folder to group www-data.  can be done without setting a user, right?
139
140 # sets up the serverpilot storage location for a user hosted web site.
141 function maybe_create_site_storage()
142 {
143   local our_app="$1"; shift
144   # make sure the base path for storage of all the apps for this user exists.
145   local full_path="$BASE_PATH/$our_app"
146   if [ ! -d "$full_path" ]; then
147     mkdir -p $full_path
148     check_result "The app storage path could not be created.\n  Path in question is: $full_path"
149   fi
150
151   # now give the web server some access to the folder.  this is crucial since the folders
152   # can be hosted in any user folder, and the group permissions will usually be only for the user.
153   chown -R $(logname):www-data "$BASE_PATH"
154   check_result "Failed to set www-data as the owner on the path: $full_path"
155   # note that web serving will also hose up unless the path to the folder is writable.  so we walk backwards
156   # and make sure group access is available.
157   local chow_path="$full_path"
158   while [[ $chow_path != $HOME ]]; do
159 #echo chow path is now $chow_path
160     chmod -R g+rx "$chow_path"
161     check_result "Failed to add group permissions for www-data on the path: $chow_path"
162     # reassert the user's ownership of any directories we might have just created.
163     chown $(logname) "$chow_path"
164     check_result "changing ownership to user failed on the path: $chow_path"
165     chow_path="$(dirname "$chow_path")"
166   done
167 }
168
169 # main body of script.
170
171 if (( $EUID != 0 )); then
172   echo "This script must be run as root or sudo."
173   exit 1
174 fi
175
176 appname="$1"; shift
177 site="$1"; shift
178
179 if [ -z "$appname" -o -z "$site" ]; then
180   echo "This script needs to know (1) the appname (application name) for the new"
181   echo "site and (2) the DNS name for the apache virtual host."
182   echo "The appname should work as a file-system compatible folder name."
183   exit 1
184 fi
185
186 maybe_create_site_storage "$appname"
187 write_apache_config "$appname" "$site"
188 enable_site "$site"
189 restart_apache
190