Setup Django on Ubuntu 8.04 LTS
Posted by brice | Filed under apache , django , linux , python , ubuntu
Installation
First, we will create the user with the name "project" (feel free to use the name you prefer but change it in configuration files too) and install Apache 2 as recommanded by Django Project. We will also install subversion to be able to retrieve the last version of django.
sudo adduser project
sudo apt-get update
sudo apt-get install python-mysqldb subversion libapache2-mod-python apache2-mpm-prefork mysql-server
Now we will download the last version of django through their subversion repository. All the sources will be put in /usr/local/src ... because that's the best place to put them :)
cd /usr/local/src/
sudo svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk
once this part done, we create the symbolic link to the django libraries and make django-admin software in the bin PATH.
sudo ln -s /usr/local/src/django-trunk/django /usr/lib/python2.5/site-packages/django
sudo ln -s /usr/local/src/django-trunk/django/bin/django-admin.py /usr/local/bin/
Configuration
First, to ensure that the configuration is ok, we will quickly create 3 django projects. Later you will just create a dev, make it a repository (I'm used to mercurial) and clone it to staging and release it on deployment.
su project
cd
django-admin.py startproject dev
django-admin.py startproject staging
django-admin.py startproject deployment
Now, we have 3 (empty) django projects and we will setup Apache 2 to access those ones. Edit a new Apache2 config website file (I'm personnaly used to vi software, feel free to replace "vi" by "nano" ) :
sudo vi /etc/apache2/sites-available/django
and add the loading of mod_python when someone try to access to the server through www.example.com (for deployment), dev.example.com (for dev) or staging.example.com (for staging):
Alias /media/ /usr/local/src/django-trunk/django/contrib/admin/media/
<Location "/media/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png)$">
SetHandler None
</LocationMatch>
# Deployment server
<VirtualHost *>
PythonPath "['/home/project/deployment/'] + sys.path
ServerAdmin bbrriiccee@gmail.com
ServerName www.example.com
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
PythonOption django.root
PythonDebug Off
PythonAutoReload Off
PythonInterpreter deployment
Alias /medias/ /home/project/deployment/medias/
<Location "/medias/">
SetHandler None
</Location>
</VirtualHost>
# Dev server
<VirtualHost *>
PythonPath "['/home/project/dev/'] + sys.path
ServerAdmin bbrriiccee@gmail.com
ServerName dev.example.com
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
PythonOption django.root
PythonDebug On
PythonAutoReload On
PythonInterpreter dev
<Location "/">
AuthType Basic
AuthName "dev"
AuthUserFile /home/project/dev/.htpasswd
Require valid-user
</Location>
Alias /medias/ /home/project/dev/medias/
<Location "/medias/">
SetHandler None
</Location>
</VirtualHost>
# Staging server
<VirtualHost *>
PythonPath "['/home/project/staging/'] + sys.path
ServerAdmin bbrriiccee@gmail.com
ServerName staging.example.com
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
PythonOption django.root
PythonDebug Off
PythonAutoReload Off
PythonInterpreter staging
<Location "/">
AuthType Basic
AuthName "staging"
AuthUserFile /home/project/staging/.htpasswd
Require valid-user
</Location>
Alias /medias/ /home/project/staging/medias/
<Location "/medias/">
SetHandler None
</Location>
</VirtualHost>
example.com domain doesn't exist on Internet on purpose and it's not a good idea to use it, but it's just for the "example" purpose ; so add this line to your hosts file (on Mac and Linux in /etc/hosts ) to make it available:
10.0.1.194 dev.example.com staging.example.com www.example.com
where 10.0.1.194 is the ip of your server. As you can read, I defined a basic http auth system on the staging and dev. This is because they are usually not supposed to be publicly available. I will use htpasswd tool to create a .htpasswd file and make just a symbolic link for staging (usually those who have access to dev should have access to staging and vice-versa) so everybody will keep the same name/password without any double input :
cd ~/dev/
htpasswd -c .htpasswd admin
ln -s ~/dev/.htpasswd ~/staging/.htpasswd
Then, lets adding this config to the enabled Apache website.
sudo a2ensite django
sudo /etc/init.d/apache2 restart
Theoretically, your servers should now be available at:
Next
The next step should be the configuration of a light http server for the static files instead of using Apache to lower the charge. You should make the domain match your DNS :p and you will probably need to select the right version control system (maybe mercurial). have fun :)
May 13, 2011 a.m.31 11:16 a.m.
Thank God! Smoeone with brains speaks!