add domain fixes, prototype for 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 # 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 "
53 #fix
54 # >"$site_config"
55
56 }
57
58 function enable_site()
59 {
60   local sitename="$1"; shift
61   local site_config="/etc/apache2/sites-available/${sitename}.conf"
62
63   a2ensite "$(basename $site_config)"
64   if [ $? -ne 0 ]; then
65     echo "There was a problem enabling the apache config file in:"
66     echo "  $site_config"
67     echo "Please consult the apache error logs for more details."
68     exit 1
69   fi
70 }
71
72 function restart_apache()
73 {
74   service apache2 restart
75   if [ $? -ne 0 ]; then
76     echo "There was a problem restarting the apache2 service."
77     echo "Please consult the apache error logs for more details."
78     exit 1
79   fi
80 }
81
82 # main body of script.
83
84 if (( $EUID != 0 )); then
85   echo "This script must be run as root or sudo."
86   exit 1
87 fi
88
89 appname="$1"; shift
90 site="$1"; shift
91
92 if [ -z "$appname" -o -z "$site" ]; then
93   echo "This script needs to know (1) the appname (application name) for the new"
94   echo "site and (2) the DNS name for the apache virtual host."
95   echo "The appname should work as a file-system compatible folder name."
96   exit 1
97 fi
98
99 # make sure there is a symbolic link from the shadow path (that mimics the serverpilot
100 # storage set up) to the real storage directory.
101 if [ ! -l "$SHADOWPATH" ]; then
102   ln -s "$BASEPATH" "$SHADOWPATH"
103 #hmmm: should we be okay with it if it's a real dir, and assume people are happy?
104 #      this wouldn't work too well if we go plunk down the new thing in /var/www,
105 #      if they are expecting this tool to totally meld with serverpilot.
106   if [ $? -ne 0 ]; then
107     echo "The shadow path for mimicking serverpilot could not be created."
108     echo "Is there a real directory present for this already?"
109     echo "Path in question is: $SHADOWPATH"
110     exit 1
111   fi
112 fi
113
114 write_apache_config "$appname" "$site"
115 enable_site "$site"
116 restart_apache
117