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: |
| 5 | # ``functions`` file |
| 6 | # is_apache_enabled_service |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 7 | # install_apache_wsgi |
| 8 | # config_apache_wsgi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 9 | # enable_apache_site |
| 10 | # disable_apache_site |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 11 | # start_apache_server |
| 12 | # stop_apache_server |
| 13 | # restart_apache_server |
| 14 | |
| 15 | # Save trace setting |
| 16 | XTRACE=$(set +o | grep xtrace) |
| 17 | set +o xtrace |
| 18 | |
| 19 | # Allow overriding the default Apache user and group, default to |
| 20 | # current user and his default group. |
| 21 | APACHE_USER=${APACHE_USER:-$USER} |
| 22 | APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)} |
| 23 | |
| 24 | |
| 25 | # Set up apache name and configuration directory |
| 26 | if is_ubuntu; then |
| 27 | APACHE_NAME=apache2 |
| 28 | APACHE_CONF_DIR=sites-available |
| 29 | elif is_fedora; then |
| 30 | APACHE_NAME=httpd |
| 31 | APACHE_CONF_DIR=conf.d |
| 32 | elif is_suse; then |
| 33 | APACHE_NAME=apache2 |
| 34 | APACHE_CONF_DIR=vhosts.d |
| 35 | fi |
| 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 ...] |
| 48 | function 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-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 56 | # install_apache_wsgi() - Install Apache server and wsgi module |
| 57 | function 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 Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 62 | # WSGI isn't enabled by default, enable it |
| 63 | sudo a2enmod wsgi |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 64 | 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 Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 69 | # WSGI isn't enabled by default, enable it |
| 70 | sudo a2enmod wsgi |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 71 | else |
| 72 | exit_distro_not_supported "apache installation" |
| 73 | fi |
| 74 | } |
| 75 | |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 76 | # enable_apache_site() - Enable a particular apache site |
| 77 | function 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 |
| 88 | function 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-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 97 | # start_apache_server() - Start running apache server |
| 98 | function start_apache_server() { |
| 99 | start_service $APACHE_NAME |
| 100 | } |
| 101 | |
| 102 | # stop_apache_server() - Stop running apache server |
| 103 | function 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 |
| 112 | function 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: |