· sysadmin freebsd python nginx

Django, uWSGI, Nginx on Freebsd

Here are the steps I took for configuring Django on Freebsd using uWSGI and Nginx.

The data flow is like this:

Web Request ---> Nginx ---> uWSGI ---> Django

I was undecided for a while on whether to choose uWSGI or gunicorn. There are some blog posts discussing the pros and cons of each. I chose uWSGI in the end.

Also, to start uWSGI in freebsd, I found two methods: using supervisord, or using a custom freebsd init script which could use uWSGI ini files. Currently using supervisord.

Install Packages Required

$ sudo pkg install python py27-virtualenv nginx uwsgi py27-supervisor

Also install any database package(s) required.

Setup your Django project

Choose a folder for setting up your Django project sources. /usr/local/www/myapp is suggested. Clone the sources to this folder, then setup the python virtual environment.

$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt

If required, also setup the database and run the migrations.

Setup uWSGI using supervisord

Setup the supervisord file at /usr/local/etc/supervisord.conf.

Sample supervisord.conf:

And start it:

$ echo supervisord_enable="YES" >> /etc/rc.conf
$ sudo service supervisord start
$ sudo supervisorctl tail -f uwsgi_myapp:uwsgi_myapp0

Setup Nginx

Use the following line in nginx.conf’s http section to include all config files from conf.d folder.

include /usr/local/etc/nginx/conf.d/*.conf;

Create a myapp.conf in conf.d.

Sample myapp.conf:

And start Nginx:

$ echo nginx_enable="YES" >> /etc/rc.conf
$ sudo service nginx start
$ sudo tail -f /var/log/nginx-error.log

Accessing http://myapp.example.com/ should work correctly after this. If not, see the supervisord and Nginx logs opened and correct the errors.