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 |
Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 32 | # Note that APACHE_CONF_DIR is really more accurately apache's vhost |
| 33 | # configuration dir but we can't just change this because public interfaces. |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 34 | if is_ubuntu; then |
| 35 | APACHE_NAME=apache2 |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 36 | APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available} |
Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 37 | APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf-enabled} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 38 | elif is_fedora; then |
| 39 | APACHE_NAME=httpd |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 40 | APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d} |
Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 41 | APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf.d} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 42 | elif is_suse; then |
| 43 | APACHE_NAME=apache2 |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 44 | APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d} |
Clark Boylan | cfb9f05 | 2016-11-29 10:43:05 -0800 | [diff] [blame] | 45 | APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf.d} |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 46 | fi |
Clark Boylan | 66ce5c2 | 2016-10-05 12:11:05 -0700 | [diff] [blame] | 47 | APACHE_LOG_DIR="/var/log/${APACHE_NAME}" |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 48 | |
| 49 | # Functions |
| 50 | # --------- |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 51 | |
| 52 | # Enable apache mod and restart apache if it isn't already enabled. |
| 53 | function enable_apache_mod { |
| 54 | local mod=$1 |
| 55 | # Apache installation, because we mark it NOPRIME |
| 56 | if is_ubuntu || is_suse ; then |
| 57 | if ! a2query -m $mod ; then |
| 58 | sudo a2enmod $mod |
| 59 | restart_apache_server |
| 60 | fi |
| 61 | elif is_fedora; then |
| 62 | # pass |
| 63 | true |
| 64 | else |
| 65 | exit_distro_not_supported "apache enable mod" |
| 66 | fi |
| 67 | } |
| 68 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 69 | # install_apache_wsgi() - Install Apache server and wsgi module |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 70 | function install_apache_wsgi { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 71 | # Apache installation, because we mark it NOPRIME |
| 72 | if is_ubuntu; then |
| 73 | # Install apache2, which is NOPRIME'd |
Davanum Srinivas | afa8a00 | 2016-12-19 09:51:01 -0500 | [diff] [blame] | 74 | install_package apache2 |
| 75 | if python3_enabled; then |
| 76 | if is_package_installed libapache2-mod-wsgi; then |
| 77 | uninstall_package libapache2-mod-wsgi |
| 78 | fi |
| 79 | install_package libapache2-mod-wsgi-py3 |
| 80 | else |
| 81 | install_package libapache2-mod-wsgi |
| 82 | fi |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 83 | elif is_fedora; then |
| 84 | sudo rm -f /etc/httpd/conf.d/000-* |
| 85 | install_package httpd mod_wsgi |
| 86 | elif is_suse; then |
| 87 | install_package apache2 apache2-mod_wsgi |
| 88 | else |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 89 | exit_distro_not_supported "apache wsgi installation" |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 90 | fi |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 91 | # WSGI isn't enabled by default, enable it |
| 92 | enable_apache_mod wsgi |
Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 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 | # |
Sean Dague | 8f8b274 | 2017-04-13 09:34:12 -0400 | [diff] [blame] | 98 | # On Ubuntu 14.04+, the site configuration file must have a .conf suffix for a2ensite and a2dissite to |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 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 | # |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 102 | # 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] | 103 | # |
| 104 | # On RHEL and CentOS, things should hopefully work as in Fedora. |
| 105 | # |
| 106 | # The table below summarizes what should happen on each distribution: |
| 107 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 108 | # | Distribution | File name | Site enabling command | Site disabling command | |
| 109 | # +----------------------+--------------------+--------------------------+--------------------------+ |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 110 | # | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site | |
| 111 | # | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} | |
| 112 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 113 | function apache_site_config_for { |
| 114 | local site=$@ |
| 115 | if is_ubuntu; then |
Sean Dague | 8f8b274 | 2017-04-13 09:34:12 -0400 | [diff] [blame] | 116 | # Ubuntu 14.04 - Apache 2.4 |
| 117 | echo $APACHE_CONF_DIR/${site}.conf |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 118 | elif is_fedora || is_suse; then |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 119 | # 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] | 120 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 121 | if [ -f $enabled_site_file ]; then |
| 122 | echo ${enabled_site_file} |
| 123 | else |
| 124 | echo ${enabled_site_file}.disabled |
| 125 | fi |
| 126 | fi |
| 127 | } |
| 128 | |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 129 | # enable_apache_site() - Enable a particular apache site |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 130 | function enable_apache_site { |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 131 | local site=$@ |
| 132 | if is_ubuntu; then |
| 133 | sudo a2ensite ${site} |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 134 | elif is_fedora || is_suse; then |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 135 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
| 136 | # Do nothing if site already enabled or no site config exists |
| 137 | if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then |
| 138 | sudo mv ${enabled_site_file}.disabled ${enabled_site_file} |
| 139 | fi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 140 | fi |
| 141 | } |
| 142 | |
| 143 | # disable_apache_site() - Disable a particular apache site |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 144 | function disable_apache_site { |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 145 | local site=$@ |
| 146 | if is_ubuntu; then |
| 147 | sudo a2dissite ${site} |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 148 | elif is_fedora || is_suse; then |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 149 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
| 150 | # Do nothing if no site config exists |
| 151 | if [[ -f ${enabled_site_file} ]]; then |
| 152 | sudo mv ${enabled_site_file} ${enabled_site_file}.disabled |
| 153 | fi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 154 | fi |
| 155 | } |
| 156 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 157 | # start_apache_server() - Start running apache server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 158 | function start_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 159 | start_service $APACHE_NAME |
| 160 | } |
| 161 | |
| 162 | # stop_apache_server() - Stop running apache server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 163 | function stop_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 164 | if [ -n "$APACHE_NAME" ]; then |
| 165 | stop_service $APACHE_NAME |
| 166 | else |
| 167 | exit_distro_not_supported "apache configuration" |
| 168 | fi |
| 169 | } |
| 170 | |
| 171 | # restart_apache_server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 172 | function restart_apache_server { |
Morgan Fainberg | 2df0046 | 2014-07-15 11:06:36 -0700 | [diff] [blame] | 173 | # Apache can be slow to stop, doing an explicit stop, sleep, start helps |
| 174 | # to mitigate issues where apache will claim a port it's listening on is |
| 175 | # still in use and fail to start. |
Sean Dague | 2b85cf0 | 2017-04-13 09:02:14 -0400 | [diff] [blame^] | 176 | restart_service $APACHE_NAME |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 179 | # reload_apache_server |
| 180 | function reload_apache_server { |
| 181 | reload_service $APACHE_NAME |
| 182 | } |
| 183 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 184 | # Restore xtrace |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 185 | $_XTRACE_LIB_APACHE |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 186 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 187 | # Tell emacs to use shell-script-mode |
| 188 | ## Local variables: |
| 189 | ## mode: shell-script |
| 190 | ## End: |