blob: d1a11ae18b32a4a4cb5df11fd0328f8ef23c4dbb [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
Clark Boylancfb9f052016-11-29 10:43:05 -080032# Note that APACHE_CONF_DIR is really more accurately apache's vhost
33# configuration dir but we can't just change this because public interfaces.
zhang-hared98a5d02013-06-21 18:18:02 +080034if is_ubuntu; then
35 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050036 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available}
Clark Boylancfb9f052016-11-29 10:43:05 -080037 APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf-enabled}
zhang-hared98a5d02013-06-21 18:18:02 +080038elif is_fedora; then
39 APACHE_NAME=httpd
Dean Troyer444a8d52014-06-06 16:36:52 -050040 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d}
Clark Boylancfb9f052016-11-29 10:43:05 -080041 APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf.d}
zhang-hared98a5d02013-06-21 18:18:02 +080042elif is_suse; then
43 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050044 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d}
Clark Boylancfb9f052016-11-29 10:43:05 -080045 APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf.d}
zhang-hared98a5d02013-06-21 18:18:02 +080046fi
Clark Boylan66ce5c22016-10-05 12:11:05 -070047APACHE_LOG_DIR="/var/log/${APACHE_NAME}"
zhang-hared98a5d02013-06-21 18:18:02 +080048
49# Functions
50# ---------
Gregory Haynes4b49e402016-08-31 18:19:51 -070051
52# Enable apache mod and restart apache if it isn't already enabled.
53function enable_apache_mod {
54 local mod=$1
55 # Apache installation, because we mark it NOPRIME
56 if is_ubuntu || is_suse ; then
57 if ! a2query -m $mod ; then
58 sudo a2enmod $mod
59 restart_apache_server
60 fi
61 elif is_fedora; then
62 # pass
63 true
64 else
65 exit_distro_not_supported "apache enable mod"
66 fi
67}
68
zhang-hared98a5d02013-06-21 18:18:02 +080069# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110070function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080071 # Apache installation, because we mark it NOPRIME
72 if is_ubuntu; then
73 # Install apache2, which is NOPRIME'd
Davanum Srinivasafa8a002016-12-19 09:51:01 -050074 install_package apache2
75 if python3_enabled; then
76 if is_package_installed libapache2-mod-wsgi; then
77 uninstall_package libapache2-mod-wsgi
78 fi
79 install_package libapache2-mod-wsgi-py3
80 else
81 install_package libapache2-mod-wsgi
82 fi
zhang-hared98a5d02013-06-21 18:18:02 +080083 elif is_fedora; then
84 sudo rm -f /etc/httpd/conf.d/000-*
85 install_package httpd mod_wsgi
86 elif is_suse; then
87 install_package apache2 apache2-mod_wsgi
88 else
Gregory Haynes4b49e402016-08-31 18:19:51 -070089 exit_distro_not_supported "apache wsgi installation"
zhang-hared98a5d02013-06-21 18:18:02 +080090 fi
Gregory Haynes4b49e402016-08-31 18:19:51 -070091 # WSGI isn't enabled by default, enable it
92 enable_apache_mod wsgi
Noboru Iwamatsub4495eb2014-07-02 18:31:31 +090093
94 # ensure mod_version enabled for <IfVersion ...>. This is
95 # built-in statically on anything recent, but precise (2.2)
96 # doesn't have it enabled
97 sudo a2enmod version || true
zhang-hared98a5d02013-06-21 18:18:02 +080098}
99
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700100# get_apache_version() - return the version of Apache installed
101# This function is used to determine the Apache version installed. There are
102# various differences between Apache 2.2 and 2.4 that warrant special handling.
103function get_apache_version {
104 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +1100105 local version_str
106 version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700107 elif is_fedora; then
Ian Wienandada886d2015-10-07 14:06:26 +1100108 local version_str
109 version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700110 elif is_suse; then
Ian Wienandada886d2015-10-07 14:06:26 +1100111 local version_str
112 version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700113 else
114 exit_distro_not_supported "cannot determine apache version"
115 fi
116 if [[ "$version_str" =~ ^2\.2\. ]]; then
117 echo "2.2"
118 elif [[ "$version_str" =~ ^2\.4\. ]]; then
119 echo "2.4"
120 else
121 exit_distro_not_supported "apache version not supported"
122 fi
123}
124
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000125# apache_site_config_for() - The filename of the site's configuration file.
126# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
127#
128# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
129# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
130# files are 000-default.conf and default-ssl.conf.
131#
132# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
133# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
134# sites' files are default and default-ssl.
135#
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200136# 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 +0000137#
138# On RHEL and CentOS, things should hopefully work as in Fedora.
139#
140# The table below summarizes what should happen on each distribution:
141# +----------------------+--------------------+--------------------------+--------------------------+
142# | Distribution | File name | Site enabling command | Site disabling command |
143# +----------------------+--------------------+--------------------------+--------------------------+
144# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
145# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
146# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
147# +----------------------+--------------------+--------------------------+--------------------------+
148function apache_site_config_for {
149 local site=$@
150 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +1100151 local apache_version
152 apache_version=$(get_apache_version)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700153 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000154 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500155 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000156 else
157 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500158 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000159 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200160 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000161 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500162 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000163 if [ -f $enabled_site_file ]; then
164 echo ${enabled_site_file}
165 else
166 echo ${enabled_site_file}.disabled
167 fi
168 fi
169}
170
Jamie Lennox54707012013-09-17 12:07:48 +1000171# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100172function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000173 local site=$@
174 if is_ubuntu; then
175 sudo a2ensite ${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 site already enabled or no site config exists
179 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
180 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
181 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000182 fi
183}
184
185# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100186function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000187 local site=$@
188 if is_ubuntu; then
189 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200190 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500191 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
192 # Do nothing if no site config exists
193 if [[ -f ${enabled_site_file} ]]; then
194 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
195 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000196 fi
197}
198
zhang-hared98a5d02013-06-21 18:18:02 +0800199# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100200function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800201 start_service $APACHE_NAME
202}
203
204# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100205function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800206 if [ -n "$APACHE_NAME" ]; then
207 stop_service $APACHE_NAME
208 else
209 exit_distro_not_supported "apache configuration"
210 fi
211}
212
213# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100214function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700215 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
216 # to mitigate issues where apache will claim a port it's listening on is
217 # still in use and fail to start.
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900218 time_start "restart_apache_server"
Morgan Fainberg2df00462014-07-15 11:06:36 -0700219 stop_service $APACHE_NAME
220 sleep 3
221 start_service $APACHE_NAME
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900222 time_stop "restart_apache_server"
zhang-hared98a5d02013-06-21 18:18:02 +0800223}
224
Gregory Haynes4b49e402016-08-31 18:19:51 -0700225# reload_apache_server
226function reload_apache_server {
227 reload_service $APACHE_NAME
228}
229
zhang-hared98a5d02013-06-21 18:18:02 +0800230# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100231$_XTRACE_LIB_APACHE
zhang-hared98a5d02013-06-21 18:18:02 +0800232
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100233# Tell emacs to use shell-script-mode
234## Local variables:
235## mode: shell-script
236## End: