blob: 740f58835ef7d812aa6f0d32650897a7d5567433 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
zhang-hared98a5d02013-06-21 18:18:02 +08003# lib/apache
4# Functions to control configuration and operation of apache web server
5
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - ``functions`` file
Dean Troyerd8864fe2014-02-17 11:00:42 -06009# - ``STACK_USER`` must be defined
10#
Stephan Renatuse578eff2013-11-19 13:31:04 +010011# lib/apache exports the following functions:
12#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010013# - install_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
Ian Wienand523f4882015-10-13 11:03:03 +110022_XTRACE_LIB_APACHE=$(set +o | grep xtrace)
zhang-hared98a5d02013-06-21 18:18:02 +080023set +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# ---------
Gregory Haynes4b49e402016-08-31 18:19:51 -070045
46# Enable apache mod and restart apache if it isn't already enabled.
47function enable_apache_mod {
48 local mod=$1
49 # Apache installation, because we mark it NOPRIME
50 if is_ubuntu || is_suse ; then
51 if ! a2query -m $mod ; then
52 sudo a2enmod $mod
53 restart_apache_server
54 fi
55 elif is_fedora; then
56 # pass
57 true
58 else
59 exit_distro_not_supported "apache enable mod"
60 fi
61}
62
zhang-hared98a5d02013-06-21 18:18:02 +080063# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110064function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080065 # Apache installation, because we mark it NOPRIME
66 if is_ubuntu; then
67 # Install apache2, which is NOPRIME'd
68 install_package apache2 libapache2-mod-wsgi
69 elif is_fedora; then
70 sudo rm -f /etc/httpd/conf.d/000-*
71 install_package httpd mod_wsgi
72 elif is_suse; then
73 install_package apache2 apache2-mod_wsgi
74 else
Gregory Haynes4b49e402016-08-31 18:19:51 -070075 exit_distro_not_supported "apache wsgi installation"
zhang-hared98a5d02013-06-21 18:18:02 +080076 fi
Gregory Haynes4b49e402016-08-31 18:19:51 -070077 # WSGI isn't enabled by default, enable it
78 enable_apache_mod wsgi
Noboru Iwamatsub4495eb2014-07-02 18:31:31 +090079
80 # ensure mod_version enabled for <IfVersion ...>. This is
81 # built-in statically on anything recent, but precise (2.2)
82 # doesn't have it enabled
83 sudo a2enmod version || true
zhang-hared98a5d02013-06-21 18:18:02 +080084}
85
Morgan Fainbergd074dc72014-06-24 21:33:39 -070086# get_apache_version() - return the version of Apache installed
87# This function is used to determine the Apache version installed. There are
88# various differences between Apache 2.2 and 2.4 that warrant special handling.
89function get_apache_version {
90 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +110091 local version_str
92 version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070093 elif is_fedora; then
Ian Wienandada886d2015-10-07 14:06:26 +110094 local version_str
95 version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070096 elif is_suse; then
Ian Wienandada886d2015-10-07 14:06:26 +110097 local version_str
98 version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070099 else
100 exit_distro_not_supported "cannot determine apache version"
101 fi
102 if [[ "$version_str" =~ ^2\.2\. ]]; then
103 echo "2.2"
104 elif [[ "$version_str" =~ ^2\.4\. ]]; then
105 echo "2.4"
106 else
107 exit_distro_not_supported "apache version not supported"
108 fi
109}
110
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000111# apache_site_config_for() - The filename of the site's configuration file.
112# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
113#
114# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
115# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
116# files are 000-default.conf and default-ssl.conf.
117#
118# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
119# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
120# sites' files are default and default-ssl.
121#
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200122# 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 +0000123#
124# On RHEL and CentOS, things should hopefully work as in Fedora.
125#
126# The table below summarizes what should happen on each distribution:
127# +----------------------+--------------------+--------------------------+--------------------------+
128# | Distribution | File name | Site enabling command | Site disabling command |
129# +----------------------+--------------------+--------------------------+--------------------------+
130# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
131# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
132# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
133# +----------------------+--------------------+--------------------------+--------------------------+
134function apache_site_config_for {
135 local site=$@
136 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +1100137 local apache_version
138 apache_version=$(get_apache_version)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700139 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000140 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500141 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000142 else
143 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500144 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000145 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200146 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000147 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500148 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000149 if [ -f $enabled_site_file ]; then
150 echo ${enabled_site_file}
151 else
152 echo ${enabled_site_file}.disabled
153 fi
154 fi
155}
156
Jamie Lennox54707012013-09-17 12:07:48 +1000157# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100158function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000159 local site=$@
160 if is_ubuntu; then
161 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200162 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500163 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
164 # Do nothing if site already enabled or no site config exists
165 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
166 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
167 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000168 fi
169}
170
171# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100172function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000173 local site=$@
174 if is_ubuntu; then
175 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200176 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500177 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
178 # Do nothing if no site config exists
179 if [[ -f ${enabled_site_file} ]]; then
180 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
181 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000182 fi
183}
184
zhang-hared98a5d02013-06-21 18:18:02 +0800185# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100186function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800187 start_service $APACHE_NAME
188}
189
190# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100191function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800192 if [ -n "$APACHE_NAME" ]; then
193 stop_service $APACHE_NAME
194 else
195 exit_distro_not_supported "apache configuration"
196 fi
197}
198
199# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100200function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700201 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
202 # to mitigate issues where apache will claim a port it's listening on is
203 # still in use and fail to start.
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900204 time_start "restart_apache_server"
Morgan Fainberg2df00462014-07-15 11:06:36 -0700205 stop_service $APACHE_NAME
206 sleep 3
207 start_service $APACHE_NAME
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900208 time_stop "restart_apache_server"
zhang-hared98a5d02013-06-21 18:18:02 +0800209}
210
Gregory Haynes4b49e402016-08-31 18:19:51 -0700211# reload_apache_server
212function reload_apache_server {
213 reload_service $APACHE_NAME
214}
215
zhang-hared98a5d02013-06-21 18:18:02 +0800216# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100217$_XTRACE_LIB_APACHE
zhang-hared98a5d02013-06-21 18:18:02 +0800218
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100219# Tell emacs to use shell-script-mode
220## Local variables:
221## mode: shell-script
222## End: