Setup nginx and uwsgi with python3 on ubuntu 14.04

First, install the needed components:

sudo apt-get install nginx uwsgi uwsgi-plugin-python3

Create nginx config for the new website:

sudo nano /etc/nginx/sites-available/mysite.com

Add the following, change atleast server_name and root lines to match your setup:

server {
    server_name mysite.com;
    error_log  /var/log/nginx/mysite.com.error.log;
    access_log         /var/log/nginx/mysite.com.access.log;
    root /home/youruser/web/mysite.com;
    location / {
        uwsgi_pass unix:/var/run/uwsgi/app/myapp/socket;
        include uwsgi_params;
    }
}

Save file and close your editor.

Make nginx use the new site:

sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/mysite.com

Add uwsgi config for new site:

sudo nano /etc/uwsgi/apps-available/mysite.ini
[uwsgi]
uid=youruser
gid=youruser
# socket line should match uwsgi_pass line in your nginx config
socket = /var/run/uwsgi/app/myapp/socket
chown-socket = www-data
chdir = /home/youruser/web/mysite.com
# Directory you can create to serve static files in the project, or just omit.
check-static=/home/youruser/web/mysite.com/static
# This is the main python file in your project
file = webpage.py

Make uwsgi use the new config:
sudo ln -s /etc/uwsgi/apps-available/mysite.ini /etc/uwsgi/apps-enabled/mysite.ini

Reload config:

sudo service uwsgi restart
sudo service nginx reload

If you do not already have a wsgi-compatible python web project to use with this setup, here is a small guide to get you started:

cd /home/youruser/web/mysite.com

For this example I use bottle.py

wget http://bottlepy.org/bottle.py
nano webpage.py

Add the following:

from bottle import route, run, template, default_app

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

if __name__ == "__main__":
    run(host='localhost', port=8080)
else:
    application = default_app()

You can now test your app at your_domain/hello/name

Comments are closed.