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