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