Running Django with Postgres, Nginx, and FastCGI on Ubuntu 10.4
Last week, I decided that I needed to virtualize my Django test environment, and get it off my MacBook. I plan on upgrading at some point in the future, so not having to go through all the steps to set up the environment again would be nice. Plus, getting Django running on the MacBook was a bit complicated, and I’d like to avoid that. So, with the goal of “easy installation” in mind, I started working on a new VM.
*Update*:
A friend and co-worker, Brandon Konkle, just posted a great article using the basics I listed below, but adding Green Unicorn instead of FastCGI, and with virtualenv to manage dependencies. Great read.
The Stack
Starting off, I decided on a few main goals for my VM:
- It would run on Ubuntu, because that’s the Linux distro I’m most familiar with.
- The web server would need to have a low memory footprint.
- It shouldn’t run MySQL, as some of the Django muckity-mucks recommend against it.
- Ubuntu Server 10.4 x64
- Nginx
- PostgreSQL 8
- Django 1.2
Now, some of this stuff (Ubuntu and Django) are versions that have very recently been released. If you aren’t comfortable running the latest revisions (or if your app has specific needs for older versions), pick those instead.
The Problem
Unfortunately, I couldn’t find a set of instructions detailing exactly what I want. Plenty of good posts on running Django on Apache, or using MySQL, but nothing for the stack I had decided on. So, I culled what I could from various posts, and decided to aggregate the steps here.
Assumptions
This post assumes that you have some knowledge of how to get Ubuntu up and running. If you need additional information for that, I recommend checking out the Slice Host Articles. They’re a top-notch resource for all kinds of info.
Note: If you’re on a MacBook, you’ll need to select the “Apple Laptop” keyboard during Ubuntu setup. If you don’t do this, your keys may act funky. To reconfigure, you can run:
sudo dpkg-reconfigure console-setup
Install Nginx
Once you’ve got Ubuntu up and running, it’s time to get Nginx going. I’m just going to link to the slicehost articles for this, because, frankly, they’ve already gone through the trouble on this part.
I don’t do the last step, because I’m building this on a VM, so instead I just use Nginx’s default site configuration file. You should use whichever option is better for your environment.
Install PostgreSQL
Luckily, Ubuntu makes it pretty easy to get PostgreSQL installed.
sudo apt-get install postgresql pgadmin3 python-psycopg2
This will install PosgresSQL, PG Admin III, and PsycoPG (which you need for Python to talk to the database).
You’ll also want to setup a database user account.
sudo su - passwd postgres su postgres psql template1
The last command will open a PosgreSQL command line, where you can type the following, making the necessary substitutions:
ALTER USER postgres WITH ENCRYPTED PASSWORD 'mypassword';
Then exit PSQL.
Install Django
To install Django, it’s easiest to install subversion first, and then clone the repository.
sudo apt-get install subversion svn co http://code.djangoproject.com/svn/django/trunk django_trunk sudo python django_trunk/setup.py install
Once Django is installed, you can verify it by opening up your Python console:
python import django print django.VERSION import psycopg2 psycopg2.apilevel exit()
Install flup
We also need to install flup, so that FastCGI can pass the Django requests from Nginx to the Django server.
sudo wget http://www.saddi.com/software/flup/dist/flup-1.0.2-py2.5.egg sudo easy_install flup-1.0.2-py2.5.egg
Build Your Test Project
Now that everything is installed, it’s time to make sure it worked. Build out an empty test project.
cd ~/public_html/testproject/private django-admin.py startproject testproject cd testproject python manage.py startapp myapp nano settings.py
You’ll then need to enter some database information.
DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2' DATABASE_NAME = 'testproject' DATABASE_USER = 'postgres' DATABASE_PASSWORD = 'mypasswd' DATABASE_HOST = '' DATABASE_PORT = ''
Don’t worry about setting up the media directories and such, as we just want the default “Welcome to Django” page for the time being.
Nginx Configuration
Now that the backend stuff is good to go, it’s time to get your Nginx configuration taken care of. If you installed Nginx via aptitude, you’ll want to stop the server.
sudo /etc/init.d/nginx stop
Since I’m just using this as a test bed, accessed locally, I made my changes in Nginx’s default site configuration. Feel free to add another virtual host if you need to.
sudo nano /etc/nginx/sites-available/default
Once you’re in the configuration file, change it to this:
server
{
listen 80;
server_name localhost;
access_log /home/username/public_html/testproject/log/localhost.access.log;
error_log /home/username/public_html/testproject/log/localhost.error.log;
root /home/username/public_html/testproject/public;
location /site_media
{
root /home/username/public_html/testproject/public;
}
location /
{
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8081;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
This will ensure that all requests for dynamic content (read: Django) get passed to Django, and all static content (images, stylesheets, scripts, etc.) just go through Nginx as normal. You don’t have to use port 8081 here, but take note of whichever port you do decide on, as we’ll need it next.
Run Django as a FastCGI process
The last thing we need to do is get Django set up to run as a FastCGI process.
cd ~/public_html/testproject/ python manage.py runfcgi host=127.0.0.1 port=8081 --settings=settings
Also, don’t forget to restart Nginx.
sudo /etc/init.d/nginx start
The End
And that’s it! You should now be able to head over to the VM’s IP address, and get the “Welcome to Django - Get Working!” page. If not, you should review the steps I’ve placed here, and see if you missed anything. There’s also the possibility that I missed something, in which case, feel free to flog me in the comments below.
Gotta Give Respect
Respect needs to be paid to David McLaughlin and Antonio Cangiano, both of whom produced a lot of the information that I’ve repurposed here. You should read their guides if you have any additional questions (David goes into FTP servers and getting your Admin media synced, for instance).
-
howardtharp liked this
-
bkonkle liked this
-
kevin-whitaker posted this