909604bbe118b65adc2e590da8c5a1933e52b29e
[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
43 # set permissions on the actual app folder.
44 <Directory \"$full_path\">
45   Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews 
46   Require all granted
47 </Directory>
48
49 <VirtualHost *:80>
50     ServerName ${sitename}
51     DocumentRoot ${full_path}
52     ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
53     CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
54     Include /etc/apache2/conf-library/basic-options.conf
55     Include /etc/apache2/conf-library/rewrite-enabling.conf
56 </VirtualHost>
57 " >"$site_config" 
58 }
59
60 # turns on the config file we create above for apache.
61 function enable_site()
62 {
63   local sitename="$1"; shift
64   local site_config="/etc/apache2/sites-available/${sitename}.conf"
65
66   outfile="$TMP/apacheout.$RANDOM"
67   a2ensite "$(basename $site_config)" &>$outfile
68   if [ $? -ne 0 ]; then
69     # an error happened, so we show the command's output at least.
70     cat $outfile
71     echo
72     echo "There was a problem enabling the apache config file in:"
73     echo "  $site_config"
74     echo "Please consult the apache error logs for more details."
75     exit 1
76   fi
77   \rm "$outfile"
78 }
79
80 # restarts the apache2 service.
81 function restart_apache()
82 {
83   service apache2 restart
84   if [ $? -ne 0 ]; then
85     echo "There was a problem restarting the apache2 service."
86     echo "Please consult the apache error logs for more details."
87     exit 1
88   fi
89 }
90
91 # sets up the serverpilot storage location for a user hosted web site.
92 function maybe_create_site_storage()
93 {
94   local our_app="$1"; shift
95   # make sure the base path for storage of all the apps for this user exists.
96   local full_path="$BASE_PATH/$our_app"
97   if [ ! -d "$full_path" ]; then
98     mkdir -p $full_path
99     check_result "The app storage path could not be created.\n  Path in question is: $full_path"
100   fi
101
102   # now give the web server some access to the folder.  this is crucial since the folders
103   # can be hosted in any user folder, and the group permissions will not necessarily be correct already.
104   local chow_path="$full_path"
105   # only the first chmod is recursive; the rest just apply to the specific folder of interest.
106   chmod -R g+rx "$chow_path"
107   # walk backwards up the path and fix perms.
108   while [[ $chow_path != $HOME ]]; do
109 echo chow path is now $chow_path
110     chmod g+rx "$chow_path"
111     check_result "Failed to add group permissions on the path: $chow_path"
112     # reassert the user's ownership of any directories we might have just created.
113     chown $(logname) "$chow_path"
114     check_result "changing ownership to user failed on the path: $chow_path"
115     chow_path="$(dirname "$chow_path")"
116   done
117 }
118
119 # main body of script.
120
121 if (( $EUID != 0 )); then
122   echo "This script must be run as root or sudo."
123   exit 1
124 fi
125
126 appname="$1"; shift
127 site="$1"; shift
128
129 if [ -z "$appname" -o -z "$site" ]; then
130   echo "This script needs to know (1) the appname (application name) for the new"
131   echo "site and (2) the DNS name for the apache virtual host."
132   echo "The appname should work as a file-system compatible folder name."
133   exit 1
134 fi
135
136 maybe_create_site_storage "$appname"
137 write_apache_config "$appname" "$site"
138 enable_site "$site"
139 restart_apache
140