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 | |
Sean Dague | 604e598 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 69 | # NOTE(sdague): Install uwsgi including apache module, we need to get |
| 70 | # to 2.0.6+ to get a working mod_proxy_uwsgi. We can probably build a |
| 71 | # check for that and do it differently for different platforms. |
| 72 | function install_apache_uwsgi { |
| 73 | local apxs="apxs2" |
| 74 | if is_fedora; then |
| 75 | apxs="apxs" |
| 76 | fi |
| 77 | |
| 78 | # Ubuntu xenial is back level on uwsgi so the proxy doesn't |
| 79 | # actually work. Hence we have to build from source for now. |
| 80 | # |
| 81 | # Centos 7 actually has the module in epel, but there was a big |
| 82 | # push to disable epel by default. As such, compile from source |
| 83 | # there as well. |
| 84 | |
| 85 | local dir |
| 86 | dir=$(mktemp -d) |
| 87 | pushd $dir |
| 88 | pip_install uwsgi |
| 89 | pip download uwsgi -c $REQUIREMENTS_DIR/upper-constraints.txt |
| 90 | local uwsgi |
| 91 | uwsgi=$(ls uwsgi*) |
| 92 | tar xvf $uwsgi |
| 93 | cd uwsgi*/apache2 |
| 94 | sudo $apxs -i -c mod_proxy_uwsgi.c |
| 95 | popd |
| 96 | # delete the temp directory |
| 97 | sudo rm -rf $dir |
| 98 | |
| 99 | if is_ubuntu; then |
| 100 | # we've got to enable proxy and proxy_uwsgi for this to work |
| 101 | sudo a2enmod proxy |
| 102 | sudo a2enmod proxy_uwsgi |
| 103 | elif is_fedora; then |
| 104 | # redhat is missing a nice way to turn on/off modules |
| 105 | echo "LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so" \ |
| 106 | | sudo tee /etc/httpd/conf.modules.d/02-proxy-uwsgi.conf |
| 107 | fi |
| 108 | restart_apache_server |
| 109 | } |
| 110 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 111 | # install_apache_wsgi() - Install Apache server and wsgi module |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 112 | function install_apache_wsgi { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 113 | # Apache installation, because we mark it NOPRIME |
| 114 | if is_ubuntu; then |
| 115 | # Install apache2, which is NOPRIME'd |
Davanum Srinivas | afa8a00 | 2016-12-19 09:51:01 -0500 | [diff] [blame] | 116 | install_package apache2 |
| 117 | if python3_enabled; then |
| 118 | if is_package_installed libapache2-mod-wsgi; then |
| 119 | uninstall_package libapache2-mod-wsgi |
| 120 | fi |
| 121 | install_package libapache2-mod-wsgi-py3 |
| 122 | else |
| 123 | install_package libapache2-mod-wsgi |
| 124 | fi |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 125 | elif is_fedora; then |
| 126 | sudo rm -f /etc/httpd/conf.d/000-* |
| 127 | install_package httpd mod_wsgi |
| 128 | elif is_suse; then |
| 129 | install_package apache2 apache2-mod_wsgi |
| 130 | else |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 131 | exit_distro_not_supported "apache wsgi installation" |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 132 | fi |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 133 | # WSGI isn't enabled by default, enable it |
| 134 | enable_apache_mod wsgi |
Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 137 | # apache_site_config_for() - The filename of the site's configuration file. |
| 138 | # This function uses the global variables APACHE_NAME and APACHE_CONF_DIR. |
| 139 | # |
Sean Dague | 8f8b274 | 2017-04-13 09:34:12 -0400 | [diff] [blame] | 140 | # 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] | 141 | # recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites' |
| 142 | # files are 000-default.conf and default-ssl.conf. |
| 143 | # |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 144 | # 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] | 145 | # |
| 146 | # On RHEL and CentOS, things should hopefully work as in Fedora. |
| 147 | # |
| 148 | # The table below summarizes what should happen on each distribution: |
| 149 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 150 | # | Distribution | File name | Site enabling command | Site disabling command | |
| 151 | # +----------------------+--------------------+--------------------------+--------------------------+ |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 152 | # | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site | |
| 153 | # | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} | |
| 154 | # +----------------------+--------------------+--------------------------+--------------------------+ |
| 155 | function apache_site_config_for { |
| 156 | local site=$@ |
| 157 | if is_ubuntu; then |
Sean Dague | 8f8b274 | 2017-04-13 09:34:12 -0400 | [diff] [blame] | 158 | # Ubuntu 14.04 - Apache 2.4 |
| 159 | echo $APACHE_CONF_DIR/${site}.conf |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 160 | elif is_fedora || is_suse; then |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 161 | # 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] | 162 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 163 | if [ -f $enabled_site_file ]; then |
| 164 | echo ${enabled_site_file} |
| 165 | else |
| 166 | echo ${enabled_site_file}.disabled |
| 167 | fi |
| 168 | fi |
| 169 | } |
| 170 | |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 171 | # enable_apache_site() - Enable a particular apache site |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 172 | function enable_apache_site { |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 173 | local site=$@ |
| 174 | if is_ubuntu; then |
| 175 | sudo a2ensite ${site} |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 176 | elif is_fedora || is_suse; then |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 177 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
| 178 | # Do nothing if site already enabled or no site config exists |
| 179 | if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then |
| 180 | sudo mv ${enabled_site_file}.disabled ${enabled_site_file} |
| 181 | fi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 182 | fi |
| 183 | } |
| 184 | |
| 185 | # disable_apache_site() - Disable a particular apache site |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 186 | function disable_apache_site { |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 187 | local site=$@ |
| 188 | if is_ubuntu; then |
Chris Dent | 2fcdaac | 2017-04-18 16:54:12 +0100 | [diff] [blame^] | 189 | sudo a2dissite ${site} || true |
Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 190 | elif is_fedora || is_suse; then |
Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 191 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
| 192 | # Do nothing if no site config exists |
| 193 | if [[ -f ${enabled_site_file} ]]; then |
| 194 | sudo mv ${enabled_site_file} ${enabled_site_file}.disabled |
| 195 | fi |
Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 196 | fi |
| 197 | } |
| 198 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 199 | # start_apache_server() - Start running apache server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 200 | function start_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 201 | start_service $APACHE_NAME |
| 202 | } |
| 203 | |
| 204 | # stop_apache_server() - Stop running apache server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 205 | function stop_apache_server { |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 206 | if [ -n "$APACHE_NAME" ]; then |
| 207 | stop_service $APACHE_NAME |
| 208 | else |
| 209 | exit_distro_not_supported "apache configuration" |
| 210 | fi |
| 211 | } |
| 212 | |
| 213 | # restart_apache_server |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 214 | function restart_apache_server { |
Morgan Fainberg | 2df0046 | 2014-07-15 11:06:36 -0700 | [diff] [blame] | 215 | # Apache can be slow to stop, doing an explicit stop, sleep, start helps |
| 216 | # to mitigate issues where apache will claim a port it's listening on is |
| 217 | # still in use and fail to start. |
Sean Dague | 2b85cf0 | 2017-04-13 09:02:14 -0400 | [diff] [blame] | 218 | restart_service $APACHE_NAME |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 221 | # reload_apache_server |
| 222 | function reload_apache_server { |
| 223 | reload_service $APACHE_NAME |
| 224 | } |
| 225 | |
Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 226 | function write_uwsgi_config { |
| 227 | local file=$1 |
| 228 | local wsgi=$2 |
| 229 | local url=$3 |
| 230 | local http=$4 |
| 231 | local name="" |
| 232 | name=$(basename $wsgi) |
| 233 | local socket="/tmp/${name}.socket" |
| 234 | |
| 235 | # always cleanup given that we are using iniset here |
| 236 | rm -rf $file |
| 237 | iniset "$file" uwsgi wsgi-file "$wsgi" |
| 238 | iniset "$file" uwsgi socket "$socket" |
| 239 | iniset "$file" uwsgi processes $API_WORKERS |
| 240 | # This is running standalone |
| 241 | iniset "$file" uwsgi master true |
| 242 | # Set die-on-term & exit-on-reload so that uwsgi shuts down |
| 243 | iniset "$file" uwsgi die-on-term true |
| 244 | iniset "$file" uwsgi exit-on-reload true |
| 245 | iniset "$file" uwsgi enable-threads true |
| 246 | iniset "$file" uwsgi plugins python |
| 247 | # uwsgi recommends this to prevent thundering herd on accept. |
| 248 | iniset "$file" uwsgi thunder-lock true |
| 249 | # Override the default size for headers from the 4k default. |
| 250 | iniset "$file" uwsgi buffer-size 65535 |
| 251 | # Make sure the client doesn't try to re-use the connection. |
| 252 | iniset "$file" uwsgi add-header "Connection: close" |
| 253 | # This ensures that file descriptors aren't shared between processes. |
| 254 | iniset "$file" uwsgi lazy-apps true |
| 255 | iniset "$file" uwsgi chmod-socket 666 |
| 256 | |
| 257 | # If we said bind directly to http, then do that and don't start the apache proxy |
| 258 | if [[ -n "$http" ]]; then |
| 259 | iniset "$file" uwsgi http $http |
| 260 | else |
| 261 | local apache_conf="" |
| 262 | apache_conf=$(apache_site_config_for $name) |
Sean Dague | 6ed5315 | 2017-04-13 13:33:16 -0400 | [diff] [blame] | 263 | echo "ProxyPass \"${url}\" \"unix:${socket}|uwsgi://uwsgi-uds-${name}/\" retry=0 " | sudo tee $apache_conf |
Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 264 | enable_apache_site $name |
| 265 | reload_apache_server |
| 266 | fi |
| 267 | } |
| 268 | |
| 269 | function remove_uwsgi_config { |
| 270 | local file=$1 |
| 271 | local wsgi=$2 |
| 272 | local name="" |
| 273 | name=$(basename $wsgi) |
| 274 | |
| 275 | rm -rf $file |
| 276 | disable_apache_site $name |
| 277 | } |
| 278 | |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 279 | # Restore xtrace |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 280 | $_XTRACE_LIB_APACHE |
zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 281 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 282 | # Tell emacs to use shell-script-mode |
| 283 | ## Local variables: |
| 284 | ## mode: shell-script |
| 285 | ## End: |