Wednesday, October 29, 2008

Django From the Ground Up: Episode 13

with host Eric Florenzano

Bookmark and Share

In this episode we take our server that we started in Episode 1, and we install everything necessary to deploy our site. That includes Apache, nginx, Django, some reusable apps, and PostgreSQL.

One thing to note is that you don't technically need to have a DNS pointed to your server. You can fake it by adding these directives to /etc/hosts:

127.0.0.1    media.startthedark.com
127.0.0.1    startthedark.com
127.0.0.1    www.startthedark.com

Of course, if your server is not running locally, you'll have to replace the IP address with your server's IP address. Make sure to undo these changes when you're finished following along.

Here's a rundown of the commands that I used in the episode, for those who prefer to copy and paste:

Installing the base components:

sudo apt-get install apache2-mpm-prefork libapache2-mod-wsgi nginx memcached postgresql-8.3 subversion python-setuptools python-psycopg2 python-memcached

Adding our new user:

sudo adduser --home=/var/www/startthedark.com
sudo su startthedark

Setting up our source code repositories:

sudo mkdir /git
sudo chown startthedark:startthedark -R /git
mv /home/ericflo/startthedark /git/startthedark
sudo mkdir /python
sudo chown startthedark:startthedark -R /python
cd /python
svn checkout http://django-pagination.googlecode.com/svn/trunk/ django-pagination
cd django-pagination
sudo pylink pagination
cd ..
svn checkout http://django-registration.googlecode.com/svn/trunk/ django-registration
cd django-registration
sudo pylink registration
cd ..
svn checkout http://django-gravatar.googlecode.com/svn/trunk/ django-gravatar
cd django-gravatar
sudo pylink gravatar
svn checkout http://code.djangoproject.com/svn/django/trunk/ django-trunk
cd django-trunk
sudo pylink django
sudo easy_install dateutil

Chcking out our own source code:

cd /var/www/startthedark.com
git clone /git/starthedark

Setting up Apache:

cd /etc/apache2/sites-available
nano startthedark.com

<VirtualHost * >
        #Basic setup
        ServerAdmin floguy@gmail.com
        ServerName www.startthedark.com
        ServerAlias media.startthedark.com
        ServerAlias startthedark.com
        DocumentRoot /var/www/startthedark.com/startthedark/media

        WSGIScriptAlias / /var/www/startthedark.com/startthedark/startthedark.wsgi
</VirtualHost>

sudo a2ensite startthedark.com
sudo /etc/init.d/apache2 reload

Creating our PostgreSQL database:

sudo su postgres
createuser startthedark
createdb -O startthedark startthedark
exit

Setting up nginx:

cd /etc/nginx
nano 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       90;
proxy_send_timeout          90;
proxy_read_timeout          90;
proxy_buffer_size           4k;
proxy_buffers               4 32k;
proxy_busy_buffers_size     64k;
proxy_temp_file_write_size  64k;

nano nginx.conf

upstream webcluster {
    server 127.0.0.1:8080;
}

cd sites-available
nano startthedark.com

server {
    listen 80;
    server_name  media.startthedark.com;
    access_log   /var/log/nginx/startthedark.media.access.log;
    location     / {
        autoindex on;
        index     index.html;
        root      /var/www/startthedark.com/startthedark/media;
    }
}
server {
    listen       80;
    server_name  startthedark.com www.startthedark.com;
    access_log   /var/log/nginx/startthedark.django.access.log;
    if ($host !~* "^startthedark\.com") {
        rewrite  ^(.*)$ http://startthedark.com$1 permanent;
        break;
    }
    location     / {
        proxy_pass  http://webcluster;
        include     /etc/nginx/proxy.conf;
    }
}

ln -s /etc/nginx/sites-available/startthedark.com /etc/nginx/sites-enabled/startthedark.com
sudo /etc/init.d/nginx restart
  • Running Time: 20:32

Comments - 10 people have already said something. Join the discussion.

  • ali said

    The link to the .mov file is invalid

  • UloPe said

    Great Screencast.

    One thing to notice though:
    In Ubuntu it is reccomended to use aptitude instead of apt-get since it handles dependencies much better.

  • Nasim said

    May I ask where it is recommended for aptitude over apt-get? I use apt-get all the time which satisfies me handling dependencies very good.

  • huxley said

    From <a href="http://www.debian.org/doc/FAQ/ch-pkgtools.en.html#s-aptitude">The Debian GNU/Linux FAQ</a> from which Ubuntu descends:

    <blockquote>aptitude provides the functionality of dselect and apt-get, as well as many additional features not found in either program ... Note that aptitude is the preferred program for package management from console both for package installations and package or system upgrades.</blockquote>

  • huxley said

    Sigh ... do the comments support Markdown/Textile/ReST/??? instead of HTML? In the meantime just cut and paste to see the recommendation in context.

  • amccloud said

    sudo adduser --home=/var/www/startthedark.com

    should be

    sudo adduser startthedark --home=/var/www/startthedark.com

  • HW said

    Does this setup work even if you have an apache that hosts PHP? I've heard about crashing apache servers, once you introduce mod_wsgi...

    Also, can you clarify how it works? nginx takes care of all non-media requests, and works as a server too?

    Thanks for the great screencast!

  • zst said

    Great screencasts, though I'm wondering if anyone else is experiencing a clash between apache and nginx? When nginx and apache are configured based on the cut-and-paste samples in the blog post, apache throws the following error:

    (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down

    I'm assuming that's because nginx is already started and is listening on the same port. But I'll admit, I'm too much of a deployment noob to understand how to resolve...Any have any suggestions?

  • ooopinionsss said

    How you think when the economic crisis will end? I wish to make statistics of independent opinions!

  • Aldo said

    @zst: make sure apache is listening to port 8080. Check your http.conf file.

    Where is episode 14? ;-)