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 |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 14 | # - apache_site_config_for |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 15 | # - enable_apache_site |
| 16 | # - disable_apache_site |
| 17 | # - start_apache_server |
| 18 | # - stop_apache_server |
| 19 | # - restart_apache_server |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 20 | |
| 21 | # Save trace setting |
| 22 | XTRACE=$(set +o | grep xtrace) |
| 23 | set +o xtrace |
| 24 | |
| 25 | # Allow overriding the default Apache user and group, default to |
| 26 | # current user and his default group. |
Stephan Renatus | e578eff | 2013-11-19 13:31:04 +0100 | [diff] [blame] | 27 | APACHE_USER=${APACHE_USER:-$STACK_USER} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 28 | APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)} |
| 29 | |
| 30 | |
| 31 | # Set up apache name and configuration directory |
| 32 | if is_ubuntu; then |
| 33 | APACHE_NAME=apache2 |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 34 | APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 35 | elif is_fedora; then |
| 36 | APACHE_NAME=httpd |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 37 | APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 38 | elif is_suse; then |
| 39 | APACHE_NAME=apache2 |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 40 | APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 41 | fi |
| 42 | |
| 43 | # Functions |
| 44 | # --------- |
| 45 | |
| 46 | # is_apache_enabled_service() checks if the service(s) specified as arguments are |
| 47 | # apache enabled by the user in ``APACHE_ENABLED_SERVICES`` as web front end. |
| 48 | # |
| 49 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 50 | # is a short-circuit boolean, i.e it returns on the first match. |
| 51 | # |
| 52 | # Uses global ``APACHE_ENABLED_SERVICES`` |
| 53 | # APACHE_ENABLED_SERVICES service [service ...] |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 54 | function is_apache_enabled_service { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 55 | services=$@ |
| 56 | for service in ${services}; do |
| 57 | [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 58 | done |
| 59 | return 1 |
| 60 | } |
| 61 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 62 | # install_apache_wsgi() - Install Apache server and wsgi module |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 63 | function install_apache_wsgi { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 64 | # Apache installation, because we mark it NOPRIME |
| 65 | if is_ubuntu; then |
| 66 | # Install apache2, which is NOPRIME'd |
| 67 | install_package apache2 libapache2-mod-wsgi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 68 | # WSGI isn't enabled by default, enable it |
| 69 | sudo a2enmod wsgi |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 70 | elif is_fedora; then |
| 71 | sudo rm -f /etc/httpd/conf.d/000-* |
| 72 | install_package httpd mod_wsgi |
| 73 | elif is_suse; then |
| 74 | install_package apache2 apache2-mod_wsgi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 75 | # WSGI isn't enabled by default, enable it |
| 76 | sudo a2enmod wsgi |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 77 | else |
| 78 | exit_distro_not_supported "apache installation" |
| 79 | fi |
| 80 | } |
| 81 | |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 82 | # apache_site_config_for() - The filename of the site's configuration file. |
| 83 | # This function uses the global variables APACHE_NAME and APACHE_CONF_DIR. |
| 84 | # |
| 85 | # On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to |
| 86 | # recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites' |
| 87 | # files are 000-default.conf and default-ssl.conf. |
| 88 | # |
| 89 | # On Ubuntu 12.04, the site configuration file may have any format, as long as it is in |
| 90 | # /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default |
| 91 | # sites' files are default and default-ssl. |
| 92 | # |
| 93 | # On Fedora, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled. |
| 94 | # |
| 95 | # On RHEL and CentOS, things should hopefully work as in Fedora. |
| 96 | # |
| 97 | # The table below summarizes what should happen on each distribution: |
| 98 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 99 | # | Distribution | File name | Site enabling command | Site disabling command | |
| 100 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 101 | # | Ubuntu 12.04 | site | a2ensite site | a2dissite site | |
| 102 | # | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site | |
| 103 | # | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} | |
| 104 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 105 | function apache_site_config_for { |
| 106 | local site=$@ |
| 107 | if is_ubuntu; then |
| 108 | local apache_version=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/) |
| 109 | if [[ "$apache_version" =~ ^2\.2\. ]]; then |
| 110 | # Ubuntu 12.04 - Apache 2.2 |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 111 | echo $APACHE_CONF_DIR/${site} |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 112 | else |
| 113 | # Ubuntu 14.04 - Apache 2.4 |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 114 | echo $APACHE_CONF_DIR/${site}.conf |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 115 | fi |
| 116 | elif is_fedora; then |
| 117 | # fedora conf.d is only imported if it ends with .conf so this is approx the same |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 118 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 119 | if [ -f $enabled_site_file ]; then |
| 120 | echo ${enabled_site_file} |
| 121 | else |
| 122 | echo ${enabled_site_file}.disabled |
| 123 | fi |
| 124 | fi |
| 125 | } |
| 126 | |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 127 | # enable_apache_site() - Enable a particular apache site |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 128 | function enable_apache_site { |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 129 | local site=$@ |
| 130 | if is_ubuntu; then |
| 131 | sudo a2ensite ${site} |
| 132 | elif is_fedora; then |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 133 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
| 134 | # Do nothing if site already enabled or no site config exists |
| 135 | if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then |
| 136 | sudo mv ${enabled_site_file}.disabled ${enabled_site_file} |
| 137 | fi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 138 | fi |
| 139 | } |
| 140 | |
| 141 | # disable_apache_site() - Disable a particular apache site |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 142 | function disable_apache_site { |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 143 | local site=$@ |
| 144 | if is_ubuntu; then |
| 145 | sudo a2dissite ${site} |
| 146 | elif is_fedora; then |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame^] | 147 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
| 148 | # Do nothing if no site config exists |
| 149 | if [[ -f ${enabled_site_file} ]]; then |
| 150 | sudo mv ${enabled_site_file} ${enabled_site_file}.disabled |
| 151 | fi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 152 | fi |
| 153 | } |
| 154 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 155 | # start_apache_server() - Start running apache server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 156 | function start_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 157 | start_service $APACHE_NAME |
| 158 | } |
| 159 | |
| 160 | # stop_apache_server() - Stop running apache server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 161 | function stop_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 162 | if [ -n "$APACHE_NAME" ]; then |
| 163 | stop_service $APACHE_NAME |
| 164 | else |
| 165 | exit_distro_not_supported "apache configuration" |
| 166 | fi |
| 167 | } |
| 168 | |
| 169 | # restart_apache_server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 170 | function restart_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 171 | restart_service $APACHE_NAME |
| 172 | } |
| 173 | |
| 174 | # Restore xtrace |
| 175 | $XTRACE |
| 176 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 177 | # Tell emacs to use shell-script-mode |
| 178 | ## Local variables: |
| 179 | ## mode: shell-script |
| 180 | ## End: |