I’ve recently moved my Django websites from Dreamhost due to poor FastCGI performance to the 256 plan option from SliceHost. If you sign-up with them, please use “inerte@gmail.com” as your referral. I have no idea how much they pay for referrals, but any money is good money 😉
Here’s my guide:
Sign-up to SliceHost
Painless registration gave me the root password in 2 minutes. Write down your IP address and your root password somewhere so you won’t forget it.
Setup the packages
SSH (I’ve used Putty) to your server, authenticate, and execute the commands below. I’ve decided to install PHP because I have domains using it. Also, it’s necessary for phpMyAdmin.
$ apt-get install apache2 $ apt-get install libapache2-mod-python2.4 $ apt-get install mysql-server $ apt-get install python2.4-mysqldb $ apt-get install php5 $ apt-get install php5-mysql $ apache2ctl restart $ /etc/init.d/apache2 reload
Setup Mysql
$ mysql -u root mysql> UPDATE mysql.user SET Password=PASSWORD ('your_root_mysql_password') WHERE User = 'root'; mysql> FLUSH PRIVILEGES; mysql> quit
Optional: Install phpMyAdmin
It’s just easier. I’ve decided not to install a FTP server to upload my files. Instead, I’ve used WinSFTP, a sftp client for Microsoft Windows. Download and install it. Open, paste your Slice IP address and browse to the /var/www/ directory, upload phpMyAdmin and follow its install instructions.
Setup Django
$ cd /usr/lib/python2.4/site-packages/ $ svn co http://code.djangoproject.com/svn/django/trunk/ django
Setup your Django project
Open WinSFTP again, browse to the /usr/lib/python2.4/site-packages/ directory and upload your Django project.
Edit its settings.py file.
vi /usr/lib/python2.4/site-packages/your_django_project/settings.py
I’ll show only what you have to change, besides whatever is needed for your project to work (like INSTALLED_APPS):
DATABASE_ENGINE = 'mysql' DATABASE_NAME = 'your_db_name' DATABASE_USER = 'your_db_user' DATABASE_PASSWORD = 'your_db_password'
I use a directory for media files called “web” on most of my projects:
MEDIA_ROOT = '/var/www/your_domain.com/web/' MEDIA_URL = '/web/'
You’ll also have to change the TEMPLATE_DIRS tuple. Just put whatever you use. Here’s mine for reference:
TEMPLATE_DIRS = (
# Put strings here, like “/home/html/django_templates”.
# Always use forward slashes, even on Windows.
‘/usr/lib/python2.4/site-packages/my_django_project/templates/my_django_project/’,
)
Setup the domain
$ mkdir /var/www/your_domain.com $ mkdir /var/log/apache2/your_domain.com $ vi /etc/apache2/sites-available/your_domain.com
Paste this text inside the file:
<VirtualHost *>
ServerName www.your_domain.com
ServerAlias your_domain.com
# The three lines below remove the www from the domain name. I don’t like wwws.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.your_domain\.com [NC]
RewriteRule (.*) http://your_domain.com$1 [R=301,L]
DocumentRoot /var/www/your_domain.com
CustomLog /var/log/apache2/your_domain.com/access.log combined
ErrorLog /var/log/apache2/your_domain.com/error.log
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE your_django_project.settings
PythonDebug Off
PythonPath “[‘/usr/lib/python2.4/site-packages/django’] + sys.path”
# My own media directory (as mentioned in the previous section)
<Location “/web/”>
SetHandler None
</Location>
# Necessary for Django’s admin media files
<Location “/media/”>
SetHandler None
</Location>
</VirtualHost>
Symlink your new domain configuration file to the correct directory:
ln -s /etc/apache2/sites-available/your_domain.com /etc/apache2/sites-enabled/your_domain.com
Symlink Django’s admin media files to your domain:
ln -s /usr/lib/python2.4/site-packages/django/django/contrib/admin/media/ /var/www/your_domain.com/media
Edit Apache’s configuration file to tell it your IP address:
vi /etc/apache2/apache2.conf
Paste this before the # Include the virtual host configurations: line (it’s one before the last):
ServerName your.slice.ip.address
Edit Apache’s log rotation to include your new domain:
vi /etc/logrotate.d/apache2
Paste this at the end:
/var/log/apache2/your_domain.com/*.log { weekly missingok rotate 52 compress delaycompress notifempty create 640 root adm sharedscripts postrotate if [ -f /var/run/apache2.pid ]; then /etc/init.d/apache2 restart > /dev/null fi endscript }
DNS Server
I’ve tried to install a DNS Server but not only I thought it’s hard, having a single point of failure is bad. So I’ve signed up with DNS Made Easy and I let them manage this for me. It’s super simple, after joining, add your domain to DNS Made Easy and write down the DNS server addresses. Wait until the domain name is “created” (to me, it varied from 30 minutes to 2 hours), and change the dns servers from your domain name registrar.
Last action
Restart Apache one more time:
$ apache2ctl restart $ /etc/init.d/apache2 reload
And we’re done! With one caveat: We’ve checked out Django “trunk” directory into your Python’s directory. That means the real django lives a directory below:
/usr/lib/python2.4/site-packages/django/django/
Which means that if you ever want to use Django outside Apache’s mod_python, you’ll have to add the /usr/lib/python2.4/site-packages/django directory to your sys.path. Or, you could checkout Django to somewhere else, and move the “real” django directory to site-packages/ (and change the /etc/apache2/sites-available/your_domain.com) accordingly.
If you’re having problems, write something at the comments here and I will try to help you.
Leave a Reply
You must be logged in to post a comment.