Skip to content

Instantly share code, notes, and snippets.

@sebbekarlsson
Last active June 25, 2021 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sebbekarlsson/619acf8502326ad6d89d4eb20b6271c0 to your computer and use it in GitHub Desktop.
Save sebbekarlsson/619acf8502326ad6d89d4eb20b6271c0 to your computer and use it in GitHub Desktop.
Creating a simple web application in Python and running it under Nginx

Python web application with Nginx

Let us create a simple API in Python and serving it using Nginx.

In this example we will use Debian 9 as a distro for the server.

The code

Alright, let's write some code.
We will create an API that will serve an array of fruits.

Let's create a __main__.py file:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api/fruits')
def fruits():
    return jsonify([
        'apple',
        'banana',
        'pear',
        'orange'
    ])

if __name__ == '__main__':
    app.run(debug=True)

Save the file.
You can now start the application right away by executing:

python __main__.py

The application should now be available at: http://localhost:5000

Configuring the sever

We need to configure the sever to be able to run the application.

Installing requirements

apt-get install python-pip python-virtualenv nginx uwsgi uwsgi-plugin-python

Remove the default nginx configuration

rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default

Making the app compatible with nginx

To make it compatible with Nginx, you will need these three files:

  • yourapp.ini - uwsgi config
  • yourapp.service - systemd service
  • yourapp.nginx - nginx configuration

yourapp.ini

[uwsgi]
wsgi-file = __main__.py
virtualenv = venv
plugin = python
callable = app
master = true
processes = 5
socket = /var/run/yourapp.sock
chmod-socket = 660
vacuum = true
logto = /var/log/yourapp.log
die-on-term = true

yourapp.service

[Unit]
Description=uWSGI instance to serve yourapp

[Service]
User=someuser
ExecStart=/bin/bash -c 'cd /path/to/yourapp; uwsgi --ini yourapp.ini 2>&1 >> /tmp/yourapp.log'

[Install]
WantedBy=multi-user.target

yourapp.nginx

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name yourapp.com;
    client_max_body_size 300M;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/run/yourapp.sock;
    }
}

Place yourapp.ini next to your __main__.py file and place yourapp.service in /etc/systemd/system/.
yourapp.nginx should be placed in /etc/nginx-sites-available/.. You will also need to symlink the nginx file like this:

ln -s /etc/nginx/sites-available/yourapp.nginx /etc/nginx/sites-enabled/.

Configuring the environment for the application

A good practice is to run the application from a python-virtualenv.

1 - create the virtualenv

cd /path/to/yourapp
virtualenv -p /usr/bin/python2.7 ./venv
source ./venv/bin/activate

2 - install the requirements

In our case, all we need is flask:

pip install flask

Wrapping it all up and starting the application

Alright, we are ready to start everything.

systemctl restart nginx
systemctl start yourapp.service

Errors

To see errors, please look at these log files:

cat /var/log/nginx/error.log
cat /var/log/yourapp.log
journalctl -u yourapp.service

Common errors:

  • wrong permissions on /var/run/yourapp.sock
  • no runnable app found in application
  • lack of dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment