Setup using python apps in nginx
2016-01-30This is a quick summary how to run Python apps on a nginx webserver using Flask and uwsgi on Ubuntu 14.04.
Prerequisites
You should already have nginx running on your server. Additionally you need to install these packages:
apt-get install python-virtualenv uwsgi-plugin-python3
Application environment
For better separation of single services each app should run in its own Python environment. This allows to use different Python versions and different packages being installed for each app.
Create a new Python environment in the folder myApp/env
.
mkdir myApp cd myApp virtualenv -p python3 envOnce that is done you can install Flask and all other packages your app is going to use into that environment.
env/bin/activate pip install flask deactivateCopy your application files to the
myApp
folder.
Configure uwsgi
We'll start uwsgi by using an ini configuration file at myApp/myApp.ini
[uwsgi] module = %n callable = app chdir = %d virtualenv = %d/env plugin = python34 master = true processes = 1 threads = 2 uid = www-data gid = www-data socket = /tmp/uwsgi.%n.sock chown-socket = www-data:www-data chmod-socket = 660 vacuum = true die-on-term = true harakiri = 120You can try starting your application by calling
uwsgi --ini myApp/myApp.ini
In order to start it automatically a configuration file for upstart needs to be placed at
/etc/init/myApp.conf
description "My uwsgi application" start on runlevel [2345] stop on runlevel [06] respawn exec uwsgi --ini myApp/myApp.iniTo start it now type
start myApp
.
Configure nginx
Now that your Python app is running and accessible through uwsgi it needs to be assinged a URL in the webserver. A new location has to be configured inside the nginx configuration like this
location /myapp { include uwsgi_params; uwsgi_pass unix:/run/uwsgi.myApp.sock; }This will place your application at
your-server.com/myapp
.
Anbieterkennzeichnung
Datenschutzhinweis
This work is licensed under a
Creative Commons Attribution-ShareAlike 4.0 International License.