blob: baf0fbc5f5d68357fe6ce63df4d771f52117a6b5 [file] [log] [blame]
zhang-hared98a5d02013-06-21 18:18:02 +08001# lib/apache
2# Functions to control configuration and operation of apache web server
3
4# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - ``functions`` file
Dean Troyerd8864fe2014-02-17 11:00:42 -06007# - ``STACK_USER`` must be defined
8#
Stephan Renatuse578eff2013-11-19 13:31:04 +01009# lib/apache exports the following functions:
10#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010011# - is_apache_enabled_service
12# - install_apache_wsgi
13# - config_apache_wsgi
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000014# - apache_site_config_for
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010015# - enable_apache_site
16# - disable_apache_site
17# - start_apache_server
18# - stop_apache_server
19# - restart_apache_server
zhang-hared98a5d02013-06-21 18:18:02 +080020
21# Save trace setting
22XTRACE=$(set +o | grep xtrace)
23set +o xtrace
24
25# Allow overriding the default Apache user and group, default to
26# current user and his default group.
Stephan Renatuse578eff2013-11-19 13:31:04 +010027APACHE_USER=${APACHE_USER:-$STACK_USER}
zhang-hared98a5d02013-06-21 18:18:02 +080028APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
29
30
31# Set up apache name and configuration directory
32if is_ubuntu; then
33 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050034 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available}
zhang-hared98a5d02013-06-21 18:18:02 +080035elif is_fedora; then
36 APACHE_NAME=httpd
Dean Troyer444a8d52014-06-06 16:36:52 -050037 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d}
zhang-hared98a5d02013-06-21 18:18:02 +080038elif is_suse; then
39 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050040 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d}
zhang-hared98a5d02013-06-21 18:18:02 +080041fi
42
43# Functions
44# ---------
45
46# is_apache_enabled_service() checks if the service(s) specified as arguments are
47# apache enabled by the user in ``APACHE_ENABLED_SERVICES`` as web front end.
48#
49# Multiple services specified as arguments are ``OR``'ed together; the test
50# is a short-circuit boolean, i.e it returns on the first match.
51#
52# Uses global ``APACHE_ENABLED_SERVICES``
53# APACHE_ENABLED_SERVICES service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +110054function is_apache_enabled_service {
zhang-hared98a5d02013-06-21 18:18:02 +080055 services=$@
56 for service in ${services}; do
57 [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
58 done
59 return 1
60}
61
zhang-hared98a5d02013-06-21 18:18:02 +080062# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110063function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080064 # Apache installation, because we mark it NOPRIME
65 if is_ubuntu; then
66 # Install apache2, which is NOPRIME'd
67 install_package apache2 libapache2-mod-wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100068 # WSGI isn't enabled by default, enable it
69 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080070 elif is_fedora; then
71 sudo rm -f /etc/httpd/conf.d/000-*
72 install_package httpd mod_wsgi
73 elif is_suse; then
74 install_package apache2 apache2-mod_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100075 # WSGI isn't enabled by default, enable it
76 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080077 else
78 exit_distro_not_supported "apache installation"
79 fi
80}
81
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000082# apache_site_config_for() - The filename of the site's configuration file.
83# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
84#
85# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
86# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
87# files are 000-default.conf and default-ssl.conf.
88#
89# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
90# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
91# sites' files are default and default-ssl.
92#
93# On Fedora, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled.
94#
95# On RHEL and CentOS, things should hopefully work as in Fedora.
96#
97# The table below summarizes what should happen on each distribution:
98# +----------------------+--------------------+--------------------------+--------------------------+
99# | Distribution | File name | Site enabling command | Site disabling command |
100# +----------------------+--------------------+--------------------------+--------------------------+
101# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
102# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
103# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
104# +----------------------+--------------------+--------------------------+--------------------------+
105function apache_site_config_for {
106 local site=$@
107 if is_ubuntu; then
108 local apache_version=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
109 if [[ "$apache_version" =~ ^2\.2\. ]]; then
110 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500111 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000112 else
113 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500114 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000115 fi
116 elif is_fedora; then
117 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500118 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000119 if [ -f $enabled_site_file ]; then
120 echo ${enabled_site_file}
121 else
122 echo ${enabled_site_file}.disabled
123 fi
124 fi
125}
126
Jamie Lennox54707012013-09-17 12:07:48 +1000127# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100128function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000129 local site=$@
130 if is_ubuntu; then
131 sudo a2ensite ${site}
132 elif is_fedora; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500133 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
134 # Do nothing if site already enabled or no site config exists
135 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
136 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
137 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000138 fi
139}
140
141# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100142function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000143 local site=$@
144 if is_ubuntu; then
145 sudo a2dissite ${site}
146 elif is_fedora; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500147 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
148 # Do nothing if no site config exists
149 if [[ -f ${enabled_site_file} ]]; then
150 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
151 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000152 fi
153}
154
zhang-hared98a5d02013-06-21 18:18:02 +0800155# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100156function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800157 start_service $APACHE_NAME
158}
159
160# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100161function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800162 if [ -n "$APACHE_NAME" ]; then
163 stop_service $APACHE_NAME
164 else
165 exit_distro_not_supported "apache configuration"
166 fi
167}
168
169# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100170function restart_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800171 restart_service $APACHE_NAME
172}
173
174# Restore xtrace
175$XTRACE
176
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100177# Tell emacs to use shell-script-mode
178## Local variables:
179## mode: shell-script
180## End: