| Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 3 | # lib/apache | 
 | 4 | # Functions to control configuration and operation of apache web server | 
 | 5 |  | 
 | 6 | # Dependencies: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 7 | # | 
 | 8 | # - ``functions`` file | 
| Dean Troyer | d8864fe | 2014-02-17 11:00:42 -0600 | [diff] [blame] | 9 | # - ``STACK_USER`` must be defined | 
 | 10 | # | 
| Stephan Renatus | e578eff | 2013-11-19 13:31:04 +0100 | [diff] [blame] | 11 | # lib/apache exports the following functions: | 
 | 12 | # | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 13 | # - install_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 | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 22 | _XTRACE_LIB_APACHE=$(set +o | grep xtrace) | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 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 | # --------- | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 45 | # install_apache_wsgi() - Install Apache server and wsgi module | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 46 | function install_apache_wsgi { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 47 |     # Apache installation, because we mark it NOPRIME | 
 | 48 |     if is_ubuntu; then | 
 | 49 |         # Install apache2, which is NOPRIME'd | 
 | 50 |         install_package apache2 libapache2-mod-wsgi | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 51 |         # WSGI isn't enabled by default, enable it | 
 | 52 |         sudo a2enmod wsgi | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 53 |     elif is_fedora; then | 
 | 54 |         sudo rm -f /etc/httpd/conf.d/000-* | 
 | 55 |         install_package httpd mod_wsgi | 
 | 56 |     elif is_suse; then | 
 | 57 |         install_package apache2 apache2-mod_wsgi | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 58 |         # WSGI isn't enabled by default, enable it | 
 | 59 |         sudo a2enmod wsgi | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 60 |     else | 
 | 61 |         exit_distro_not_supported "apache installation" | 
 | 62 |     fi | 
| Noboru Iwamatsu | b4495eb | 2014-07-02 18:31:31 +0900 | [diff] [blame] | 63 |  | 
 | 64 |     # ensure mod_version enabled for <IfVersion ...>.  This is | 
 | 65 |     # built-in statically on anything recent, but precise (2.2) | 
 | 66 |     # doesn't have it enabled | 
 | 67 |     sudo a2enmod version || true | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 68 | } | 
 | 69 |  | 
| Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 70 | # get_apache_version() - return the version of Apache installed | 
 | 71 | # This function is used to determine the Apache version installed. There are | 
 | 72 | # various differences between Apache 2.2 and 2.4 that warrant special handling. | 
 | 73 | function get_apache_version { | 
 | 74 |     if is_ubuntu; then | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 75 |         local version_str | 
 | 76 |         version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/) | 
| Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 77 |     elif is_fedora; then | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 78 |         local version_str | 
 | 79 |         version_str=$(rpm -qa --queryformat '%{VERSION}' httpd) | 
| Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 80 |     elif is_suse; then | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 81 |         local version_str | 
 | 82 |         version_str=$(rpm -qa --queryformat '%{VERSION}' apache2) | 
| Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 83 |     else | 
 | 84 |         exit_distro_not_supported "cannot determine apache version" | 
 | 85 |     fi | 
 | 86 |     if [[ "$version_str" =~ ^2\.2\. ]]; then | 
 | 87 |         echo "2.2" | 
 | 88 |     elif [[ "$version_str" =~ ^2\.4\. ]]; then | 
 | 89 |         echo "2.4" | 
 | 90 |     else | 
 | 91 |         exit_distro_not_supported "apache version not supported" | 
 | 92 |     fi | 
 | 93 | } | 
 | 94 |  | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 95 | # apache_site_config_for() - The filename of the site's configuration file. | 
 | 96 | # This function uses the global variables APACHE_NAME and APACHE_CONF_DIR. | 
 | 97 | # | 
 | 98 | # On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to | 
 | 99 | # recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites' | 
 | 100 | # files are 000-default.conf and default-ssl.conf. | 
 | 101 | # | 
 | 102 | # On Ubuntu 12.04, the site configuration file may have any format, as long as it is in | 
 | 103 | # /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default | 
 | 104 | # sites' files are default and default-ssl. | 
 | 105 | # | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 106 | # On Fedora and openSUSE, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled. | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 107 | # | 
 | 108 | # On RHEL and CentOS, things should hopefully work as in Fedora. | 
 | 109 | # | 
 | 110 | # The table below summarizes what should happen on each distribution: | 
 | 111 | # +----------------------+--------------------+--------------------------+--------------------------+ | 
 | 112 | # | Distribution         | File name          | Site enabling command    | Site disabling command   | | 
 | 113 | # +----------------------+--------------------+--------------------------+--------------------------+ | 
 | 114 | # | Ubuntu 12.04         | site               | a2ensite site            | a2dissite site           | | 
 | 115 | # | Ubuntu 14.04         | site.conf          | a2ensite site            | a2dissite site           | | 
 | 116 | # | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} | | 
 | 117 | # +----------------------+--------------------+--------------------------+--------------------------+ | 
 | 118 | function apache_site_config_for { | 
 | 119 |     local site=$@ | 
 | 120 |     if is_ubuntu; then | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 121 |         local apache_version | 
 | 122 |         apache_version=$(get_apache_version) | 
| Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 123 |         if [[ "$apache_version" == "2.2" ]]; then | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 124 |             # Ubuntu 12.04 - Apache 2.2 | 
| Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 125 |             echo $APACHE_CONF_DIR/${site} | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 126 |         else | 
 | 127 |             # Ubuntu 14.04 - Apache 2.4 | 
| Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 128 |             echo $APACHE_CONF_DIR/${site}.conf | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 129 |         fi | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 130 |     elif is_fedora || is_suse; then | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 131 |         # 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] | 132 |         local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 133 |         if [ -f $enabled_site_file ]; then | 
 | 134 |             echo ${enabled_site_file} | 
 | 135 |         else | 
 | 136 |             echo ${enabled_site_file}.disabled | 
 | 137 |         fi | 
 | 138 |     fi | 
 | 139 | } | 
 | 140 |  | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 141 | # enable_apache_site() - Enable a particular apache site | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 142 | function enable_apache_site { | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 143 |     local site=$@ | 
 | 144 |     if is_ubuntu; then | 
 | 145 |         sudo a2ensite ${site} | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 146 |     elif is_fedora || is_suse; 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 site already enabled or no site config exists | 
 | 149 |         if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then | 
 | 150 |             sudo mv ${enabled_site_file}.disabled ${enabled_site_file} | 
 | 151 |         fi | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 152 |     fi | 
 | 153 | } | 
 | 154 |  | 
 | 155 | # disable_apache_site() - Disable a particular apache site | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 156 | function disable_apache_site { | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 157 |     local site=$@ | 
 | 158 |     if is_ubuntu; then | 
 | 159 |         sudo a2dissite ${site} | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 160 |     elif is_fedora || is_suse; then | 
| Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 161 |         local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" | 
 | 162 |         # Do nothing if no site config exists | 
 | 163 |         if [[ -f ${enabled_site_file} ]]; then | 
 | 164 |             sudo mv ${enabled_site_file} ${enabled_site_file}.disabled | 
 | 165 |         fi | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 166 |     fi | 
 | 167 | } | 
 | 168 |  | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 169 | # start_apache_server() - Start running apache server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 170 | function start_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 171 |     start_service $APACHE_NAME | 
 | 172 | } | 
 | 173 |  | 
 | 174 | # stop_apache_server() - Stop running apache server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 175 | function stop_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 176 |     if [ -n "$APACHE_NAME" ]; then | 
 | 177 |         stop_service $APACHE_NAME | 
 | 178 |     else | 
 | 179 |         exit_distro_not_supported "apache configuration" | 
 | 180 |     fi | 
 | 181 | } | 
 | 182 |  | 
 | 183 | # restart_apache_server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 184 | function restart_apache_server { | 
| Morgan Fainberg | 2df0046 | 2014-07-15 11:06:36 -0700 | [diff] [blame] | 185 |     # Apache can be slow to stop, doing an explicit stop, sleep, start helps | 
 | 186 |     # to mitigate issues where apache will claim a port it's listening on is | 
 | 187 |     # still in use and fail to start. | 
 | 188 |     stop_service $APACHE_NAME | 
 | 189 |     sleep 3 | 
 | 190 |     start_service $APACHE_NAME | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 191 | } | 
 | 192 |  | 
 | 193 | # Restore xtrace | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 194 | $_XTRACE_LIB_APACHE | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 195 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 196 | # Tell emacs to use shell-script-mode | 
 | 197 | ## Local variables: | 
 | 198 | ## mode: shell-script | 
 | 199 | ## End: |