more cleanups on apache site adder
[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 echo WORKDIR is $WORKDIR
8 source "$WORKDIR/../core/launch_feisty_meow.sh"
9
10 # some convenient defaults for our current usage.
11
12 BASE_PATH="$HOME/apps"
13 STORAGE_SUFFIX="/public"
14
15 # this function writes out the new configuration file for the site.
16 function write_apache_config()
17 {
18   local appname="$1"; shift
19   local sitename="$1"; shift
20   local site_config="/etc/apache2/sites-available/${sitename}.conf"
21
22   # check if config file already exists and bail if so.
23   if [ -f "$site_config" ]; then
24     echo "The apache configuration file already exists at:"
25     echo "  $site_config"
26     echo "Please remove this file before proceeding, if it is junk.  For example:"
27     echo "  sudo rm $site_config"
28     exit 1
29   fi
30
31   echo "Creating a new apache2 site for $sitename with config file:"
32   echo "  $site_config"
33
34   local full_path="${BASE_PATH}/${appname}${STORAGE_SUFFIX}"
35 echo really full path is $full_path
36
37 #no, bad!  the public folder will be a link.
38 # will apache be happy if the site folder doesn't exist yet?
39 #  # make the storage directory if it's not already present.
40 #  if [ ! -d "$full_path" ]; then
41 #    mkdir -p "$full_path"
42 #    if [ $? -ne 0 ]; then
43 #      echo "Failed to create the storage directory for $appname in"
44 #      echo "the folder: $full_path"
45 #      exit 1
46 #    fi
47 #  fi
48
49 echo "
50 <VirtualHost *:80>
51     ServerName ${sitename}
52 #    ServerAlias ${sitename} *.${sitename}
53     DocumentRoot ${full_path}
54     ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
55     CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
56     Include /etc/apache2/conf-library/basic-options.conf
57     Include /etc/apache2/conf-library/rewrite-enabling.conf
58 </VirtualHost>
59 " >"$site_config" 
60 }
61
62 # turns on the config file we create above for apache.
63 function enable_site()
64 {
65   local sitename="$1"; shift
66   local site_config="/etc/apache2/sites-available/${sitename}.conf"
67
68   outfile="$TMP/apacheout.$RANDOM"
69   a2ensite "$(basename $site_config)" &>$outfile
70   if [ $? -ne 0 ]; then
71     # an error happened, so we show the command's output at least.
72     cat $outfile
73     echo
74     echo "There was a problem enabling the apache config file in:"
75     echo "  $site_config"
76     echo "Please consult the apache error logs for more details."
77     exit 1
78   fi
79   rm "$outfile"
80 }
81
82 # restarts the apache2 service.
83 function restart_apache()
84 {
85   service apache2 restart
86   if [ $? -ne 0 ]; then
87     echo "There was a problem restarting the apache2 service."
88     echo "Please consult the apache error logs for more details."
89     exit 1
90   fi
91 }
92
93 # chown folder to group www-data.  can be done without setting a user, right?
94
95 # sets up the serverpilot storage location for a user hosted web site.
96 function maybe_create_site_storage()
97 {
98   local our_app="$1"; shift
99   # make sure the base path for storage of all the apps for this user exists.
100   local full_path="$BASE_PATH/$our_app"
101 echo full path is $full_path
102   if [ ! -d "$full_path" ]; then
103     mkdir -p $full_path
104     check_result "The app storage path could not be created.\n  Path in question is: $full_path"
105   fi
106   # now give the web server some access to the folder.  this is crucial since the folders
107   # can be hosted in any user folder, and the group permissions will usually be only for the user.
108   chown -R $(logname):www-data "$BASE_PATH"
109   check_result "Failed to set www-data as the owner on the path: $full_path"
110   # note that web serving will also hose up unless the path to the folder is writable.  so we walk backwards
111   # and make sure group access is available.
112   local chow_path="$full_path"
113   while [[ $chow_path != $HOME ]]; do
114 echo chow path is now $chow_path
115     chmod -R g+rx "$chow_path"
116     check_result "Failed to add group permissions for www-data on the path: $chow_path"
117     # reassert the user's ownership of any directories we might have just created.
118     chown $(logname) "$chow_path"
119     check_result "changing ownership to user failed on the path: $chow_path"
120 echo just chowned $chow_path for user $(logname)
121     chow_path="$(dirname "$chow_path")"
122   done
123 }
124
125 # main body of script.
126
127 if (( $EUID != 0 )); then
128   echo "This script must be run as root or sudo."
129   exit 1
130 fi
131
132 appname="$1"; shift
133 site="$1"; shift
134
135 if [ -z "$appname" -o -z "$site" ]; then
136   echo "This script needs to know (1) the appname (application name) for the new"
137   echo "site and (2) the DNS name for the apache virtual host."
138   echo "The appname should work as a file-system compatible folder name."
139   exit 1
140 fi
141
142 maybe_create_site_storage "$appname"
143 write_apache_config "$appname" "$site"
144 enable_site "$site"
145 restart_apache
146