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