blob: 15b4297809afc369bc77bf8235a6faf433db6cb8 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
zhang-hared98a5d02013-06-21 18:18:02 +08003# lib/apache
4# Functions to control configuration and operation of apache web server
5
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - ``functions`` file
Dean Troyerd8864fe2014-02-17 11:00:42 -06009# - ``STACK_USER`` must be defined
10#
Stephan Renatuse578eff2013-11-19 13:31:04 +010011# lib/apache exports the following functions:
12#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010013# - install_apache_wsgi
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +000014# - apache_site_config_for
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010015# - enable_apache_site
16# - disable_apache_site
17# - start_apache_server
18# - stop_apache_server
19# - restart_apache_server
zhang-hared98a5d02013-06-21 18:18:02 +080020
21# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110022_XTRACE_LIB_APACHE=$(set +o | grep xtrace)
zhang-hared98a5d02013-06-21 18:18:02 +080023set +o xtrace
24
25# Allow overriding the default Apache user and group, default to
26# current user and his default group.
Stephan Renatuse578eff2013-11-19 13:31:04 +010027APACHE_USER=${APACHE_USER:-$STACK_USER}
zhang-hared98a5d02013-06-21 18:18:02 +080028APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
29
Brian Haley7943a922022-03-14 13:53:41 -040030APACHE_LOCAL_HOST=$SERVICE_LOCAL_HOST
31if [[ "$SERVICE_IP_VERSION" == 6 ]]; then
32 APACHE_LOCAL_HOST=[$APACHE_LOCAL_HOST]
33fi
34
zhang-hared98a5d02013-06-21 18:18:02 +080035
36# Set up apache name and configuration directory
Clark Boylancfb9f052016-11-29 10:43:05 -080037# 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-hared98a5d02013-06-21 18:18:02 +080039if is_ubuntu; then
40 APACHE_NAME=apache2
Dean Troyer444a8d52014-06-06 16:36:52 -050041 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available}
Clark Boylancfb9f052016-11-29 10:43:05 -080042 APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf-enabled}
zhang-hared98a5d02013-06-21 18:18:02 +080043elif is_fedora; then
44 APACHE_NAME=httpd
Dean Troyer444a8d52014-06-06 16:36:52 -050045 APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d}
Clark Boylancfb9f052016-11-29 10:43:05 -080046 APACHE_SETTINGS_DIR=${APACHE_SETTINGS_DIR:-/etc/$APACHE_NAME/conf.d}
zhang-hared98a5d02013-06-21 18:18:02 +080047fi
Clark Boylan66ce5c22016-10-05 12:11:05 -070048APACHE_LOG_DIR="/var/log/${APACHE_NAME}"
zhang-hared98a5d02013-06-21 18:18:02 +080049
50# Functions
51# ---------
Gregory Haynes4b49e402016-08-31 18:19:51 -070052
53# Enable apache mod and restart apache if it isn't already enabled.
54function enable_apache_mod {
55 local mod=$1
Clark Boylanb9be9412025-03-26 10:09:38 -070056 local should_restart=$2
Gregory Haynes4b49e402016-08-31 18:19:51 -070057 # Apache installation, because we mark it NOPRIME
Clark Boylan35649ae2017-05-27 17:52:55 -070058 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 Boylanb9be9412025-03-26 10:09:38 -070063 if [[ "$should_restart" != "norestart" ]] ; then
64 restart_apache_server
65 fi
Clark Boylan35649ae2017-05-27 17:52:55 -070066 fi
Gregory Haynes4b49e402016-08-31 18:19:51 -070067 elif is_fedora; then
68 # pass
69 true
70 else
71 exit_distro_not_supported "apache enable mod"
72 fi
73}
74
Sean Dague604e5982017-04-13 13:28:12 -040075# 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.
78function install_apache_uwsgi {
79 local apxs="apxs2"
80 if is_fedora; then
81 apxs="apxs"
82 fi
83
Ian Wienand2d903562018-05-03 10:51:30 +100084 if is_ubuntu; then
Dr. Jens Harbott34800932020-02-13 09:38:35 +000085 local pkg_list="uwsgi uwsgi-plugin-python3 libapache2-mod-proxy-uwsgi"
Dr. Jens Harbott34800932020-02-13 09:38:35 +000086 install_package ${pkg_list}
Ian Wienand343e3512022-02-03 11:19:08 +110087 # 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
wangxiyuan6440c6d2022-08-05 14:18:13 +080092 elif is_fedora && ! is_openeuler && ! [[ $DISTRO =~ f36 ]]; then
Ian Wienand2d903562018-05-03 10:51:30 +100093 # 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 Ressic2c2b6b2020-06-15 12:48:38 +0200102 # Compile uwsgi from source.
Ian Wienand2d903562018-05-03 10:51:30 +1000103 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 Wienand2d903562018-05-03 10:51:30 +1000116 fi
Sean Dague604e5982017-04-13 13:28:12 -0400117
Martin Kopecec07b342023-01-24 17:38:45 +0100118 if is_ubuntu; then
Clark Boylanb9be9412025-03-26 10:09:38 -0700119 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 Dague604e5982017-04-13 13:28:12 -0400125 elif is_fedora; then
126 # redhat is missing a nice way to turn on/off modules
127 echo "LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so" \
128 | sudo tee /etc/httpd/conf.modules.d/02-proxy-uwsgi.conf
Clark Boylanb9be9412025-03-26 10:09:38 -0700129 restart_apache_server
Sean Dague604e5982017-04-13 13:28:12 -0400130 fi
Sean Dague604e5982017-04-13 13:28:12 -0400131}
132
zhang-hared98a5d02013-06-21 18:18:02 +0800133# install_apache_wsgi() - Install Apache server and wsgi module
Ian Wienandaee18c72014-02-21 15:35:08 +1100134function install_apache_wsgi {
zhang-hared98a5d02013-06-21 18:18:02 +0800135 # Apache installation, because we mark it NOPRIME
136 if is_ubuntu; then
137 # Install apache2, which is NOPRIME'd
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500138 install_package apache2
Jens Harbottd7a82f42020-06-23 10:21:09 +0200139 if is_package_installed libapache2-mod-wsgi; then
140 uninstall_package libapache2-mod-wsgi
Davanum Srinivasafa8a002016-12-19 09:51:01 -0500141 fi
Jens Harbottd7a82f42020-06-23 10:21:09 +0200142 install_package libapache2-mod-wsgi-py3
zhang-hared98a5d02013-06-21 18:18:02 +0800143 elif is_fedora; then
144 sudo rm -f /etc/httpd/conf.d/000-*
Hirotaka Wakabayashi1e265082020-07-02 06:19:21 +0000145 install_package httpd python3-mod_wsgi
Sean Mooney80c3ffe2023-03-27 20:56:20 +0000146 # rpm distros dont enable httpd by default so enable it to support reboots.
147 sudo systemctl enable httpd
Ian Wienand41e6e122017-08-08 15:06:26 +1000148 # For consistency with Ubuntu, switch to the worker mpm, as
Attila Fazekas9fd38e72017-12-11 12:20:25 +0100149 # the default is event
Ian Wienand41e6e122017-08-08 15:06:26 +1000150 sudo sed -i '/mod_mpm_prefork.so/s/^/#/g' /etc/httpd/conf.modules.d/00-mpm.conf
Attila Fazekas9fd38e72017-12-11 12:20:25 +0100151 sudo sed -i '/mod_mpm_event.so/s/^/#/g' /etc/httpd/conf.modules.d/00-mpm.conf
Ian Wienand41e6e122017-08-08 15:06:26 +1000152 sudo sed -i '/mod_mpm_worker.so/s/^#//g' /etc/httpd/conf.modules.d/00-mpm.conf
zhang-hared98a5d02013-06-21 18:18:02 +0800153 else
Gregory Haynes4b49e402016-08-31 18:19:51 -0700154 exit_distro_not_supported "apache wsgi installation"
zhang-hared98a5d02013-06-21 18:18:02 +0800155 fi
Gregory Haynes4b49e402016-08-31 18:19:51 -0700156 # WSGI isn't enabled by default, enable it
157 enable_apache_mod wsgi
Morgan Fainbergd074dc72014-06-24 21:33:39 -0700158}
159
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000160# apache_site_config_for() - The filename of the site's configuration file.
161# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
162#
Sean Dague8f8b2742017-04-13 09:34:12 -0400163# On Ubuntu 14.04+, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000164# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
165# files are 000-default.conf and default-ssl.conf.
166#
Martin Kopecec07b342023-01-24 17:38:45 +0100167# On Fedora, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled.
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000168#
169# On RHEL and CentOS, things should hopefully work as in Fedora.
170#
171# The table below summarizes what should happen on each distribution:
172# +----------------------+--------------------+--------------------------+--------------------------+
173# | Distribution | File name | Site enabling command | Site disabling command |
174# +----------------------+--------------------+--------------------------+--------------------------+
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000175# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
176# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
177# +----------------------+--------------------+--------------------------+--------------------------+
178function apache_site_config_for {
179 local site=$@
180 if is_ubuntu; then
Sean Dague8f8b2742017-04-13 09:34:12 -0400181 # Ubuntu 14.04 - Apache 2.4
182 echo $APACHE_CONF_DIR/${site}.conf
Martin Kopecec07b342023-01-24 17:38:45 +0100183 elif is_fedora; then
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000184 # fedora conf.d is only imported if it ends with .conf so this is approx the same
Dean Troyer444a8d52014-06-06 16:36:52 -0500185 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
Gabriel Assis Bezerraa688bc62014-05-27 20:58:22 +0000186 if [ -f $enabled_site_file ]; then
187 echo ${enabled_site_file}
188 else
189 echo ${enabled_site_file}.disabled
190 fi
191 fi
192}
193
Jamie Lennox54707012013-09-17 12:07:48 +1000194# enable_apache_site() - Enable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100195function enable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000196 local site=$@
Clark Boylan35649ae2017-05-27 17:52:55 -0700197 # Many of our sites use mod version. Just enable it.
198 enable_apache_mod version
Jamie Lennox54707012013-09-17 12:07:48 +1000199 if is_ubuntu; then
200 sudo a2ensite ${site}
Martin Kopecec07b342023-01-24 17:38:45 +0100201 elif is_fedora; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500202 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
203 # Do nothing if site already enabled or no site config exists
204 if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
205 sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
206 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000207 fi
208}
209
210# disable_apache_site() - Disable a particular apache site
Ian Wienandaee18c72014-02-21 15:35:08 +1100211function disable_apache_site {
Jamie Lennox54707012013-09-17 12:07:48 +1000212 local site=$@
213 if is_ubuntu; then
Chris Dent2fcdaac2017-04-18 16:54:12 +0100214 sudo a2dissite ${site} || true
Martin Kopecec07b342023-01-24 17:38:45 +0100215 elif is_fedora; then
Dean Troyer444a8d52014-06-06 16:36:52 -0500216 local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
217 # Do nothing if no site config exists
218 if [[ -f ${enabled_site_file} ]]; then
219 sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
220 fi
Jamie Lennox54707012013-09-17 12:07:48 +1000221 fi
222}
223
zhang-hared98a5d02013-06-21 18:18:02 +0800224# start_apache_server() - Start running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100225function start_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800226 start_service $APACHE_NAME
227}
228
229# stop_apache_server() - Stop running apache server
Ian Wienandaee18c72014-02-21 15:35:08 +1100230function stop_apache_server {
zhang-hared98a5d02013-06-21 18:18:02 +0800231 if [ -n "$APACHE_NAME" ]; then
232 stop_service $APACHE_NAME
233 else
234 exit_distro_not_supported "apache configuration"
235 fi
236}
237
238# restart_apache_server
Ian Wienandaee18c72014-02-21 15:35:08 +1100239function restart_apache_server {
Morgan Fainberg2df00462014-07-15 11:06:36 -0700240 # Apache can be slow to stop, doing an explicit stop, sleep, start helps
241 # to mitigate issues where apache will claim a port it's listening on is
242 # still in use and fail to start.
Sean Dague2b85cf02017-04-13 09:02:14 -0400243 restart_service $APACHE_NAME
zhang-hared98a5d02013-06-21 18:18:02 +0800244}
245
Stephen Finucaneb6613b12023-12-06 10:22:30 +0000246# write_uwsgi_config() - Create a new uWSGI config file
Sean Dague2f8c88e2017-04-13 09:08:39 -0400247function write_uwsgi_config {
Stephen Finucaned1263302023-12-06 09:58:18 +0000248 local conf=$1
Sean Dague2f8c88e2017-04-13 09:08:39 -0400249 local wsgi=$2
250 local url=$3
251 local http=$4
Stephen Finucaneb6613b12023-12-06 10:22:30 +0000252 local name=$5
253
254 if [ -z "$name" ]; then
255 name=$(basename $wsgi)
256 fi
rabiaa26baa2017-04-20 10:55:16 +0530257
258 # create a home for the sockets; note don't use /tmp -- apache has
259 # a private view of it on some platforms.
260 local socket_dir='/var/run/uwsgi'
Kirill Zaitsevd0db62a2017-05-26 19:02:52 +0300261
262 # /var/run will be empty on ubuntu after reboot, so we can use systemd-temptiles
263 # to automatically create $socket_dir.
264 sudo mkdir -p /etc/tmpfiles.d/
265 echo "d $socket_dir 0755 $STACK_USER root" | sudo tee /etc/tmpfiles.d/uwsgi.conf
266 sudo systemd-tmpfiles --create /etc/tmpfiles.d/uwsgi.conf
267
rabiaa26baa2017-04-20 10:55:16 +0530268 local socket="$socket_dir/${name}.socket"
Sean Dague2f8c88e2017-04-13 09:08:39 -0400269
270 # always cleanup given that we are using iniset here
Stephen Finucaned1263302023-12-06 09:58:18 +0000271 rm -rf $conf
Stephen Finucaneb6613b12023-12-06 10:22:30 +0000272 # Set either the module path or wsgi script path depending on what we've
273 # been given. Note that the regex isn't exhaustive - neither Python modules
274 # nor Python variables can start with a number - but it's "good enough"
275 if [[ "$wsgi" =~ ^[a-zA-Z0-9_.]+:[a-zA-Z0-9_]+$ ]]; then
276 iniset "$conf" uwsgi module "$wsgi"
277 else
278 deprecated 'Configuring uWSGI with a WSGI file is deprecated, use module paths instead'
279 iniset "$conf" uwsgi wsgi-file "$wsgi"
280 fi
Stephen Finucaned1263302023-12-06 09:58:18 +0000281 iniset "$conf" uwsgi processes $API_WORKERS
Sean Dague2f8c88e2017-04-13 09:08:39 -0400282 # This is running standalone
Stephen Finucaned1263302023-12-06 09:58:18 +0000283 iniset "$conf" uwsgi master true
Sean Dague2f8c88e2017-04-13 09:08:39 -0400284 # Set die-on-term & exit-on-reload so that uwsgi shuts down
Stephen Finucaned1263302023-12-06 09:58:18 +0000285 iniset "$conf" uwsgi die-on-term true
286 iniset "$conf" uwsgi exit-on-reload false
Matthew Treinish477a9622017-08-04 11:09:26 -0400287 # Set worker-reload-mercy so that worker will not exit till the time
288 # configured after graceful shutdown
Stephen Finucaned1263302023-12-06 09:58:18 +0000289 iniset "$conf" uwsgi worker-reload-mercy $WORKER_TIMEOUT
290 iniset "$conf" uwsgi enable-threads true
291 iniset "$conf" uwsgi plugins http,python3
Sean Dague2f8c88e2017-04-13 09:08:39 -0400292 # uwsgi recommends this to prevent thundering herd on accept.
Stephen Finucaned1263302023-12-06 09:58:18 +0000293 iniset "$conf" uwsgi thunder-lock true
Matthew Treinish477a9622017-08-04 11:09:26 -0400294 # Set hook to trigger graceful shutdown on SIGTERM
Stephen Finucaned1263302023-12-06 09:58:18 +0000295 iniset "$conf" uwsgi hook-master-start "unix_signal:15 gracefully_kill_them_all"
Sean Dague2f8c88e2017-04-13 09:08:39 -0400296 # Override the default size for headers from the 4k default.
Stephen Finucaned1263302023-12-06 09:58:18 +0000297 iniset "$conf" uwsgi buffer-size 65535
Sean Dague2f8c88e2017-04-13 09:08:39 -0400298 # Make sure the client doesn't try to re-use the connection.
Stephen Finucaned1263302023-12-06 09:58:18 +0000299 iniset "$conf" uwsgi add-header "Connection: close"
Sean Dague2f8c88e2017-04-13 09:08:39 -0400300 # This ensures that file descriptors aren't shared between processes.
Stephen Finucaned1263302023-12-06 09:58:18 +0000301 iniset "$conf" uwsgi lazy-apps true
Rodolfo Alonso Hernandez45123632024-11-29 07:39:37 +0000302 # Starting time of the WSGi server
303 iniset "$conf" uwsgi start-time %t
Sean Dague2f8c88e2017-04-13 09:08:39 -0400304
305 # If we said bind directly to http, then do that and don't start the apache proxy
306 if [[ -n "$http" ]]; then
Stephen Finucaned1263302023-12-06 09:58:18 +0000307 iniset "$conf" uwsgi http $http
Sean Dague2f8c88e2017-04-13 09:08:39 -0400308 else
309 local apache_conf=""
310 apache_conf=$(apache_site_config_for $name)
Stephen Finucaned1263302023-12-06 09:58:18 +0000311 iniset "$conf" uwsgi socket "$socket"
312 iniset "$conf" uwsgi chmod-socket 666
Dan Smithc3b0b902023-08-04 06:41:30 -0700313 echo "ProxyPass \"${url}\" \"unix:${socket}|uwsgi://uwsgi-uds-${name}\" retry=0 acquire=1 " | sudo tee -a $apache_conf
Sean Dague2f8c88e2017-04-13 09:08:39 -0400314 enable_apache_site $name
Ian Wienandf6a2d2c2017-04-26 10:50:29 +1000315 restart_apache_server
Sean Dague2f8c88e2017-04-13 09:08:39 -0400316 fi
317}
318
Matthew Treinish1fa65362017-06-23 22:32:37 +0000319# For services using chunked encoding, the only services known to use this
320# currently are Glance and Swift, we need to use an http proxy instead of
321# mod_proxy_uwsgi because the chunked encoding gets dropped. See:
Stephen Finucane497b4fd2024-10-18 13:47:55 +0100322# https://github.com/unbit/uwsgi/issues/1540.
Matthew Treinish1fa65362017-06-23 22:32:37 +0000323function write_local_uwsgi_http_config {
Stephen Finucaned1263302023-12-06 09:58:18 +0000324 local conf=$1
Matthew Treinish1fa65362017-06-23 22:32:37 +0000325 local wsgi=$2
326 local url=$3
Stephen Finucaneb6613b12023-12-06 10:22:30 +0000327 local name=$4
328
329 if [ -z "$name" ]; then
330 name=$(basename $wsgi)
331 fi
Matthew Treinish1fa65362017-06-23 22:32:37 +0000332
333 # create a home for the sockets; note don't use /tmp -- apache has
334 # a private view of it on some platforms.
335
336 # always cleanup given that we are using iniset here
Stephen Finucaned1263302023-12-06 09:58:18 +0000337 rm -rf $conf
Stephen Finucaneb6613b12023-12-06 10:22:30 +0000338 # Set either the module path or wsgi script path depending on what we've
339 # been given
340 if [[ "$wsgi" =~ ^[a-zA-Z0-9_.]+:[a-zA-Z0-9_]+$ ]]; then
341 iniset "$conf" uwsgi module "$wsgi"
342 else
343 deprecated 'Configuring uWSGI with a WSGI file is deprecated, use module paths instead'
344 iniset "$conf" uwsgi wsgi-file "$wsgi"
345 fi
Matthew Treinish1fa65362017-06-23 22:32:37 +0000346 port=$(get_random_port)
Stephen Finucaned1263302023-12-06 09:58:18 +0000347 iniset "$conf" uwsgi http-socket "$APACHE_LOCAL_HOST:$port"
348 iniset "$conf" uwsgi processes $API_WORKERS
Matthew Treinish1fa65362017-06-23 22:32:37 +0000349 # This is running standalone
Stephen Finucaned1263302023-12-06 09:58:18 +0000350 iniset "$conf" uwsgi master true
Matthew Treinish1fa65362017-06-23 22:32:37 +0000351 # Set die-on-term & exit-on-reload so that uwsgi shuts down
Stephen Finucaned1263302023-12-06 09:58:18 +0000352 iniset "$conf" uwsgi die-on-term true
353 iniset "$conf" uwsgi exit-on-reload false
Stephen Finucanea6f39012023-12-06 17:20:37 +0000354 # Set worker-reload-mercy so that worker will not exit till the time
355 # configured after graceful shutdown
356 iniset "$conf" uwsgi worker-reload-mercy $WORKER_TIMEOUT
Stephen Finucaned1263302023-12-06 09:58:18 +0000357 iniset "$conf" uwsgi enable-threads true
358 iniset "$conf" uwsgi plugins http,python3
Matthew Treinish1fa65362017-06-23 22:32:37 +0000359 # uwsgi recommends this to prevent thundering herd on accept.
Stephen Finucaned1263302023-12-06 09:58:18 +0000360 iniset "$conf" uwsgi thunder-lock true
Matthew Treinish477a9622017-08-04 11:09:26 -0400361 # Set hook to trigger graceful shutdown on SIGTERM
Stephen Finucaned1263302023-12-06 09:58:18 +0000362 iniset "$conf" uwsgi hook-master-start "unix_signal:15 gracefully_kill_them_all"
Matthew Treinish1fa65362017-06-23 22:32:37 +0000363 # Override the default size for headers from the 4k default.
Stephen Finucaned1263302023-12-06 09:58:18 +0000364 iniset "$conf" uwsgi buffer-size 65535
Matthew Treinish1fa65362017-06-23 22:32:37 +0000365 # Make sure the client doesn't try to re-use the connection.
Stephen Finucaned1263302023-12-06 09:58:18 +0000366 iniset "$conf" uwsgi add-header "Connection: close"
Matthew Treinish1fa65362017-06-23 22:32:37 +0000367 # This ensures that file descriptors aren't shared between processes.
Stephen Finucaned1263302023-12-06 09:58:18 +0000368 iniset "$conf" uwsgi lazy-apps true
369 iniset "$conf" uwsgi chmod-socket 666
370 iniset "$conf" uwsgi http-raw-body true
371 iniset "$conf" uwsgi http-chunked-input true
372 iniset "$conf" uwsgi http-auto-chunked true
373 iniset "$conf" uwsgi http-keepalive false
Matthew Treinishb79531a2017-06-30 12:10:06 -0400374 # Increase socket timeout for slow chunked uploads
Stephen Finucaned1263302023-12-06 09:58:18 +0000375 iniset "$conf" uwsgi socket-timeout 30
Rodolfo Alonso Hernandez45123632024-11-29 07:39:37 +0000376 # Starting time of the WSGi server
377 iniset "$conf" uwsgi start-time %t
Matthew Treinish1fa65362017-06-23 22:32:37 +0000378
379 enable_apache_mod proxy
380 enable_apache_mod proxy_http
381 local apache_conf=""
382 apache_conf=$(apache_site_config_for $name)
383 echo "KeepAlive Off" | sudo tee $apache_conf
Matthew Treinisha3488d52017-08-10 14:55:15 -0400384 echo "SetEnv proxy-sendchunked 1" | sudo tee -a $apache_conf
Dan Smithc3b0b902023-08-04 06:41:30 -0700385 echo "ProxyPass \"${url}\" \"http://$APACHE_LOCAL_HOST:$port\" retry=0 acquire=1 " | sudo tee -a $apache_conf
Matthew Treinish1fa65362017-06-23 22:32:37 +0000386 enable_apache_site $name
387 restart_apache_server
388}
389
Dan Smith09eea0b2020-07-09 08:31:51 -0700390# Write a straight-through proxy for a service that runs locally and just needs
391# to be reachable via the main http proxy at $loc
392function write_local_proxy_http_config {
393 local name=$1
394 local url=$2
395 local loc=$3
396 local apache_conf
397 apache_conf=$(apache_site_config_for $name)
398
399 enable_apache_mod proxy
400 enable_apache_mod proxy_http
401
402 echo "KeepAlive Off" | sudo tee $apache_conf
403 echo "SetEnv proxy-sendchunked 1" | sudo tee -a $apache_conf
Dan Smithc3b0b902023-08-04 06:41:30 -0700404 echo "ProxyPass \"${loc}\" \"$url\" retry=0 acquire=1 " | sudo tee -a $apache_conf
Dan Smith09eea0b2020-07-09 08:31:51 -0700405 enable_apache_site $name
406 restart_apache_server
407}
408
Sean Dague2f8c88e2017-04-13 09:08:39 -0400409function remove_uwsgi_config {
Stephen Finucaned1263302023-12-06 09:58:18 +0000410 local conf=$1
Sean Dague2f8c88e2017-04-13 09:08:39 -0400411 local wsgi=$2
412 local name=""
Stephen Finucaned5182ce2024-04-19 12:27:14 +0100413 # TODO(stephenfin): Remove this call when everyone is using module path
414 # configuration instead of file path configuration
Sean Dague2f8c88e2017-04-13 09:08:39 -0400415 name=$(basename $wsgi)
416
Stephen Finucaned5182ce2024-04-19 12:27:14 +0100417 if [[ "$wsgi" = /* ]]; then
418 deprecated "Passing a wsgi script to remove_uwsgi_config is deprecated, pass an application name instead"
419 fi
420
Stephen Finucaned1263302023-12-06 09:58:18 +0000421 rm -rf $conf
Sean Dague2f8c88e2017-04-13 09:08:39 -0400422 disable_apache_site $name
423}
424
zhang-hared98a5d02013-06-21 18:18:02 +0800425# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100426$_XTRACE_LIB_APACHE
zhang-hared98a5d02013-06-21 18:18:02 +0800427
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100428# Tell emacs to use shell-script-mode
429## Local variables:
430## mode: shell-script
431## End: