blob: 3a1f6f1263282446b75ea5037fefdc2cbbfa60d4 [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:
5# ``functions`` file
6# is_apache_enabled_service
zhang-hared98a5d02013-06-21 18:18:02 +08007# install_apache_wsgi
8# config_apache_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +10009# enable_apache_site
10# disable_apache_site
zhang-hared98a5d02013-06-21 18:18:02 +080011# start_apache_server
12# stop_apache_server
13# restart_apache_server
14
15# Save trace setting
16XTRACE=$(set +o | grep xtrace)
17set +o xtrace
18
19# Allow overriding the default Apache user and group, default to
20# current user and his default group.
21APACHE_USER=${APACHE_USER:-$USER}
22APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
23
24
25# Set up apache name and configuration directory
26if is_ubuntu; then
27 APACHE_NAME=apache2
28 APACHE_CONF_DIR=sites-available
29elif is_fedora; then
30 APACHE_NAME=httpd
31 APACHE_CONF_DIR=conf.d
32elif is_suse; then
33 APACHE_NAME=apache2
34 APACHE_CONF_DIR=vhosts.d
35fi
36
37# Functions
38# ---------
39
40# is_apache_enabled_service() checks if the service(s) specified as arguments are
41# apache enabled by the user in ``APACHE_ENABLED_SERVICES`` as web front end.
42#
43# Multiple services specified as arguments are ``OR``'ed together; the test
44# is a short-circuit boolean, i.e it returns on the first match.
45#
46# Uses global ``APACHE_ENABLED_SERVICES``
47# APACHE_ENABLED_SERVICES service [service ...]
48function is_apache_enabled_service() {
49 services=$@
50 for service in ${services}; do
51 [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
52 done
53 return 1
54}
55
zhang-hared98a5d02013-06-21 18:18:02 +080056# install_apache_wsgi() - Install Apache server and wsgi module
57function install_apache_wsgi() {
58 # Apache installation, because we mark it NOPRIME
59 if is_ubuntu; then
60 # Install apache2, which is NOPRIME'd
61 install_package apache2 libapache2-mod-wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100062 # WSGI isn't enabled by default, enable it
63 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080064 elif is_fedora; then
65 sudo rm -f /etc/httpd/conf.d/000-*
66 install_package httpd mod_wsgi
67 elif is_suse; then
68 install_package apache2 apache2-mod_wsgi
Jamie Lennox54707012013-09-17 12:07:48 +100069 # WSGI isn't enabled by default, enable it
70 sudo a2enmod wsgi
zhang-hared98a5d02013-06-21 18:18:02 +080071 else
72 exit_distro_not_supported "apache installation"
73 fi
74}
75
Jamie Lennox54707012013-09-17 12:07:48 +100076# enable_apache_site() - Enable a particular apache site
77function enable_apache_site() {
78 local site=$@
79 if is_ubuntu; then
80 sudo a2ensite ${site}
81 elif is_fedora; then
82 # fedora conf.d is only imported if it ends with .conf so this is approx the same
83 sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site} /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
84 fi
85}
86
87# disable_apache_site() - Disable a particular apache site
88function disable_apache_site() {
89 local site=$@
90 if is_ubuntu; then
91 sudo a2dissite ${site}
92 elif is_fedora; then
93 sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
94 fi
95}
96
zhang-hared98a5d02013-06-21 18:18:02 +080097# start_apache_server() - Start running apache server
98function start_apache_server() {
99 start_service $APACHE_NAME
100}
101
102# stop_apache_server() - Stop running apache server
103function stop_apache_server() {
104 if [ -n "$APACHE_NAME" ]; then
105 stop_service $APACHE_NAME
106 else
107 exit_distro_not_supported "apache configuration"
108 fi
109}
110
111# restart_apache_server
112function restart_apache_server() {
113 restart_service $APACHE_NAME
114}
115
116# Restore xtrace
117$XTRACE
118
119# Local variables:
120# mode: shell-script
121# End: