fixing this up for site avenger
[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 $USER:www-data "$full_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 != $BASE_PATH ]]; 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: $full_path"
117     chow_path="$(dirname "$chow_path")"
118   done
119 }
120
121 # main body of script.
122
123 if (( $EUID != 0 )); then
124   echo "This script must be run as root or sudo."
125   exit 1
126 fi
127
128 appname="$1"; shift
129 site="$1"; shift
130
131 if [ -z "$appname" -o -z "$site" ]; then
132   echo "This script needs to know (1) the appname (application name) for the new"
133   echo "site and (2) the DNS name for the apache virtual host."
134   echo "The appname should work as a file-system compatible folder name."
135   exit 1
136 fi
137
138 maybe_create_site_storage "$appname"
139 write_apache_config "$appname" "$site"
140 enable_site "$site"
141 restart_apache
142