... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

I’m building production environment for my django apps

October 21, 2009 | frameworks, linux, other, python
author: Karol Zielinski | comments: 4 | views: 2542
Tags: , , , , , ,

I’m building production environment on my VPS for all of my django applications. What I need is: Django framework, Apache (with mod_wsgi) and nginx. I will do it on ubuntu 9.04.

Let’s fun.

Install nginx and apache with mod_wsgi

I assume that you have nginx installed. If not:

sudo apt-get install nginx

and now…
you need to install apache with all necessary modules.

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

now… we need to add mod_wsgi to our apache.

sudo aptitude install libapache2-mod-wsgi

and restart it:

sudo /etc/init.d/apache2 restart

Configure apache

sudo vim /etc/apache2/apache2.conf

change ‘Timeout’ variable to some lower value… for example 30 or 90.
change ‘KeepAlive’ variable to ‘Off’.
close the file.

sudo vim /etc/apache2/ports.conf

change ‘NameVirtualHost’ and ‘Listen’ to ’127.0.0.1:80′ so the first two uncommented lines should look like:


NameVirtualHost 127.0.0.1:80
Listen 127.0.0.1:80

close the file.

sudo /etc/init.d/apache2 restart

don’t care about the warnings.

Run django app

I assume that you have django installed. If not:

sudo apt-get install python-django

or you can always install latest (official/development) version.

I assume that you have some django project created – if not… create some.

Let’s configure all these things.

My project is under ‘/opt/django-projects/’ path.

add site.com to apache

sudo vim /etc/apache2/sites-available/site.com

and add there:


<VirtualHost *:80>
        ServerName site.com
        ServerAlias www.site.com
        WSGIScriptAlias / /opt/django-projects/site/site.wsgi
</VirtualHost>

<VirtualHost 127.0.0.1:80>
    ServerName www.site.com
    ServerAlias site.com

    <Directory /opt/django-projects/site/>
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn
    ErrorLog  /var/log/site-apache_error.log
    CustomLog /var/log/site-apache_access.log combined

    WSGIDaemonProcess site.com user=www-data group=www-data threads=25
    WSGIProcessGroup site.com
    WSGIScriptAlias / /opt/django-projects/site/site.wsgi
</VirtualHost>

now… we should create our ‘site.wsgi’ file.

vim /opt/django-projects/site/site.wsgi

and add there:


import os
import sys

sys.path.append('/opt/django-projects/')

os.environ['DJANGO_SETTINGS_MODULE'] = 'site.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

now:

a2ensite site.com

or just:

ln -s /etc/apache2/sites-available/site.com /etc/apache2/sites-enabled/

and:

sudo /etc/init.d/apache2 reload

apache is ready.

add site.com to nginx

sudo vim /etc/nginx/sites-available/site.com

and add there:


server {
        listen OUR_IP:80;
        server_name site.com;
        rewrite ^(.*) http://www.site.com permanent;
}

server {
    listen OUR_IP:80;
    server_name www.site.com;

    access_log /var/log/site-nginx-access.log;
    error_log /var/log/site-nginx-error.log;

    location / {
        proxy_pass    http://127.0.0.1:80/;
        include       /etc/nginx/proxy.conf;
    }

    location /media/ {
            root   /opt/django-projects/site/site_media/;
            expires 1d;
    }
}

now have a look at ‘/etc/nginx/proxy.conf’. This file should looks like:


# proxy.conf
proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   110;
proxy_send_timeout      110;
proxy_read_timeout      110;
proxy_buffers           32 4k;

now:

sudo ln -s /etc/nginx/sites-available/site.com /etc/nginx/sites-enabled/site.com

and:

sudo /etc/init.d/nginx restart

The end.
Check site.com in your web browser.

Bookmark and Share
Post I’m building production environment for my django apps to develway Post I’m building production environment for my django apps to Delicious Post I’m building production environment for my django apps to Digg Post I’m building production environment for my django apps to Facebook Post I’m building production environment for my django apps to Reddit Post I’m building production environment for my django apps to StumbleUpon

Related news and resources

Comments (4)

4Avatars v0.3.1 v0.3.1
Bryan Goines
October 21, 2009, 2:29 pm

Great tutorial.

Apache + mod_wsgi is not really required.

If you are looking for better performance and less resource hogs.

Just run nginx + upstream (balancer) with python’s wsgi server (cherrypy, tornado, etc ..) to run your django apps.

Bryan

4Avatars v0.3.1 v0.3.1
I want to serve php via Php-fpm on my ubuntu
October 23, 2009, 4:52 am

[...] plan is to have one server for my python and php applications. I already wrote about configuring nginx + apache for django applications. As well as configuring WordPress on nginx + lighttpd + FastCGI + php (via spawn-fcgi). Today I [...]

4Avatars v0.3.1 v0.3.1
Subversion server, with web access on ubuntu
October 29, 2009, 9:25 am

[...] I assume that you have apache installed. [...]

4Avatars v0.3.1 v0.3.1
FreeMind
December 7, 2009, 11:12 pm

I am setting up a production environment for private local network, will this work ?

Write a comment

Karol Zielinski :: Just a tech stuff Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati