| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 1 | # lib/apache | 
 | 2 | # Functions to control configuration and operation of apache web server | 
 | 3 |  | 
 | 4 | # Dependencies: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 5 | # | 
 | 6 | # - ``functions`` file | 
| Dean Troyer | d8864fe | 2014-02-17 11:00:42 -0600 | [diff] [blame] | 7 | # - ``STACK_USER`` must be defined | 
 | 8 | # | 
| Stephan Renatus | e578eff | 2013-11-19 13:31:04 +0100 | [diff] [blame] | 9 | # lib/apache exports the following functions: | 
 | 10 | # | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 11 | # - 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-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 19 |  | 
 | 20 | # Save trace setting | 
 | 21 | XTRACE=$(set +o | grep xtrace) | 
 | 22 | set +o xtrace | 
 | 23 |  | 
 | 24 | # Allow overriding the default Apache user and group, default to | 
 | 25 | # current user and his default group. | 
| Stephan Renatus | e578eff | 2013-11-19 13:31:04 +0100 | [diff] [blame] | 26 | APACHE_USER=${APACHE_USER:-$STACK_USER} | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 27 | APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)} | 
 | 28 |  | 
 | 29 |  | 
 | 30 | # Set up apache name and configuration directory | 
 | 31 | if is_ubuntu; then | 
 | 32 |     APACHE_NAME=apache2 | 
 | 33 |     APACHE_CONF_DIR=sites-available | 
 | 34 | elif is_fedora; then | 
 | 35 |     APACHE_NAME=httpd | 
 | 36 |     APACHE_CONF_DIR=conf.d | 
 | 37 | elif is_suse; then | 
 | 38 |     APACHE_NAME=apache2 | 
 | 39 |     APACHE_CONF_DIR=vhosts.d | 
 | 40 | fi | 
 | 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 53 | function is_apache_enabled_service { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 54 |     services=$@ | 
 | 55 |     for service in ${services}; do | 
 | 56 |         [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 | 
 | 57 |     done | 
 | 58 |     return 1 | 
 | 59 | } | 
 | 60 |  | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 61 | # install_apache_wsgi() - Install Apache server and wsgi module | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 62 | function install_apache_wsgi { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 63 |     # 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 Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 67 |         # WSGI isn't enabled by default, enable it | 
 | 68 |         sudo a2enmod wsgi | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 69 |     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 Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 74 |         # WSGI isn't enabled by default, enable it | 
 | 75 |         sudo a2enmod wsgi | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 76 |     else | 
 | 77 |         exit_distro_not_supported "apache installation" | 
 | 78 |     fi | 
 | 79 | } | 
 | 80 |  | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 81 | # enable_apache_site() - Enable a particular apache site | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 82 | function enable_apache_site { | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 83 |     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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 93 | function disable_apache_site { | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 94 |     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-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 102 | # start_apache_server() - Start running apache server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 103 | function start_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 104 |     start_service $APACHE_NAME | 
 | 105 | } | 
 | 106 |  | 
 | 107 | # stop_apache_server() - Stop running apache server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 108 | function stop_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 109 |     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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 117 | function restart_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 118 |     restart_service $APACHE_NAME | 
 | 119 | } | 
 | 120 |  | 
 | 121 | # Restore xtrace | 
 | 122 | $XTRACE | 
 | 123 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 124 | # Tell emacs to use shell-script-mode | 
 | 125 | ## Local variables: | 
 | 126 | ## mode: shell-script | 
 | 127 | ## End: |