| 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 | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 56 | if is_ubuntu; then | 
|  | 57 | # Skip mod_version as it is not a valid mod to enable | 
|  | 58 | # on debuntu, instead it is built in. | 
|  | 59 | if [[ "$mod" != "version" ]] && ! a2query -m $mod ; then | 
|  | 60 | sudo a2enmod $mod | 
|  | 61 | restart_apache_server | 
|  | 62 | fi | 
|  | 63 | elif is_suse; then | 
|  | 64 | if ! a2enmod -q $mod ; then | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 65 | sudo a2enmod $mod | 
|  | 66 | restart_apache_server | 
|  | 67 | fi | 
|  | 68 | elif is_fedora; then | 
|  | 69 | # pass | 
|  | 70 | true | 
|  | 71 | else | 
|  | 72 | exit_distro_not_supported "apache enable mod" | 
|  | 73 | fi | 
|  | 74 | } | 
|  | 75 |  | 
| Sean Dague | 604e598 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 76 | # NOTE(sdague): Install uwsgi including apache module, we need to get | 
|  | 77 | # to 2.0.6+ to get a working mod_proxy_uwsgi. We can probably build a | 
|  | 78 | # check for that and do it differently for different platforms. | 
|  | 79 | function install_apache_uwsgi { | 
|  | 80 | local apxs="apxs2" | 
|  | 81 | if is_fedora; then | 
|  | 82 | apxs="apxs" | 
|  | 83 | fi | 
|  | 84 |  | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 85 | # This varies based on packaged/installed.  If we've | 
|  | 86 | # pip_installed, then the pip setup will only build a "python" | 
|  | 87 | # module that will be either python2 or python3 depending on what | 
|  | 88 | # it was built with. | 
| Sean Dague | 604e598 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 89 | # | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 90 | # For package installs, the distro ships both plugins and you need | 
|  | 91 | # to select the right one ... it will not be autodetected. | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 92 | UWSGI_PYTHON_PLUGIN=python3 | 
| Sean Dague | 604e598 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 93 |  | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 94 | if is_ubuntu; then | 
| Dr. Jens Harbott | 3480093 | 2020-02-13 09:38:35 +0000 | [diff] [blame] | 95 | local pkg_list="uwsgi uwsgi-plugin-python3 libapache2-mod-proxy-uwsgi" | 
| Dr. Jens Harbott | 3480093 | 2020-02-13 09:38:35 +0000 | [diff] [blame] | 96 | install_package ${pkg_list} | 
| Federico Ressi | c2c2b6b | 2020-06-15 12:48:38 +0200 | [diff] [blame] | 97 | elif is_fedora; then | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 98 | # Note httpd comes with mod_proxy_uwsgi and it is loaded by | 
|  | 99 | # default; the mod_proxy_uwsgi package actually conflicts now. | 
|  | 100 | # See: | 
|  | 101 | #  https://bugzilla.redhat.com/show_bug.cgi?id=1574335 | 
|  | 102 | # | 
|  | 103 | # Thus there is nothing else to do after this install | 
|  | 104 | install_package uwsgi \ | 
|  | 105 | uwsgi-plugin-python3 | 
| Andreas Jaeger | 10c3ffd | 2020-06-15 10:03:42 +0200 | [diff] [blame] | 106 | elif [[ $os_VENDOR =~ openSUSE ]]; then | 
|  | 107 | install_package uwsgi \ | 
|  | 108 | uwsgi-python3 \ | 
|  | 109 | apache2-mod_uwsgi | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 110 | else | 
| Federico Ressi | c2c2b6b | 2020-06-15 12:48:38 +0200 | [diff] [blame] | 111 | # Compile uwsgi from source. | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 112 | local dir | 
|  | 113 | dir=$(mktemp -d) | 
|  | 114 | pushd $dir | 
|  | 115 | pip_install uwsgi | 
|  | 116 | pip download uwsgi -c $REQUIREMENTS_DIR/upper-constraints.txt | 
|  | 117 | local uwsgi | 
|  | 118 | uwsgi=$(ls uwsgi*) | 
|  | 119 | tar xvf $uwsgi | 
|  | 120 | cd uwsgi*/apache2 | 
|  | 121 | sudo $apxs -i -c mod_proxy_uwsgi.c | 
|  | 122 | popd | 
|  | 123 | # delete the temp directory | 
|  | 124 | sudo rm -rf $dir | 
|  | 125 | UWSGI_PYTHON_PLUGIN=python | 
|  | 126 | fi | 
| Sean Dague | 604e598 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 127 |  | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 128 | if is_ubuntu || is_suse ; then | 
| Sean Dague | 604e598 | 2017-04-13 13:28:12 -0400 | [diff] [blame] | 129 | # we've got to enable proxy and proxy_uwsgi for this to work | 
|  | 130 | sudo a2enmod proxy | 
|  | 131 | sudo a2enmod proxy_uwsgi | 
|  | 132 | elif is_fedora; then | 
|  | 133 | # redhat is missing a nice way to turn on/off modules | 
|  | 134 | echo "LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so" \ | 
|  | 135 | | sudo tee /etc/httpd/conf.modules.d/02-proxy-uwsgi.conf | 
|  | 136 | fi | 
|  | 137 | restart_apache_server | 
|  | 138 | } | 
|  | 139 |  | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 140 | # install_apache_wsgi() - Install Apache server and wsgi module | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 141 | function install_apache_wsgi { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 142 | # Apache installation, because we mark it NOPRIME | 
|  | 143 | if is_ubuntu; then | 
|  | 144 | # Install apache2, which is NOPRIME'd | 
| Davanum Srinivas | afa8a00 | 2016-12-19 09:51:01 -0500 | [diff] [blame] | 145 | install_package apache2 | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 146 | if is_package_installed libapache2-mod-wsgi; then | 
|  | 147 | uninstall_package libapache2-mod-wsgi | 
| Davanum Srinivas | afa8a00 | 2016-12-19 09:51:01 -0500 | [diff] [blame] | 148 | fi | 
| Jens Harbott | d7a82f4 | 2020-06-23 10:21:09 +0200 | [diff] [blame] | 149 | install_package libapache2-mod-wsgi-py3 | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 150 | elif is_fedora; then | 
|  | 151 | sudo rm -f /etc/httpd/conf.d/000-* | 
| Hirotaka Wakabayashi | 1e26508 | 2020-07-02 06:19:21 +0000 | [diff] [blame] | 152 | install_package httpd python3-mod_wsgi | 
| Ian Wienand | 41e6e12 | 2017-08-08 15:06:26 +1000 | [diff] [blame] | 153 | # For consistency with Ubuntu, switch to the worker mpm, as | 
| Attila Fazekas | 9fd38e7 | 2017-12-11 12:20:25 +0100 | [diff] [blame] | 154 | # the default is event | 
| Ian Wienand | 41e6e12 | 2017-08-08 15:06:26 +1000 | [diff] [blame] | 155 | sudo sed -i '/mod_mpm_prefork.so/s/^/#/g' /etc/httpd/conf.modules.d/00-mpm.conf | 
| Attila Fazekas | 9fd38e7 | 2017-12-11 12:20:25 +0100 | [diff] [blame] | 156 | sudo sed -i '/mod_mpm_event.so/s/^/#/g' /etc/httpd/conf.modules.d/00-mpm.conf | 
| Ian Wienand | 41e6e12 | 2017-08-08 15:06:26 +1000 | [diff] [blame] | 157 | sudo sed -i '/mod_mpm_worker.so/s/^#//g' /etc/httpd/conf.modules.d/00-mpm.conf | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 158 | elif is_suse; then | 
|  | 159 | install_package apache2 apache2-mod_wsgi | 
|  | 160 | else | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 161 | exit_distro_not_supported "apache wsgi installation" | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 162 | fi | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 163 | # WSGI isn't enabled by default, enable it | 
|  | 164 | enable_apache_mod wsgi | 
| Morgan Fainberg | d074dc7 | 2014-06-24 21:33:39 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 167 | # apache_site_config_for() - The filename of the site's configuration file. | 
|  | 168 | # This function uses the global variables APACHE_NAME and APACHE_CONF_DIR. | 
|  | 169 | # | 
| Sean Dague | 8f8b274 | 2017-04-13 09:34:12 -0400 | [diff] [blame] | 170 | # 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] | 171 | # recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites' | 
|  | 172 | # files are 000-default.conf and default-ssl.conf. | 
|  | 173 | # | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 174 | # 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] | 175 | # | 
|  | 176 | # On RHEL and CentOS, things should hopefully work as in Fedora. | 
|  | 177 | # | 
|  | 178 | # The table below summarizes what should happen on each distribution: | 
|  | 179 | # +----------------------+--------------------+--------------------------+--------------------------+ | 
|  | 180 | # | Distribution         | File name          | Site enabling command    | Site disabling command   | | 
|  | 181 | # +----------------------+--------------------+--------------------------+--------------------------+ | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 182 | # | Ubuntu 14.04         | site.conf          | a2ensite site            | a2dissite site           | | 
|  | 183 | # | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} | | 
|  | 184 | # +----------------------+--------------------+--------------------------+--------------------------+ | 
|  | 185 | function apache_site_config_for { | 
|  | 186 | local site=$@ | 
|  | 187 | if is_ubuntu; then | 
| Sean Dague | 8f8b274 | 2017-04-13 09:34:12 -0400 | [diff] [blame] | 188 | # Ubuntu 14.04 - Apache 2.4 | 
|  | 189 | echo $APACHE_CONF_DIR/${site}.conf | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 190 | elif is_fedora || is_suse; then | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 191 | # 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] | 192 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" | 
| Gabriel Assis Bezerra | a688bc6 | 2014-05-27 20:58:22 +0000 | [diff] [blame] | 193 | if [ -f $enabled_site_file ]; then | 
|  | 194 | echo ${enabled_site_file} | 
|  | 195 | else | 
|  | 196 | echo ${enabled_site_file}.disabled | 
|  | 197 | fi | 
|  | 198 | fi | 
|  | 199 | } | 
|  | 200 |  | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 201 | # enable_apache_site() - Enable a particular apache site | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 202 | function enable_apache_site { | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 203 | local site=$@ | 
| Clark Boylan | 35649ae | 2017-05-27 17:52:55 -0700 | [diff] [blame] | 204 | # Many of our sites use mod version. Just enable it. | 
|  | 205 | enable_apache_mod version | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 206 | if is_ubuntu; then | 
|  | 207 | sudo a2ensite ${site} | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 208 | elif is_fedora || is_suse; then | 
| Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 209 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" | 
|  | 210 | # Do nothing if site already enabled or no site config exists | 
|  | 211 | if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then | 
|  | 212 | sudo mv ${enabled_site_file}.disabled ${enabled_site_file} | 
|  | 213 | fi | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 214 | fi | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | # disable_apache_site() - Disable a particular apache site | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 218 | function disable_apache_site { | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 219 | local site=$@ | 
|  | 220 | if is_ubuntu; then | 
| Chris Dent | 2fcdaac | 2017-04-18 16:54:12 +0100 | [diff] [blame] | 221 | sudo a2dissite ${site} || true | 
| Ralf Haferkamp | 633a129 | 2014-06-16 14:10:05 +0200 | [diff] [blame] | 222 | elif is_fedora || is_suse; then | 
| Dean Troyer | 444a8d5 | 2014-06-06 16:36:52 -0500 | [diff] [blame] | 223 | local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" | 
|  | 224 | # Do nothing if no site config exists | 
|  | 225 | if [[ -f ${enabled_site_file} ]]; then | 
|  | 226 | sudo mv ${enabled_site_file} ${enabled_site_file}.disabled | 
|  | 227 | fi | 
| Jamie Lennox | 5470701 | 2013-09-17 12:07:48 +1000 | [diff] [blame] | 228 | fi | 
|  | 229 | } | 
|  | 230 |  | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 231 | # start_apache_server() - Start running apache server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 232 | function start_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 233 | start_service $APACHE_NAME | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | # stop_apache_server() - Stop running apache server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 237 | function stop_apache_server { | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 238 | if [ -n "$APACHE_NAME" ]; then | 
|  | 239 | stop_service $APACHE_NAME | 
|  | 240 | else | 
|  | 241 | exit_distro_not_supported "apache configuration" | 
|  | 242 | fi | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | # restart_apache_server | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 246 | function restart_apache_server { | 
| Morgan Fainberg | 2df0046 | 2014-07-15 11:06:36 -0700 | [diff] [blame] | 247 | # Apache can be slow to stop, doing an explicit stop, sleep, start helps | 
|  | 248 | # to mitigate issues where apache will claim a port it's listening on is | 
|  | 249 | # still in use and fail to start. | 
| Sean Dague | 2b85cf0 | 2017-04-13 09:02:14 -0400 | [diff] [blame] | 250 | restart_service $APACHE_NAME | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 253 | function write_uwsgi_config { | 
|  | 254 | local file=$1 | 
|  | 255 | local wsgi=$2 | 
|  | 256 | local url=$3 | 
|  | 257 | local http=$4 | 
|  | 258 | local name="" | 
|  | 259 | name=$(basename $wsgi) | 
| rabi | aa26baa | 2017-04-20 10:55:16 +0530 | [diff] [blame] | 260 |  | 
|  | 261 | # create a home for the sockets; note don't use /tmp -- apache has | 
|  | 262 | # a private view of it on some platforms. | 
|  | 263 | local socket_dir='/var/run/uwsgi' | 
| Kirill Zaitsev | d0db62a | 2017-05-26 19:02:52 +0300 | [diff] [blame] | 264 |  | 
|  | 265 | # /var/run will be empty on ubuntu after reboot, so we can use systemd-temptiles | 
|  | 266 | # to automatically create $socket_dir. | 
|  | 267 | sudo mkdir -p /etc/tmpfiles.d/ | 
|  | 268 | echo "d $socket_dir 0755 $STACK_USER root" | sudo tee /etc/tmpfiles.d/uwsgi.conf | 
|  | 269 | sudo systemd-tmpfiles --create /etc/tmpfiles.d/uwsgi.conf | 
|  | 270 |  | 
| rabi | aa26baa | 2017-04-20 10:55:16 +0530 | [diff] [blame] | 271 | local socket="$socket_dir/${name}.socket" | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 272 |  | 
|  | 273 | # always cleanup given that we are using iniset here | 
|  | 274 | rm -rf $file | 
|  | 275 | iniset "$file" uwsgi wsgi-file "$wsgi" | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 276 | iniset "$file" uwsgi processes $API_WORKERS | 
|  | 277 | # This is running standalone | 
|  | 278 | iniset "$file" uwsgi master true | 
|  | 279 | # Set die-on-term & exit-on-reload so that uwsgi shuts down | 
|  | 280 | iniset "$file" uwsgi die-on-term true | 
| Dinesh Bhor | ef60f2b | 2017-09-05 14:40:32 +0530 | [diff] [blame] | 281 | iniset "$file" uwsgi exit-on-reload false | 
| Matthew Treinish | 477a962 | 2017-08-04 11:09:26 -0400 | [diff] [blame] | 282 | # Set worker-reload-mercy so that worker will not exit till the time | 
|  | 283 | # configured after graceful shutdown | 
|  | 284 | iniset "$file" uwsgi worker-reload-mercy $WORKER_TIMEOUT | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 285 | iniset "$file" uwsgi enable-threads true | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 286 | iniset "$file" uwsgi plugins http,${UWSGI_PYTHON_PLUGIN} | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 287 | # uwsgi recommends this to prevent thundering herd on accept. | 
|  | 288 | iniset "$file" uwsgi thunder-lock true | 
| Matthew Treinish | 477a962 | 2017-08-04 11:09:26 -0400 | [diff] [blame] | 289 | # Set hook to trigger graceful shutdown on SIGTERM | 
|  | 290 | iniset "$file" uwsgi hook-master-start "unix_signal:15 gracefully_kill_them_all" | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 291 | # Override the default size for headers from the 4k default. | 
|  | 292 | iniset "$file" uwsgi buffer-size 65535 | 
|  | 293 | # Make sure the client doesn't try to re-use the connection. | 
|  | 294 | iniset "$file" uwsgi add-header "Connection: close" | 
|  | 295 | # This ensures that file descriptors aren't shared between processes. | 
|  | 296 | iniset "$file" uwsgi lazy-apps true | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 297 |  | 
|  | 298 | # If we said bind directly to http, then do that and don't start the apache proxy | 
|  | 299 | if [[ -n "$http" ]]; then | 
|  | 300 | iniset "$file" uwsgi http $http | 
|  | 301 | else | 
|  | 302 | local apache_conf="" | 
|  | 303 | apache_conf=$(apache_site_config_for $name) | 
| Chris Dent | b90bb1a | 2017-04-18 16:30:14 +0000 | [diff] [blame] | 304 | iniset "$file" uwsgi socket "$socket" | 
|  | 305 | iniset "$file" uwsgi chmod-socket 666 | 
| Jens Harbott | 56e75e4 | 2021-09-28 20:02:34 +0200 | [diff] [blame] | 306 | echo "ProxyPass \"${url}\" \"unix:${socket}|uwsgi://uwsgi-uds-${name}\" retry=0 " | sudo tee -a $apache_conf | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 307 | enable_apache_site $name | 
| Ian Wienand | f6a2d2c | 2017-04-26 10:50:29 +1000 | [diff] [blame] | 308 | restart_apache_server | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 309 | fi | 
|  | 310 | } | 
|  | 311 |  | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 312 | # For services using chunked encoding, the only services known to use this | 
|  | 313 | # currently are Glance and Swift, we need to use an http proxy instead of | 
|  | 314 | # mod_proxy_uwsgi because the chunked encoding gets dropped. See: | 
|  | 315 | # https://github.com/unbit/uwsgi/issues/1540 You can workaround this on python2 | 
|  | 316 | # but that involves having apache buffer the request before sending it to | 
| Jeremy Liu | 2f7df51 | 2017-07-12 10:09:48 +0800 | [diff] [blame] | 317 | # uwsgi. | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 318 | function write_local_uwsgi_http_config { | 
|  | 319 | local file=$1 | 
|  | 320 | local wsgi=$2 | 
|  | 321 | local url=$3 | 
|  | 322 | name=$(basename $wsgi) | 
|  | 323 |  | 
|  | 324 | # create a home for the sockets; note don't use /tmp -- apache has | 
|  | 325 | # a private view of it on some platforms. | 
|  | 326 |  | 
|  | 327 | # always cleanup given that we are using iniset here | 
|  | 328 | rm -rf $file | 
|  | 329 | iniset "$file" uwsgi wsgi-file "$wsgi" | 
|  | 330 | port=$(get_random_port) | 
| Matthew Treinish | 1560efe | 2017-06-30 12:15:26 -0400 | [diff] [blame] | 331 | iniset "$file" uwsgi http-socket "127.0.0.1:$port" | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 332 | iniset "$file" uwsgi processes $API_WORKERS | 
|  | 333 | # This is running standalone | 
|  | 334 | iniset "$file" uwsgi master true | 
|  | 335 | # Set die-on-term & exit-on-reload so that uwsgi shuts down | 
|  | 336 | iniset "$file" uwsgi die-on-term true | 
| Dinesh Bhor | ef60f2b | 2017-09-05 14:40:32 +0530 | [diff] [blame] | 337 | iniset "$file" uwsgi exit-on-reload false | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 338 | iniset "$file" uwsgi enable-threads true | 
| Ian Wienand | 2d90356 | 2018-05-03 10:51:30 +1000 | [diff] [blame] | 339 | iniset "$file" uwsgi plugins http,${UWSGI_PYTHON_PLUGIN} | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 340 | # uwsgi recommends this to prevent thundering herd on accept. | 
|  | 341 | iniset "$file" uwsgi thunder-lock true | 
| Matthew Treinish | 477a962 | 2017-08-04 11:09:26 -0400 | [diff] [blame] | 342 | # Set hook to trigger graceful shutdown on SIGTERM | 
|  | 343 | iniset "$file" uwsgi hook-master-start "unix_signal:15 gracefully_kill_them_all" | 
|  | 344 | # Set worker-reload-mercy so that worker will not exit till the time | 
|  | 345 | # configured after graceful shutdown | 
|  | 346 | iniset "$file" uwsgi worker-reload-mercy $WORKER_TIMEOUT | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 347 | # Override the default size for headers from the 4k default. | 
|  | 348 | iniset "$file" uwsgi buffer-size 65535 | 
|  | 349 | # Make sure the client doesn't try to re-use the connection. | 
|  | 350 | iniset "$file" uwsgi add-header "Connection: close" | 
|  | 351 | # This ensures that file descriptors aren't shared between processes. | 
|  | 352 | iniset "$file" uwsgi lazy-apps true | 
|  | 353 | iniset "$file" uwsgi chmod-socket 666 | 
|  | 354 | iniset "$file" uwsgi http-raw-body true | 
|  | 355 | iniset "$file" uwsgi http-chunked-input true | 
|  | 356 | iniset "$file" uwsgi http-auto-chunked true | 
| Matthew Treinish | 82d0610 | 2017-06-28 17:42:31 -0400 | [diff] [blame] | 357 | iniset "$file" uwsgi http-keepalive false | 
| Matthew Treinish | b79531a | 2017-06-30 12:10:06 -0400 | [diff] [blame] | 358 | # Increase socket timeout for slow chunked uploads | 
|  | 359 | iniset "$file" uwsgi socket-timeout 30 | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 360 |  | 
|  | 361 | enable_apache_mod proxy | 
|  | 362 | enable_apache_mod proxy_http | 
|  | 363 | local apache_conf="" | 
|  | 364 | apache_conf=$(apache_site_config_for $name) | 
|  | 365 | echo "KeepAlive Off" | sudo tee $apache_conf | 
| Matthew Treinish | a3488d5 | 2017-08-10 14:55:15 -0400 | [diff] [blame] | 366 | echo "SetEnv proxy-sendchunked 1" | sudo tee -a $apache_conf | 
| Matthew Treinish | 1fa6536 | 2017-06-23 22:32:37 +0000 | [diff] [blame] | 367 | echo "ProxyPass \"${url}\" \"http://127.0.0.1:$port\" retry=0 " | sudo tee -a $apache_conf | 
|  | 368 | enable_apache_site $name | 
|  | 369 | restart_apache_server | 
|  | 370 | } | 
|  | 371 |  | 
| Dan Smith | 09eea0b | 2020-07-09 08:31:51 -0700 | [diff] [blame] | 372 | # Write a straight-through proxy for a service that runs locally and just needs | 
|  | 373 | # to be reachable via the main http proxy at $loc | 
|  | 374 | function write_local_proxy_http_config { | 
|  | 375 | local name=$1 | 
|  | 376 | local url=$2 | 
|  | 377 | local loc=$3 | 
|  | 378 | local apache_conf | 
|  | 379 | apache_conf=$(apache_site_config_for $name) | 
|  | 380 |  | 
|  | 381 | enable_apache_mod proxy | 
|  | 382 | enable_apache_mod proxy_http | 
|  | 383 |  | 
|  | 384 | echo "KeepAlive Off" | sudo tee $apache_conf | 
|  | 385 | echo "SetEnv proxy-sendchunked 1" | sudo tee -a $apache_conf | 
|  | 386 | echo "ProxyPass \"${loc}\" \"$url\" retry=0 " | sudo tee -a $apache_conf | 
|  | 387 | enable_apache_site $name | 
|  | 388 | restart_apache_server | 
|  | 389 | } | 
|  | 390 |  | 
| Sean Dague | 2f8c88e | 2017-04-13 09:08:39 -0400 | [diff] [blame] | 391 | function remove_uwsgi_config { | 
|  | 392 | local file=$1 | 
|  | 393 | local wsgi=$2 | 
|  | 394 | local name="" | 
|  | 395 | name=$(basename $wsgi) | 
|  | 396 |  | 
|  | 397 | rm -rf $file | 
|  | 398 | disable_apache_site $name | 
|  | 399 | } | 
|  | 400 |  | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 401 | # Restore xtrace | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 402 | $_XTRACE_LIB_APACHE | 
| zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame] | 403 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 404 | # Tell emacs to use shell-script-mode | 
|  | 405 | ## Local variables: | 
|  | 406 | ## mode: shell-script | 
|  | 407 | ## End: |