improving on workingness
[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   echo "
41 # set up the user's web folder as an apache user web directory.
42 #UserDir apps
43 #above didn't help either.
44
45 #
46 #all of below might be borked.  trying different approach above.
47 # set permissions on the root folders.
48 ###<Directory \"/\">
49 ###  Options -ExecCGI +Indexes +FollowSymLinks +Includes
50 ###  Order allow,deny
51 ###  Allow from all
52 ###</Directory>
53 #### set permissions on the root of the home folders.
54 ###<Directory \"/home\">
55 ###  Options -ExecCGI +Indexes +FollowSymLinks +Includes
56 ###  Order allow,deny
57 ###  Allow from all
58 ###</Directory>
59 #### set permissions on the user's home folder.
60 ###<Directory \"$HOME\">
61 ###  Options -ExecCGI +Indexes +FollowSymLinks +Includes
62 ###  Order allow,deny
63 ###  Allow from all
64 ###</Directory>
65 #### set permissions on the user's storage folder for all apps.
66 ###<Directory \"$BASE_PATH\">
67 ###  Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
68 ###  Order allow,deny
69 ###  Allow from all
70 ###</Directory>
71 #### set permissions on the actual app folder.
72 ###<Directory \"$path_above\">
73 ###  Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
74 ###  Order allow,deny
75 ###  Allow from all
76 ###</Directory>
77
78 # set permissions on the actual app folder.
79 <Directory \"$full_path\">
80   Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
81 #  Order allow,deny
82 #  Allow from all
83   Require all granted
84 </Directory>
85
86 <VirtualHost *:80>
87     ServerName ${sitename}
88 #    ServerAlias ${sitename} *.${sitename}
89     DocumentRoot ${full_path}
90     ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
91     CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
92     Include /etc/apache2/conf-library/basic-options.conf
93     Include /etc/apache2/conf-library/rewrite-enabling.conf
94 </VirtualHost>
95 " >"$site_config" 
96 }
97
98 # turns on the config file we create above for apache.
99 function enable_site()
100 {
101   local sitename="$1"; shift
102   local site_config="/etc/apache2/sites-available/${sitename}.conf"
103
104   outfile="$TMP/apacheout.$RANDOM"
105   a2ensite "$(basename $site_config)" &>$outfile
106   if [ $? -ne 0 ]; then
107     # an error happened, so we show the command's output at least.
108     cat $outfile
109     echo
110     echo "There was a problem enabling the apache config file in:"
111     echo "  $site_config"
112     echo "Please consult the apache error logs for more details."
113     exit 1
114   fi
115   \rm "$outfile"
116 }
117
118 # restarts the apache2 service.
119 function restart_apache()
120 {
121   service apache2 restart
122   if [ $? -ne 0 ]; then
123     echo "There was a problem restarting the apache2 service."
124     echo "Please consult the apache error logs for more details."
125     exit 1
126   fi
127 }
128
129 # chown folder to group www-data.  can be done without setting a user, right?
130
131 # sets up the serverpilot storage location for a user hosted web site.
132 function maybe_create_site_storage()
133 {
134   local our_app="$1"; shift
135   # make sure the base path for storage of all the apps for this user exists.
136   local full_path="$BASE_PATH/$our_app"
137   if [ ! -d "$full_path" ]; then
138     mkdir -p $full_path
139     check_result "The app storage path could not be created.\n  Path in question is: $full_path"
140   fi
141
142   # now give the web server some access to the folder.  this is crucial since the folders
143   # can be hosted in any user folder, and the group permissions will usually be only for the user.
144   chown -R $(logname):www-data "$BASE_PATH"
145   check_result "Failed to set www-data as the owner on the path: $full_path"
146   # note that web serving will also hose up unless the path to the folder is writable.  so we walk backwards
147   # and make sure group access is available.
148   local chow_path="$full_path"
149   while [[ $chow_path != $HOME ]]; do
150 #echo chow path is now $chow_path
151     chmod -R g+rx "$chow_path"
152     check_result "Failed to add group permissions for www-data on the path: $chow_path"
153     # reassert the user's ownership of any directories we might have just created.
154     chown $(logname) "$chow_path"
155     check_result "changing ownership to user failed on the path: $chow_path"
156     chow_path="$(dirname "$chow_path")"
157   done
158 }
159
160 # main body of script.
161
162 if (( $EUID != 0 )); then
163   echo "This script must be run as root or sudo."
164   exit 1
165 fi
166
167 appname="$1"; shift
168 site="$1"; shift
169
170 if [ -z "$appname" -o -z "$site" ]; then
171   echo "This script needs to know (1) the appname (application name) for the new"
172   echo "site and (2) the DNS name for the apache virtual host."
173   echo "The appname should work as a file-system compatible folder name."
174   exit 1
175 fi
176
177 maybe_create_site_storage "$appname"
178 write_apache_config "$appname" "$site"
179 enable_site "$site"
180 restart_apache
181