blob: c9e02a2b585de886aacf2334256c80024baeed0a [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# ---------
zhang-hared98a5d02013-06-21 18:18:02 +080045# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110046function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080047 # Apache installation, because we mark it NOPRIME
48 if is_ubuntu; then
49 # Install apache2, which is NOPRIME'd
50 install_package apache2 libapache2-mod-wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100051 # WSGI isn't enabled by default, enable it
52 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080053 elif is_fedora; then
54 sudo rm -f /etc/httpd/conf.d/000-*
55 install_package httpd mod_wsgi
56 elif is_suse; then
57 install_package apache2 apache2-mod_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100058 # WSGI isn't enabled by default, enable it
59 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080060 else
61 exit_distro_not_supported "apache installation"
62 fi
Noboru Iwamatsub4495eb2014-07-02 18:31:31 +090063
64 # ensure mod_version enabled for <IfVersion ...>. This is
65 # built-in statically on anything recent, but precise (2.2)
66 # doesn't have it enabled
67 sudo a2enmod version || true
zhang-hared98a5d02013-06-21 18:18:02 +080068}
69
Morgan Fainbergd074dc72014-06-24 21:33:39 -070070# get_apache_version() - return the version of Apache installed
71# This function is used to determine the Apache version installed. There are
72# various differences between Apache 2.2 and 2.4 that warrant special handling.
73function get_apache_version {
74 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +110075 local version_str
76 version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070077 elif is_fedora; then
Ian Wienandada886d2015-10-07 14:06:26 +110078 local version_str
79 version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070080 elif is_suse; then
Ian Wienandada886d2015-10-07 14:06:26 +110081 local version_str
82 version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
Morgan Fainbergd074dc72014-06-24 21:33:39 -070083 else
84 exit_distro_not_supported "cannot determine apache version"
85 fi
86 if [[ "$version_str" =~ ^2\.2\. ]]; then
87 echo "2.2"
88 elif [[ "$version_str" =~ ^2\.4\. ]]; then
89 echo "2.4"
90 else
91 exit_distro_not_supported "apache version not supported"
92 fi
93}
94
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000095# apache_site_config_for() - The filename of the site's configuration file.
96# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
97#
98# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
99# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
100# files are 000-default.conf and default-ssl.conf.
101#
102# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
103# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
104# sites' files are default and default-ssl.
105#
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200106# 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 +0000107#
108# On RHEL and CentOS, things should hopefully work as in Fedora.
109#
110# The table below summarizes what should happen on each distribution:
111# +----------------------+--------------------+--------------------------+--------------------------+
112# | Distribution | File name | Site enabling command | Site disabling command |
113# +----------------------+--------------------+--------------------------+--------------------------+
114# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
115# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
116# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
117# +----------------------+--------------------+--------------------------+--------------------------+
118function apache_site_config_for {
119 local site=$@
120 if is_ubuntu; then
Ian Wienandada886d2015-10-07 14:06:26 +1100121 local apache_version
122 apache_version=$(get_apache_version)
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700123 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000124 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500125 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000126 else
127 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500128 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000129 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200130 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000131 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500132 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000133 if [ -f $enabled_site_file ]; then
134 echo ${enabled_site_file}
135 else
136 echo ${enabled_site_file}.disabled
137 fi
138 fi
139}
140
Jamie Lennox54707012013-09-17 12:07:48 +1000141# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100142function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000143 local site=$@
144 if is_ubuntu; then
145 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200146 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500147 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
148 # Do nothing if site already enabled or no site config exists
149 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
150 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
151 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000152 fi
153}
154
155# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100156function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000157 local site=$@
158 if is_ubuntu; then
159 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200160 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500161 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
162 # Do nothing if no site config exists
163 if [[ -f ${enabled_site_file} ]]; then
164 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
165 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000166 fi
167}
168
zhang-hared98a5d02013-06-21 18:18:02 +0800169# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100170function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800171 start_service $APACHE_NAME
172}
173
174# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100175function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800176 if [ -n "$APACHE_NAME" ]; then
177 stop_service $APACHE_NAME
178 else
179 exit_distro_not_supported "apache configuration"
180 fi
181}
182
183# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100184function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700185 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
186 # to mitigate issues where apache will claim a port it's listening on is
187 # still in use and fail to start.
188 stop_service $APACHE_NAME
189 sleep 3
190 start_service $APACHE_NAME
zhang-hared98a5d02013-06-21 18:18:02 +0800191}
192
193# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100194$_XTRACE_LIB_APACHE
zhang-hared98a5d02013-06-21 18:18:02 +0800195
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100196# Tell emacs to use shell-script-mode
197## Local variables:
198## mode: shell-script
199## End: