blob: 6d22290c42128e29f22ef0f7c7b0bff720d64dcb [file] [log] [blame]
zhang-hared98a5d02013-06-21 18:18:02 +08001# lib/apache
2# Functions to control configuration and operation of apache web server
3
4# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - ``functions`` file
Dean Troyerd8864fe2014-02-17 11:00:42 -06007# - ``STACK_USER`` must be defined
8#
Stephan Renatuse578eff2013-11-19 13:31:04 +01009# lib/apache exports the following functions:
10#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010011# - install_apache_wsgi
12# - config_apache_wsgi
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000013# - apache_site_config_for
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014# - enable_apache_site
15# - disable_apache_site
16# - start_apache_server
17# - stop_apache_server
18# - restart_apache_server
zhang-hared98a5d02013-06-21 18:18:02 +080019
20# Save trace setting
21XTRACE=$(set +o | grep xtrace)
22set +o xtrace
23
24# Allow overriding the default Apache user and group, default to
25# current user and his default group.
Stephan Renatuse578eff2013-11-19 13:31:04 +010026APACHE_USER=${APACHE_USER:-$STACK_USER}
zhang-hared98a5d02013-06-21 18:18:02 +080027APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
28
29
30# Set up apache name and configuration directory
31if is_ubuntu; then
32 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050033 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available}
zhang-hared98a5d02013-06-21 18:18:02 +080034elif is_fedora; then
35 APACHE_NAME=httpd
Dean Troyer444a8d52014-06-06 16:36:52 -050036 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d}
zhang-hared98a5d02013-06-21 18:18:02 +080037elif is_suse; then
38 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050039 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d}
zhang-hared98a5d02013-06-21 18:18:02 +080040fi
41
42# Functions
43# ---------
zhang-hared98a5d02013-06-21 18:18:02 +080044# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110045function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080046 # Apache installation, because we mark it NOPRIME
47 if is_ubuntu; then
48 # Install apache2, which is NOPRIME'd
49 install_package apache2 libapache2-mod-wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100050 # WSGI isn't enabled by default, enable it
51 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080052 elif is_fedora; then
53 sudo rm -f /etc/httpd/conf.d/000-*
54 install_package httpd mod_wsgi
55 elif is_suse; then
56 install_package apache2 apache2-mod_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100057 # WSGI isn't enabled by default, enable it
58 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080059 else
60 exit_distro_not_supported "apache installation"
61 fi
62}
63
Morgan Fainbergd074dc72014-06-24 21:33:39 -070064# get_apache_version() - return the version of Apache installed
65# This function is used to determine the Apache version installed. There are
66# various differences between Apache 2.2 and 2.4 that warrant special handling.
67function get_apache_version {
68 if is_ubuntu; then
69 local version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
70 elif is_fedora; then
71 local version_str=$(rpm -qa --queryformat '%{VERSION}' httpd)
72 elif is_suse; then
73 local version_str=$(rpm -qa --queryformat '%{VERSION}' apache2)
74 else
75 exit_distro_not_supported "cannot determine apache version"
76 fi
77 if [[ "$version_str" =~ ^2\.2\. ]]; then
78 echo "2.2"
79 elif [[ "$version_str" =~ ^2\.4\. ]]; then
80 echo "2.4"
81 else
82 exit_distro_not_supported "apache version not supported"
83 fi
84}
85
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000086# apache_site_config_for() - The filename of the site's configuration file.
87# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
88#
89# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
90# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
91# files are 000-default.conf and default-ssl.conf.
92#
93# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
94# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
95# sites' files are default and default-ssl.
96#
Ralf Haferkamp633a1292014-06-16 14:10:05 +020097# 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 +000098#
99# On RHEL and CentOS, things should hopefully work as in Fedora.
100#
101# The table below summarizes what should happen on each distribution:
102# +----------------------+--------------------+--------------------------+--------------------------+
103# | Distribution | File name | Site enabling command | Site disabling command |
104# +----------------------+--------------------+--------------------------+--------------------------+
105# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
106# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
107# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
108# +----------------------+--------------------+--------------------------+--------------------------+
109function apache_site_config_for {
110 local site=$@
111 if is_ubuntu; then
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700112 local apache_version=$(get_apache_version)
113 if [[ "$apache_version" == "2.2" ]]; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000114 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -0500115 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000116 else
117 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -0500118 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000119 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200120 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000121 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500122 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000123 if [ -f $enabled_site_file ]; then
124 echo ${enabled_site_file}
125 else
126 echo ${enabled_site_file}.disabled
127 fi
128 fi
129}
130
Jamie Lennox54707012013-09-17 12:07:48 +1000131# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100132function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000133 local site=$@
134 if is_ubuntu; then
135 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200136 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500137 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
138 # Do nothing if site already enabled or no site config exists
139 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
140 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
141 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000142 fi
143}
144
145# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100146function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000147 local site=$@
148 if is_ubuntu; then
149 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200150 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500151 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
152 # Do nothing if no site config exists
153 if [[ -f ${enabled_site_file} ]]; then
154 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
155 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000156 fi
157}
158
zhang-hared98a5d02013-06-21 18:18:02 +0800159# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100160function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800161 start_service $APACHE_NAME
162}
163
164# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100165function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800166 if [ -n "$APACHE_NAME" ]; then
167 stop_service $APACHE_NAME
168 else
169 exit_distro_not_supported "apache configuration"
170 fi
171}
172
173# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100174function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700175 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
176 # to mitigate issues where apache will claim a port it's listening on is
177 # still in use and fail to start.
178 stop_service $APACHE_NAME
179 sleep 3
180 start_service $APACHE_NAME
zhang-hared98a5d02013-06-21 18:18:02 +0800181}
182
183# Restore xtrace
184$XTRACE
185
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100186# Tell emacs to use shell-script-mode
187## Local variables:
188## mode: shell-script
189## End: