HomeBack-End & DatabaseDeploy Python App On Server

Deploy Python App On Server

In this blog post we will see how to deploy your python web app on any dedicated server like VPS, Amazon EC2, Google Cloud GCP etc basically any server where you have shell access and root access.

On a server, first login via SSH shell for different server types like aws, gcp there are different ways to do it which you can find online easily.

Next install python3 if it’s not installed already.

Next go a git clone of your app and running using venv, flask run etc. All standard stuff which you have been doing till now already.

This way you can first test if your app is working fine or not.

There are plenty of blogs online available to do the above so no point in writing it again, the only main thing for AWS, GCP is you need to have an account. Both these cloud provides have 1yr free trail so it can be used easily for development purposes.

Also another thing to remember, is that since our flask/python app runs on ports like 5000, 8000 its important we open the ports via firewall. By default all ports are blocked on server. Again follow guides online on how to open these ports depending on your server provider.

Next, we need to install gunicorn which we read about in a previous blog

I won’t write about gunicorn, since have already explain about it.

The important thing that comes up with gunicorn on a server is that its not a background process. As soon you close the terminal on server, the process will close hence your web api’s will stop working. We want is so that the web api’s keep on running even after we close the terminal.

For this we will use a python lib called “supervisor”. This will start, stop our web app, even after we close the terminal. Here are different blogs on how to do this

https://www.agiliq.com/blog/2014/05/supervisor-with-django-and-gunicorn/#using-supervisor

Again, supervisor is a very well written thing so no point in going into details about it. I will simply provide good references. I found supervisor to be best and easy but there are many other options as well.

After above is done, your app should be running on port 8000 (default) even after you close your terminal.

Basically what “supervisor” does is run “gunicorn” in background and automatically restart it if it crashes.

We should be good at this stage. But most production app’s run on domains like api.etech.com and also https ssl certificate.

For this we need a server like nginx.

So you need to nginx using apt-get on your server, again plenty of blogs available. Next, do a configuration like this

server {
    listen 80;
    server_name api.etech.org;
    access_log  /var/log/nginx/example.log;

    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;
    }
  }

This basically routes all traffic on api.etech.org to port 8000

In case case of https you need to setup SSL certificate first and then do the same routing of traffic to port 8000

Above blog is mainly an outline of how you can go about setting up a production app on server and the steps to follow.

Here is a blog which shows the above process in detail
http://alexandersimoes.com/hints/2015/10/28/deploying-flask-with-nginx-gunicorn-supervisor-virtualenv-on-ubuntu.html

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: