Vaka Vishnu Vardhan Gowd
3 min readJun 10, 2024

Deploy Django/Flask/FastAPI app on Ubuntu using Nginx & Gunicorn.

Want to host your python backend application on ubuntu to its public URL ! then you are at a right place. Get exicited !

Prerequisite: Have a working version of app, make a requirements.txt file.

Step 1: Update the Ubuntu system and install all the required Python packages from the requirements.txt file.

sudo apt update
pip install -r requirements.txt

#make sure you app is configured to a certain PORT and host to listen on all public ip’s available with debugging turned off.

app runing on port 8000 and 0.0.0.0 tells os to listen on all public IPs available.

Now confirm that app is running without any errors.

$ python3 app.py

Step 2: Start the app with Gunicorn to ensure it runs continuously on the local server.

Install gunicorn3 on your machine and start the app.py via gunicorn.

$ sudo apt install gunicorn3
$ gunicorn3 --workers=3 app:app --daemon

Now check if the application is running on 127.0.0.1:8000 using curl. You should be able to connect and receive data.

curl 127.0.0.1:8000

Your done with the gunicorn part and succefully set the application running continuously in local. Now let’s move on to the Nginx where you will set a proxy server using it.

Step 3: Installing Nginx and configuring it for our locally running application.

$ sudo apt install nginx
$ sudo vim /etc/nginx/sites-enabled/app.conf

Now, a new file app.conf will open in edit mode at path /etc/nginx/sites-enabled where we will be setting our server configurations. Follow the below app.conf file:

server {
listen 5000;
server_name YOUR_MACHINE_PUBLIC_IP_ADDRESS/DOMAIN;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/etc/nginx/sites-enabled/app.conf listeing on port 5000.

note: unlink the default nginx config if any.
$ sudo unlink /etc/nginx/sites-enabled/default
And our application is listening on port 5000.

Step 5: Check the nginx configuration file (app.conf) syntax.

$ sudo nginx -t

If the configuration is correct, you should see a message indicating ‘syntax is ok’.

Step 6: Start the Nginx server and check its running status.

$ sudo nginx -s reload
or
$ sudo systemctl start nginx

$ sudo systemctl status nginx

you will see a message like this:

nginx running active

Check for the application running on the public url by loading it on web or command : “curl public_ipaddress” and you’ll see the page data.

if you got any problems with ports or starting the nginx, check if you allowed the incoming traffic on port 5000.

$ sudo ufw allow 5000
$ sudo ufw allow 5000/tcp #allows the port 5000 for incoming tcp traffic.

Now restart the nginx: “sudo systemctl restart nginx” and it should work like charm!

Congratulations on successfully deploying your application using Nginx on Ubuntu!

If you found this helpful, make sure to “follow me” for more comprehensive guides and tips.

Stay tuned for upcoming articles that will help you master the technologies and skills needed to excel in your projects!

No responses yet