3 # creates a new apache website for a specified domain.
5 # some convenient defaults for our current usage.
8 SHADOWPATH="/srv/users/serverpilot/apps"
9 STORAGESUFFIX="/public"
11 # this function writes out the new configuration file for the site.
12 function write_apache_config()
14 local appname="$1"; shift
15 local sitename="$1"; shift
16 local site_config="/etc/apache2/sites-available/${sitename}.conf"
18 # check if config file already exists and bail if so.
19 if [ -f "$site_config" ]; then
20 echo "The apache configuration file already exists at:"
22 echo "Please remove this file before proceeding, if it is junk. For example:"
23 echo " sudo rm $site_config"
27 echo "Creating a new apache2 site for $sitename with config file:"
30 local fullpath="${BASEPATH}/${appname}${STORAGESUFFIX}"
32 # make the storage directory if it's not already present.
33 if [ ! -d "$fullpath" ]; then
36 echo "Failed to create the storage directory for $appname in"
37 echo "the folder: $fullpath"
44 ServerName ${sitename}
45 # ServerAlias ${sitename} *.${sitename}
46 DocumentRoot ${fullpath}
47 ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
48 CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
49 Include /etc/apache2/conf-library/basic-options.conf
50 Include /etc/apache2/conf-library/rewrite-enabling.conf
55 # turns on the config file we create above for apache.
56 function enable_site()
58 local sitename="$1"; shift
59 local site_config="/etc/apache2/sites-available/${sitename}.conf"
61 outfile="$TMP/apacheout.$RANDOM"
62 a2ensite "$(basename $site_config)" &>$outfile
64 # an error happened, so we show the command's output at least.
67 echo "There was a problem enabling the apache config file in:"
69 echo "Please consult the apache error logs for more details."
75 # restarts the apache2 service.
76 function restart_apache()
78 service apache2 restart
80 echo "There was a problem restarting the apache2 service."
81 echo "Please consult the apache error logs for more details."
86 # sets up a link to represent the serverpilot storage location, while
87 # still storing the files under /var/www.
88 function create_shadow_path()
90 # make sure there is a symbolic link from the shadow path (that mimics the serverpilot
91 # storage set up) to the real storage directory.
92 if [ ! -L "$SHADOWPATH" ]; then
93 # create the path up to but not including the last component.
94 if [ ! -d $(dirname $SHADOWPATH) ]; then
95 mkdir -p $(dirname $SHADOWPATH)
97 echo "The parent of the shadow path could not be created."
98 echo "Path in question is: $(dirname $SHADOWPATH)"
103 ln -s "$BASEPATH" "$SHADOWPATH"
104 #hmmm: should we be okay with it if it's a real dir, and assume people are happy?
105 # this wouldn't work too well if we go plunk down the new thing in /var/www,
106 # if they are expecting this tool to totally meld with serverpilot.
107 if [ $? -ne 0 ]; then
108 echo "The shadow path for mimicking serverpilot could not be created."
109 echo "Is there a real directory present for this already?"
110 echo "Path in question is: $SHADOWPATH"
116 # main body of script.
118 if (( $EUID != 0 )); then
119 echo "This script must be run as root or sudo."
126 if [ -z "$appname" -o -z "$site" ]; then
127 echo "This script needs to know (1) the appname (application name) for the new"
128 echo "site and (2) the DNS name for the apache virtual host."
129 echo "The appname should work as a file-system compatible folder name."
134 write_apache_config "$appname" "$site"