I’m building production environment for my django apps
October 21, 2009 | frameworks, linux, other, pythonauthor: Karol Zielinski | comments: 4 | views: 2542
Tags: apache, django, environment, mod_wsgi, nginx, production, wsgi
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.
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
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.
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.
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.
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
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 [...]
October 29, 2009, 9:25 am
[...] I assume that you have apache installed. [...]
December 7, 2009, 11:12 pm
I am setting up a production environment for private local network, will this work ?