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