Merge branch 'dev' of feistymeow.org:feisty_meow into dev
[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 export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )"
8
9 source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh"
10
11 # some convenient defaults for our current usage.
12
13 if [ -z "$BASE_APPLICATION_PATH" ]; then
14   BASE_APPLICATION_PATH="$HOME/apps"
15 fi
16 if [ -z "$STORAGE_SUFFIX" ]; then
17   STORAGE_SUFFIX="/public"
18 fi
19
20 # this function writes out the new configuration file for the site.
21 function write_apache_config()
22 {
23   local appname="$1"; shift
24   local sitename="$1"; shift
25   local site_path="$1"; shift
26
27   local site_config="/etc/apache2/sites-available/${sitename}.conf"
28
29   # check if config file already exists and bail if so.
30   if [ -f "$site_config" ]; then
31     echo "The apache configuration file already exists at:"
32     echo "  $site_config"
33     echo "Since apache configuration files can get very complex, we do not want to"
34     echo "assume that this file is removable.  Calling the site addition done."
35     exit 0
36   fi
37
38   echo "Creating a new apache2 site for $sitename with config file:"
39   echo "  $site_config"
40
41   # if no path, then we default to our standard app storage location.  otherwise, we
42   # put the site where they told us to.
43   if [ -z "$site_path" ]; then
44     # path where site gets checked out, in some arcane manner, and which happens to be
45     # above the path where we put webroot (in the storage suffix, if defined).
46     local path_above="${BASE_APPLICATION_PATH}/${appname}"
47     # no slash between appname and suffix, in case suffix is empty.
48     local full_path="${path_above}${STORAGE_SUFFIX}"
49 #echo really full path is $full_path
50   else
51     # we'll go with their specification for the site storage.
52     local full_path="$site_path"
53   fi
54
55   echo "
56 # set up the user's web folder as an apache user web directory.
57
58 # set permissions on the actual app folder.
59 <Directory \"$full_path\">
60   Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
61   Require all granted
62 </Directory>
63
64 <VirtualHost *:80>
65     ServerName ${sitename}
66     DocumentRoot ${full_path}
67     ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
68     CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
69     Include /etc/apache2/conf-library/basic-options.conf
70     Include /etc/apache2/conf-library/rewrite-enabling.conf
71 </VirtualHost>
72 " >"$site_config" 
73
74   chown "$(logname):$(logname)" "$site_config"
75   test_or_die "setting ownership on: $site_config"
76 }
77
78 # turns on the config file we create above for apache.
79 function enable_site()
80 {
81   local sitename="$1"; shift
82   local site_config="/etc/apache2/sites-available/${sitename}.conf"
83
84   outfile="$TMP/apacheout.$RANDOM"
85   a2ensite "$(basename $site_config)" &>$outfile
86   if [ $? -ne 0 ]; then
87     # an error happened, so we show the command's output at least.
88     cat $outfile
89     echo
90     echo "There was a problem enabling the apache config file in:"
91     echo "  $site_config"
92     echo "Please consult the apache error logs for more details."
93     exit 1
94   fi
95   \rm "$outfile"
96 }
97
98 # restarts the apache2 service.
99 function restart_apache()
100 {
101   service apache2 restart
102   if [ $? -ne 0 ]; then
103     echo "There was a problem restarting the apache2 service."
104     echo "Please consult the apache error logs for more details."
105     exit 1
106   fi
107 }
108
109 # sets up the serverpilot storage location for a user hosted web site.
110 function maybe_create_site_storage()
111 {
112   local our_app="$1"; shift
113   # make sure the path for storage this app exists for the user.
114   local full_path="$BASE_APPLICATION_PATH/$our_app"
115   if [ ! -d "$full_path" ]; then
116     mkdir -p $full_path
117     test_or_die "The app storage path could not be created.\n  Path in question is: $full_path"
118   fi
119
120   # now give the web server some access to the folder.  this is crucial since the folders
121   # can be hosted in any user folder, and the group permissions will not necessarily be correct already.
122   local chow_path="$full_path"
123   # only the first chmod is recursive; the rest just apply to the specific folder of interest.
124   chmod -R g+rx "$chow_path"
125   # walk backwards up the path and fix perms.
126   while [[ $chow_path != $HOME ]]; do
127 echo chow path is now $chow_path
128     chmod g+rx "$chow_path"
129     test_or_die "Failed to add group permissions on the path: $chow_path"
130     # reassert the user's ownership of any directories we might have just created.
131     chown $(logname) "$chow_path"
132     test_or_die "changing ownership to user failed on the path: $chow_path"
133     chow_path="$(dirname "$chow_path")"
134   done
135 }
136
137 # main body of script.
138
139 if [[ $EUID != 0 ]]; then
140   echo "This script must be run as root or sudo."
141   exit 1
142 fi
143
144 appname="$1"; shift
145 site="$1"; shift
146 site_path="$1"; shift
147
148 if [ -z "$appname" -o -z "$site" ]; then
149 #hmmm: move to a print_instructions function.
150   echo "
151 $(basename $0): {app name} {dns name} [site path]
152
153 This script needs to know (1) the application name for the new site and
154 (2) the DNS name for the apache virtual host.  The appname should be an
155 appropriate name for a file-system compatible folder name.  There is an
156 optional third parameter (3) the path for site storage.  If the site path
157 is not provided, we'll use this path:
158   $BASE_APPLICATION_PATH/{app name}/$STORAGE_SUFFIX"
159   exit 1
160 fi
161
162 maybe_create_site_storage "$appname"
163 write_apache_config "$appname" "$site" "$site_path"
164 enable_site "$site"
165 restart_apache
166