blob: a8e9bc5ad2005a7de65f2f7263d5b6a19a2699a9 [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
22XTRACE=$(set +o | grep xtrace)
23set +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
75 local version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
76 elif is_fedora; then
77 local version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
78 elif is_suse; then
79 local version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
80 else
81 exit_distro_not_supported "cannot determine apache version"
82 fi
83 if [[ "$version_str" =~ ^2\.2\. ]]; then
84 echo "2.2"
85 elif [[ "$version_str" =~ ^2\.4\. ]]; then
86 echo "2.4"
87 else
88 exit_distro_not_supported "apache version not supported"
89 fi
90}
91
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000092# apache_site_config_for() - The filename of the site's configuration file.
93# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
94#
95# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
96# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
97# files are 000-default.conf and default-ssl.conf.
98#
99# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
100# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
101# sites' files are default and default-ssl.
102#
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200103# 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 +0000104#
105# On RHEL and CentOS, things should hopefully work as in Fedora.
106#
107# The table below summarizes what should happen on each distribution:
108# +----------------------+--------------------+--------------------------+--------------------------+
109# | Distribution | File name | Site enabling command | Site disabling command |
110# +----------------------+--------------------+--------------------------+--------------------------+
111# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
112# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
113# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
114# +----------------------+--------------------+--------------------------+--------------------------+
115function apache_site_config_for {
116 local site=$@
117 if is_ubuntu; then
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700118 local apache_version=$(get_apache_version)
119 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000120 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500121 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000122 else
123 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500124 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000125 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200126 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000127 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500128 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000129 if [ -f $enabled_site_file ]; then
130 echo ${enabled_site_file}
131 else
132 echo ${enabled_site_file}.disabled
133 fi
134 fi
135}
136
Jamie Lennox54707012013-09-17 12:07:48 +1000137# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100138function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000139 local site=$@
140 if is_ubuntu; then
141 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200142 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500143 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
144 # Do nothing if site already enabled or no site config exists
145 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
146 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
147 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000148 fi
149}
150
151# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000153 local site=$@
154 if is_ubuntu; then
155 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200156 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500157 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
158 # Do nothing if no site config exists
159 if [[ -f ${enabled_site_file} ]]; then
160 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
161 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000162 fi
163}
164
zhang-hared98a5d02013-06-21 18:18:02 +0800165# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100166function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800167 start_service $APACHE_NAME
168}
169
170# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100171function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800172 if [ -n "$APACHE_NAME" ]; then
173 stop_service $APACHE_NAME
174 else
175 exit_distro_not_supported "apache configuration"
176 fi
177}
178
179# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100180function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700181 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
182 # to mitigate issues where apache will claim a port it's listening on is
183 # still in use and fail to start.
184 stop_service $APACHE_NAME
185 sleep 3
186 start_service $APACHE_NAME
zhang-hared98a5d02013-06-21 18:18:02 +0800187}
188
189# Restore xtrace
190$XTRACE
191
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100192# Tell emacs to use shell-script-mode
193## Local variables:
194## mode: shell-script
195## End: