3 # creates a new apache website for a specified domain.
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"
10 # some convenient defaults for our current usage.
12 BASE_PATH="$HOME/apps"
13 STORAGE_SUFFIX="/public"
15 # this function writes out the new configuration file for the site.
16 function write_apache_config()
18 local appname="$1"; shift
19 local sitename="$1"; shift
20 local site_config="/etc/apache2/sites-available/${sitename}.conf"
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:"
26 echo "Please remove this file before proceeding, if it is junk. For example:"
27 echo " sudo rm $site_config"
31 echo "Creating a new apache2 site for $sitename with config file:"
34 local full_path="${BASE_PATH}/${appname}${STORAGE_SUFFIX}"
35 echo really full path is $full_path
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"
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
62 # turns on the config file we create above for apache.
63 function enable_site()
65 local sitename="$1"; shift
66 local site_config="/etc/apache2/sites-available/${sitename}.conf"
68 outfile="$TMP/apacheout.$RANDOM"
69 a2ensite "$(basename $site_config)" &>$outfile
71 # an error happened, so we show the command's output at least.
74 echo "There was a problem enabling the apache config file in:"
76 echo "Please consult the apache error logs for more details."
82 # restarts the apache2 service.
83 function restart_apache()
85 service apache2 restart
87 echo "There was a problem restarting the apache2 service."
88 echo "Please consult the apache error logs for more details."
93 # chown folder to group www-data. can be done without setting a user, right?
95 # sets up the serverpilot storage location for a user hosted web site.
96 function maybe_create_site_storage()
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
104 check_result "The app storage path could not be created.\n Path in question is: $full_path"
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")"
121 # main body of script.
123 if (( $EUID != 0 )); then
124 echo "This script must be run as root or sudo."
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."
138 maybe_create_site_storage "$appname"
139 write_apache_config "$appname" "$site"