Digital Media Insights for Business Owners

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


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>
        ErrorLog /location/error.log
        CustomLog /location/access.log combined

        ProxyTimeout 60
        ProxyRequests On
        ProxyPass /
        ProxyPassReverse /

In that newly created file, define the vhost:

					# Example uvicorn on port 5000
ProxyPass /
ProxyPassReverse /

Now create the service under systemd, giving it a name that makes sense to you:

Now create the service under systemd, giving it a name that makes sense to you:

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

Description=Uvicorn ASGI server

ExecStart=/usr/bin/env uvicorn app:app --host --port 5000 --workers 2


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