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