blob: 55083e73e51a703caab793a28b6f2c047eaf1086 [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
34 APACHE_CONF_DIR=sites-available
35elif is_fedora; then
36 APACHE_NAME=httpd
37 APACHE_CONF_DIR=conf.d
38elif is_suse; then
39 APACHE_NAME=apache2
40 APACHE_CONF_DIR=vhosts.d
41fi
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
111 echo /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
112 else
113 # Ubuntu 14.04 - Apache 2.4
114 echo /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
115 fi
116 elif is_fedora; then
117 # fedora conf.d is only imported if it ends with .conf so this is approx the same
118 local enabled_site_file="/etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf"
119 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
133 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000134 sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf.disabled /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
Jamie Lennox54707012013-09-17 12:07:48 +1000135 fi
136}
137
138# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100139function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000140 local site=$@
141 if is_ubuntu; then
142 sudo a2dissite ${site}
143 elif is_fedora; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000144 sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf.disabled
Jamie Lennox54707012013-09-17 12:07:48 +1000145 fi
146}
147
zhang-hared98a5d02013-06-21 18:18:02 +0800148# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100149function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800150 start_service $APACHE_NAME
151}
152
153# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100154function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800155 if [ -n "$APACHE_NAME" ]; then
156 stop_service $APACHE_NAME
157 else
158 exit_distro_not_supported "apache configuration"
159 fi
160}
161
162# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100163function restart_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800164 restart_service $APACHE_NAME
165}
166
167# Restore xtrace
168$XTRACE
169
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100170# Tell emacs to use shell-script-mode
171## Local variables:
172## mode: shell-script
173## End: