Digital Media Insights for Business Owners

Setting up a Reverse Proxy in Apache to serve a Python server like Uvicorn

Reverse

This is a simple step by step process to setup an Apache server as a reverse proxy for a unicorn server that serves python code. Here we go:

  1. Define your vHost. You have to let Apache know about your reverse proxy needs. In essence you are going to tell it what is the virtual host and where it can find its contents. For that, you may modify your /etc/apache2/conf.d/includes/pre_main_global.conf file with the definition of the virtual host as below:
				
					<VirtualHost *:80>
        ServerAdmin username@website.com
        ServerName sub.website.com
        ErrorLog /location/error.log
        CustomLog /location/access.log combined

        ProxyTimeout 60
        ProxyRequests On
        ProxyPass / http://127.0.0.1:5000
        ProxyPassReverse / http://127.0.0.1:5000
</VirtualHost>
				
			
				
					/etc/apache2/conf.d/userdata/ssl/2_4/{username}/{subdomain.domain.com}/proxy.conf
				
			

In that newly created file, define the vhost:

				
					# Example uvicorn on port 5000
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
				
			

Now create the service under systemd, giving it a name that makes sense to you:
/etc/systemd/system/myuvicorn.service

Now create the service under systemd, giving it a name that makes sense to you:
/etc/systemd/system/myuvicorn.service

The contents of the file are as below for a uvicorn server:

				
					[Unit]
Description=Uvicorn ASGI server
After=network.target

[Service]
User={youruser}
Group={yourgroup}
WorkingDirectory=/home/{username}/sub.website.com
ExecStart=/usr/bin/env uvicorn app:app --host 127.0.0.1 --port 5000 --workers 2
Restart=always
RestartSec=25
KillMode=mixed
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target
				
			

Now it is time to load the service:

				
					sudo systemctl daemon-reload # Reload systemd, scanning for new or changed units
sudo systemctl enable myuvicorn.service # Enable the uvicorn service to start at boot
sudo systemctl start myuvicorn.service # Start the service now
				
			

Finally, let’s stop it and restart it and also view logs in case we want to see what is happening:

				
					systemctl stop myuvicorn.service; # stops the service
systemctl enable --now myuvicorn.service; # enables the service
systemctl status myuvicorn.service -l # show me the status

journalctl -u myuvicorn.service -f # Show me logs as they happen
				
			

Hope this is helpful.

Say: "Hola" to your new clients.

Follow us on Social Media for more Tips & Tricks.

Other Posts You May Enjoy