blob: f4f82a1353d95fd83b4465f872532618563ec8aa [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
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000064# apache_site_config_for() - The filename of the site's configuration file.
65# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
66#
67# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
68# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
69# files are 000-default.conf and default-ssl.conf.
70#
71# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
72# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
73# sites' files are default and default-ssl.
74#
Ralf Haferkamp633a1292014-06-16 14:10:05 +020075# 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 +000076#
77# On RHEL and CentOS, things should hopefully work as in Fedora.
78#
79# The table below summarizes what should happen on each distribution:
80# +----------------------+--------------------+--------------------------+--------------------------+
81# | Distribution | File name | Site enabling command | Site disabling command |
82# +----------------------+--------------------+--------------------------+--------------------------+
83# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
84# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
85# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
86# +----------------------+--------------------+--------------------------+--------------------------+
87function apache_site_config_for {
88 local site=$@
89 if is_ubuntu; then
90 local apache_version=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
91 if [[ "$apache_version" =~ ^2\.2\. ]]; then
92 # Ubuntu 12.04 - Apache 2.2
Dean Troyer444a8d52014-06-06 16:36:52 -050093 echo $APACHE_CONF_DIR/${site}
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000094 else
95 # Ubuntu 14.04 - Apache 2.4
Dean Troyer444a8d52014-06-06 16:36:52 -050096 echo $APACHE_CONF_DIR/${site}.conf
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000097 fi
Ralf Haferkamp633a1292014-06-16 14:10:05 +020098 elif is_fedora || is_suse; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000099 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500100 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000101 if [ -f $enabled_site_file ]; then
102 echo ${enabled_site_file}
103 else
104 echo ${enabled_site_file}.disabled
105 fi
106 fi
107}
108
Jamie Lennox54707012013-09-17 12:07:48 +1000109# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000111 local site=$@
112 if is_ubuntu; then
113 sudo a2ensite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200114 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500115 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
116 # Do nothing if site already enabled or no site config exists
117 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
118 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
119 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000120 fi
121}
122
123# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100124function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000125 local site=$@
126 if is_ubuntu; then
127 sudo a2dissite ${site}
Ralf Haferkamp633a1292014-06-16 14:10:05 +0200128 elif is_fedora || is_suse; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500129 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
130 # Do nothing if no site config exists
131 if [[ -f ${enabled_site_file} ]]; then
132 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
133 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000134 fi
135}
136
zhang-hared98a5d02013-06-21 18:18:02 +0800137# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100138function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800139 start_service $APACHE_NAME
140}
141
142# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100143function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800144 if [ -n "$APACHE_NAME" ]; then
145 stop_service $APACHE_NAME
146 else
147 exit_distro_not_supported "apache configuration"
148 fi
149}
150
151# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700153 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
154 # to mitigate issues where apache will claim a port it's listening on is
155 # still in use and fail to start.
156 stop_service $APACHE_NAME
157 sleep 3
158 start_service $APACHE_NAME
zhang-hared98a5d02013-06-21 18:18:02 +0800159}
160
161# Restore xtrace
162$XTRACE
163
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100164# Tell emacs to use shell-script-mode
165## Local variables:
166## mode: shell-script
167## End: