blob: 8a38cc45e5a25321e9073fa88828bc607098b20b [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
Clark Boylan66ce5c22016-10-05 12:11:05 -070042APACHE_LOG_DIR="/var/log/${APACHE_NAME}"
zhang-hared98a5d02013-06-21 18:18:02 +080043
44# Functions
45# ---------
Gregory Haynes4b49e402016-08-31 18:19:51 -070046
47# Enable apache mod and restart apache if it isn't already enabled.
48function enable_apache_mod {
49 local mod=$1
50 # Apache installation, because we mark it NOPRIME
51 if is_ubuntu || is_suse ; then
52 if ! a2query -m $mod ; then
53 sudo a2enmod $mod
54 restart_apache_server
55 fi
56 elif is_fedora; then
57 # pass
58 true
59 else
60 exit_distro_not_supported "apache enable mod"
61 fi
62}
63
zhang-hared98a5d02013-06-21 18:18:02 +080064# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110065function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080066 # Apache installation, because we mark it NOPRIME
67 if is_ubuntu; then
68 # Install apache2, which is NOPRIME'd
69 install_package apache2 libapache2-mod-wsgi
70 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
75 else
Gregory Haynes4b49e402016-08-31 18:19:51 -070076 exit_distro_not_supported "apache wsgi installation"
zhang-hared98a5d02013-06-21 18:18:02 +080077 fi
Gregory Haynes4b49e402016-08-31 18:19:51 -070078 # WSGI isn't enabled by default, enable it
79 enable_apache_mod wsgi
Noboru Iwamatsub4495eb2014-07-02 18:31:31 +090080
81 # ensure mod_version enabled for <IfVersion ...>. This is
82 # built-in statically on anything recent, but precise (2.2)
83 # doesn't have it enabled
84 sudo a2enmod version || true
zhang-hared98a5d02013-06-21 18:18:02 +080085}
86
Morgan Fainbergd074dc72014-06-24 21:33:39 -070087# get_apache_version() - return the version of Apache installed
88# This function is used to determine the Apache version installed. There are
89# various differences between Apache 2.2 and 2.4 that warrant special handling.
90function get_apache_version {
91 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +110092 local version_str
93 version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070094 elif is_fedora; then
Ian Wienandada886d2015-10-07 14:06:26 +110095 local version_str
96 version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070097 elif is_suse; then
Ian Wienandada886d2015-10-07 14:06:26 +110098 local version_str
99 version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700100 else
101 exit_distro_not_supported "cannot determine apache version"
102 fi
103 if [[ "$version_str" =~ ^2\.2\. ]]; then
104 echo "2.2"
105 elif [[ "$version_str" =~ ^2\.4\. ]]; then
106 echo "2.4"
107 else
108 exit_distro_not_supported "apache version not supported"
109 fi
110}
111
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000112# apache_site_config_for() - The filename of the site's configuration file.
113# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
114#
115# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
116# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
117# files are 000-default.conf and default-ssl.conf.
118#
119# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
120# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
121# sites' files are default and default-ssl.
122#
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200123# 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 +0000124#
125# On RHEL and CentOS, things should hopefully work as in Fedora.
126#
127# The table below summarizes what should happen on each distribution:
128# +----------------------+--------------------+--------------------------+--------------------------+
129# | Distribution | File name | Site enabling command | Site disabling command |
130# +----------------------+--------------------+--------------------------+--------------------------+
131# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
132# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
133# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
134# +----------------------+--------------------+--------------------------+--------------------------+
135function apache_site_config_for {
136 local site=$@
137 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +1100138 local apache_version
139 apache_version=$(get_apache_version)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700140 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000141 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500142 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000143 else
144 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500145 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000146 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200147 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000148 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500149 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000150 if [ -f $enabled_site_file ]; then
151 echo ${enabled_site_file}
152 else
153 echo ${enabled_site_file}.disabled
154 fi
155 fi
156}
157
Jamie Lennox54707012013-09-17 12:07:48 +1000158# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100159function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000160 local site=$@
161 if is_ubuntu; then
162 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200163 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500164 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
165 # Do nothing if site already enabled or no site config exists
166 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
167 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
168 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000169 fi
170}
171
172# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100173function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000174 local site=$@
175 if is_ubuntu; then
176 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200177 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500178 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
179 # Do nothing if no site config exists
180 if [[ -f ${enabled_site_file} ]]; then
181 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
182 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000183 fi
184}
185
zhang-hared98a5d02013-06-21 18:18:02 +0800186# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100187function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800188 start_service $APACHE_NAME
189}
190
191# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100192function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800193 if [ -n "$APACHE_NAME" ]; then
194 stop_service $APACHE_NAME
195 else
196 exit_distro_not_supported "apache configuration"
197 fi
198}
199
200# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100201function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700202 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
203 # to mitigate issues where apache will claim a port it's listening on is
204 # still in use and fail to start.
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900205 time_start "restart_apache_server"
Morgan Fainberg2df00462014-07-15 11:06:36 -0700206 stop_service $APACHE_NAME
207 sleep 3
208 start_service $APACHE_NAME
Atsushi SAKAI2ca8af42015-12-08 15:36:13 +0900209 time_stop "restart_apache_server"
zhang-hared98a5d02013-06-21 18:18:02 +0800210}
211
Gregory Haynes4b49e402016-08-31 18:19:51 -0700212# reload_apache_server
213function reload_apache_server {
214 reload_service $APACHE_NAME
215}
216
zhang-hared98a5d02013-06-21 18:18:02 +0800217# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100218$_XTRACE_LIB_APACHE
zhang-hared98a5d02013-06-21 18:18:02 +0800219
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100220# Tell emacs to use shell-script-mode
221## Local variables:
222## mode: shell-script
223## End: