Created
May 7, 2012 04:39
-
-
Save simoncoulton/2625954 to your computer and use it in GitHub Desktop.
Setting up Nginx, uWSGI & Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
====================================== | |
Setting up Nginx, uWSGI and Python3 | |
====================================== | |
First off, I'm traditionally a PHP developer, but am looking at moving across to Python. I really struggled to find decent documentation on how to get a server up and running for deploying Python web applications from the point of view of someone coming from PHP. The main problems I came across with documentation were: | |
1) Only showed you how to run the server for a single web application. | |
2) Only showed you how to configure the app, not the server it was running on. | |
My preferred workflow for development is by setting up a new VM in VMware Fusion and then forwarding through all requests to that VM via /etc/hosts. This might not be the optimal way to get things up and running, but it works for me. | |
SITE_URL refers to the primary domain for the site. | |
SITE_DIR refers to the location on disk that the site is located. | |
Typical folder structure of an app looks something like: | |
root | |
app # site code goes in here | |
config | |
data | |
cache | |
log | |
libs | |
vendors # frameworks and 3rd party libraries go in here | |
public | |
css | |
img | |
js | |
--------------------------------------- | |
Installing required tools | |
--------------------------------------- | |
We'll start by installing the required tools via apt-get. | |
apt-get install uwsgi uwsgi-plugin-python3 nginx-full python-setuptools python-pip | |
--------------------------------------- | |
Creating the uWSGI upstart | |
--------------------------------------- | |
Next we need to create the upstart that will be used start the uWSGI service. | |
vi /etc/init/uwsgi.conf | |
# uWSGI - Manage uWSGI Application Server | |
description "uWSGI Emperor Mode" | |
start on (filesystem and net-device-up IFACE=lo) | |
stop on runlevel [!2345] | |
respawn | |
exec /usr/bin/uwsgi --emperor /etc/uwsgi/vassals/sites-enabled/ --logto /var/log/uwsgi.log | |
initctl reload-configuration | |
update-alternatives --set uwsgi /usr/bin/uwsgi_python32 | |
--------------------------------------- | |
Creating the site configuration file for nginx | |
--------------------------------------- | |
Create the configuration file for the site in the sites-available folder: | |
vi /etc/nginx/sites-available/SITE_URL | |
And then use the following to configure the site: | |
upstream wsgicluster { | |
server unix://tmp/SITE_URL.sock | |
} | |
server { | |
listen 80; | |
server_name SITE_URL; | |
error_log SITE_DIR/data/log/error.log; | |
access_log SITE_DIR/data/log/access.log; | |
location / { | |
include uwsgi_params; | |
uwsgi_pass wsgicluster; | |
} | |
location -^/(img|js|css)/ { | |
root SITE_DIR/public; | |
expires 30d; | |
} | |
location = /favicon.ico { | |
log_not_found off; | |
} | |
} | |
Then finally we link the sites-available configuration file to the sites-enabled: | |
ln -s /etc/nginx/sites-available/SITE_URL /etc/nginx/sites-enabled/SITE_URL | |
--------------------------------------- | |
Creating the vassal for uWSGI emperor | |
--------------------------------------- | |
Create the folders for sites-available and sites-enabled: | |
mkdir /etc/uwsgi/sites-available | |
mkdir /etc/uwsgi/sites-enabled | |
Create the configuration vassal file: | |
vi /etc/uwsgi/sites-available/SITE_URL.yml | |
And then use the following contents (you can tweak these settings): | |
uwsgi: | |
master: true | |
processes: 1 | |
vaccum: true | |
chmod-socket: 666 | |
uid: www-data | |
gid: www-data | |
plugins: python32 | |
socket: /tmp/SITE_URL.sock | |
chdir: SITE_DIR | |
pythonpath: SITE_DIR | |
module: application | |
touch-reload: SITE_DIR/application.py | |
Finally restart the services: | |
service uwsgi start | |
service nginx start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have my automation tool in python2.7 now. It is deployed with django1.11+nginx+wsgi. When I tried migrating to python3, it fails. I installed everything w.r.t. python3, created a new virtualenv, but I get "Internal Server Error".