blob: 2c436816c3ac1b1a0e539080277900e223ae8a9c [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# - install_apache_wsgi
12# - config_apache_wsgi
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000013# - apache_site_config_for
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014# - enable_apache_site
15# - disable_apache_site
16# - start_apache_server
17# - stop_apache_server
18# - restart_apache_server
zhang-hared98a5d02013-06-21 18:18:02 +080019
20# Save trace setting
21XTRACE=$(set +o | grep xtrace)
22set +o xtrace
23
24# Allow overriding the default Apache user and group, default to
25# current user and his default group.
Stephan Renatuse578eff2013-11-19 13:31:04 +010026APACHE_USER=${APACHE_USER:-$STACK_USER}
zhang-hared98a5d02013-06-21 18:18:02 +080027APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
28
29
30# Set up apache name and configuration directory
31if is_ubuntu; then
32 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050033 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available}
zhang-hared98a5d02013-06-21 18:18:02 +080034elif is_fedora; then
35 APACHE_NAME=httpd
Dean Troyer444a8d52014-06-06 16:36:52 -050036 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d}
zhang-hared98a5d02013-06-21 18:18:02 +080037elif is_suse; then
38 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050039 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d}
zhang-hared98a5d02013-06-21 18:18:02 +080040fi
41
42# Functions
43# ---------
zhang-hared98a5d02013-06-21 18:18:02 +080044# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110045function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080046 # Apache installation, because we mark it NOPRIME
47 if is_ubuntu; then
48 # Install apache2, which is NOPRIME'd
49 install_package apache2 libapache2-mod-wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100050 # WSGI isn't enabled by default, enable it
51 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080052 elif is_fedora; then
53 sudo rm -f /etc/httpd/conf.d/000-*
54 install_package httpd mod_wsgi
55 elif is_suse; then
56 install_package apache2 apache2-mod_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100057 # WSGI isn't enabled by default, enable it
58 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080059 else
60 exit_distro_not_supported "apache installation"
61 fi
Noboru Iwamatsub4495eb2014-07-02 18:31:31 +090062
63 # ensure mod_version enabled for <IfVersion ...>. This is
64 # built-in statically on anything recent, but precise (2.2)
65 # doesn't have it enabled
66 sudo a2enmod version || true
zhang-hared98a5d02013-06-21 18:18:02 +080067}
68
Morgan Fainbergd074dc72014-06-24 21:33:39 -070069# get_apache_version() - return the version of Apache installed
70# This function is used to determine the Apache version installed. There are
71# various differences between Apache 2.2 and 2.4 that warrant special handling.
72function get_apache_version {
73 if is_ubuntu; then
74 local version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
75 elif is_fedora; then
76 local version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
77 elif is_suse; then
78 local version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
79 else
80 exit_distro_not_supported "cannot determine apache version"
81 fi
82 if [[ "$version_str" =~ ^2\.2\. ]]; then
83 echo "2.2"
84 elif [[ "$version_str" =~ ^2\.4\. ]]; then
85 echo "2.4"
86 else
87 exit_distro_not_supported "apache version not supported"
88 fi
89}
90
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000091# apache_site_config_for() - The filename of the site's configuration file.
92# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
93#
94# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
95# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
96# files are 000-default.conf and default-ssl.conf.
97#
98# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
99# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
100# sites' files are default and default-ssl.
101#
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200102# On Fedora and openSUSE, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled.
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000103#
104# On RHEL and CentOS, things should hopefully work as in Fedora.
105#
106# The table below summarizes what should happen on each distribution:
107# +----------------------+--------------------+--------------------------+--------------------------+
108# | Distribution | File name | Site enabling command | Site disabling command |
109# +----------------------+--------------------+--------------------------+--------------------------+
110# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
111# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
112# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
113# +----------------------+--------------------+--------------------------+--------------------------+
114function apache_site_config_for {
115 local site=$@
116 if is_ubuntu; then
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700117 local apache_version=$(get_apache_version)
118 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000119 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500120 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000121 else
122 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500123 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000124 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200125 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000126 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500127 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000128 if [ -f $enabled_site_file ]; then
129 echo ${enabled_site_file}
130 else
131 echo ${enabled_site_file}.disabled
132 fi
133 fi
134}
135
Jamie Lennox54707012013-09-17 12:07:48 +1000136# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100137function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000138 local site=$@
139 if is_ubuntu; then
140 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200141 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500142 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
143 # Do nothing if site already enabled or no site config exists
144 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
145 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
146 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000147 fi
148}
149
150# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100151function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000152 local site=$@
153 if is_ubuntu; then
154 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200155 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500156 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
157 # Do nothing if no site config exists
158 if [[ -f ${enabled_site_file} ]]; then
159 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
160 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000161 fi
162}
163
zhang-hared98a5d02013-06-21 18:18:02 +0800164# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100165function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800166 start_service $APACHE_NAME
167}
168
169# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100170function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800171 if [ -n "$APACHE_NAME" ]; then
172 stop_service $APACHE_NAME
173 else
174 exit_distro_not_supported "apache configuration"
175 fi
176}
177
178# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100179function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700180 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
181 # to mitigate issues where apache will claim a port it's listening on is
182 # still in use and fail to start.
183 stop_service $APACHE_NAME
184 sleep 3
185 start_service $APACHE_NAME
zhang-hared98a5d02013-06-21 18:18:02 +0800186}
187
188# Restore xtrace
189$XTRACE
190
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100191# Tell emacs to use shell-script-mode
192## Local variables:
193## mode: shell-script
194## End: