blob: 2d5e39a65d0ee670608bc71d0d878f5701fa9563 [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# - is_apache_enabled_service
12# - install_apache_wsgi
13# - config_apache_wsgi
14# - 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
33 APACHE_CONF_DIR=sites-available
34elif is_fedora; then
35 APACHE_NAME=httpd
36 APACHE_CONF_DIR=conf.d
37elif is_suse; then
38 APACHE_NAME=apache2
39 APACHE_CONF_DIR=vhosts.d
40fi
41
42# Functions
43# ---------
44
45# is_apache_enabled_service() checks if the service(s) specified as arguments are
46# apache enabled by the user in ``APACHE_ENABLED_SERVICES`` as web front end.
47#
48# Multiple services specified as arguments are ``OR``'ed together; the test
49# is a short-circuit boolean, i.e it returns on the first match.
50#
51# Uses global ``APACHE_ENABLED_SERVICES``
52# APACHE_ENABLED_SERVICES service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +110053function is_apache_enabled_service {
zhang-hared98a5d02013-06-21 18:18:02 +080054 services=$@
55 for service in ${services}; do
56 [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
57 done
58 return 1
59}
60
zhang-hared98a5d02013-06-21 18:18:02 +080061# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +110062function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +080063 # Apache installation, because we mark it NOPRIME
64 if is_ubuntu; then
65 # Install apache2, which is NOPRIME'd
66 install_package apache2 libapache2-mod-wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100067 # WSGI isn't enabled by default, enable it
68 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080069 elif is_fedora; then
70 sudo rm -f /etc/httpd/conf.d/000-*
71 install_package httpd mod_wsgi
72 elif is_suse; then
73 install_package apache2 apache2-mod_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100074 # WSGI isn't enabled by default, enable it
75 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080076 else
77 exit_distro_not_supported "apache installation"
78 fi
79}
80
Jamie Lennox54707012013-09-17 12:07:48 +100081# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +110082function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +100083 local site=$@
84 if is_ubuntu; then
85 sudo a2ensite ${site}
86 elif is_fedora; then
87 # fedora conf.d is only imported if it ends with .conf so this is approx the same
88 sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site} /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
89 fi
90}
91
92# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +110093function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +100094 local site=$@
95 if is_ubuntu; then
96 sudo a2dissite ${site}
97 elif is_fedora; then
98 sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
99 fi
100}
101
zhang-hared98a5d02013-06-21 18:18:02 +0800102# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100103function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800104 start_service $APACHE_NAME
105}
106
107# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100108function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800109 if [ -n "$APACHE_NAME" ]; then
110 stop_service $APACHE_NAME
111 else
112 exit_distro_not_supported "apache configuration"
113 fi
114}
115
116# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100117function restart_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800118 restart_service $APACHE_NAME
119}
120
121# Restore xtrace
122$XTRACE
123
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100124# Tell emacs to use shell-script-mode
125## Local variables:
126## mode: shell-script
127## End: