apache site adder fixes
[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 # some convenient defaults for our current usage.
6
7 BASEPATH="/var/www"
8 SHADOWPATH="/srv/users/serverpilot/apps"
9 STORAGESUFFIX="/public"
10
11 # this function writes out the new configuration file for the site.
12 function write_apache_config()
13 {
14   local appname="$1"; shift
15   local sitename="$1"; shift
16   local site_config="/etc/apache2/sites-available/${sitename}.conf"
17
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:"
21     echo "  $site_config"
22     echo "Please remove this file before proceeding, if it is junk.  For example:"
23     echo "  sudo rm $site_config"
24     exit 1
25   fi
26
27   echo "Creating a new apache2 site for $sitename with config file:"
28   echo "  $site_config"
29
30   local fullpath="${BASEPATH}/${appname}${STORAGESUFFIX}"
31
32   # make the storage directory if it's not already present.
33   if [ ! -d "$fullpath" ]; then
34     mkdir -p "$fullpath"
35     if [ $? -ne 0 ]; then
36       echo "Failed to create the storage directory for $appname in"
37       echo "the folder: $fullpath"
38       exit 1
39     fi
40   fi
41
42 echo "
43 <VirtualHost *:80>
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
51 </VirtualHost>
52 " >"$site_config" 
53 }
54
55 # turns on the config file we create above for apache.
56 function enable_site()
57 {
58   local sitename="$1"; shift
59   local site_config="/etc/apache2/sites-available/${sitename}.conf"
60
61   outfile="$TMP/apacheout.$RANDOM"
62   a2ensite "$(basename $site_config)" &>$outfile
63   if [ $? -ne 0 ]; then
64     # an error happened, so we show the command's output at least.
65     cat $outfile
66     echo
67     echo "There was a problem enabling the apache config file in:"
68     echo "  $site_config"
69     echo "Please consult the apache error logs for more details."
70     exit 1
71   fi
72   rm "$outfile"
73 }
74
75 # restarts the apache2 service.
76 function restart_apache()
77 {
78   service apache2 restart
79   if [ $? -ne 0 ]; then
80     echo "There was a problem restarting the apache2 service."
81     echo "Please consult the apache error logs for more details."
82     exit 1
83   fi
84 }
85
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()
89 {
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)
96       if [ $? -ne 0 ]; then
97         echo "The parent of the shadow path could not be created."
98         echo "Path in question is: $(dirname $SHADOWPATH)"
99         exit 1
100       fi
101     fi
102
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"
111       exit 1
112     fi
113   fi
114 }
115
116 # main body of script.
117
118 if (( $EUID != 0 )); then
119   echo "This script must be run as root or sudo."
120   exit 1
121 fi
122
123 appname="$1"; shift
124 site="$1"; shift
125
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."
130   exit 1
131 fi
132
133 create_shadow_path
134 write_apache_config "$appname" "$site"
135 enable_site "$site"
136 restart_apache
137