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