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