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 someblogposts 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
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.
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:
supervisord.conf
And start it:
Setup Nginx
Use the following line in nginx.conf's http section to include all config files from conf.d folder.
Create a myapp.conf in conf.d.
Sample myapp.conf:
myapp.conf
And start Nginx:
Accessing http://myapp.example.com/ should work correctly after this. If not, see the supervisord and Nginx logs opened and correct the errors.
Interactions
Brood
Ideas why nginx gives a Bad Gatway 502 error ? Log say Connection refused) while connecting to upstream
After a bit of tracking down, the best way to run as another user is to create a subdirectory in /var/run, owned by the appropriate user, and store the sockets in there.
E.g. if you want to use user www:www (id 80:80), do:
Interactions
Ideas why nginx gives a Bad Gatway 502 error ? Log say Connection refused) while connecting to upstream
Ideas what can be wrong?
Check if the supervisor service is running, and if your uwsgi program is running correctly. You can check the uwsgi error log as well.
I think the supervisord config script you posted runs the uwsgi instance as root, which is presumably a bad idea.
After a bit of tracking down, the best way to run as another user is to create a subdirectory in
/var/run
, owned by the appropriate user, and store the sockets in there.E.g. if you want to use user
www:www
(id80:80
), do:Then in
supervisord.conf
have:and in
nginx.conf
:You might want to update the example to show this?
Makes sense. I'll test and update it.
Thanks.