Roshan Book

My Tech Notebook

Installing Django on an Ubuntu Linux Server


This post was originally published at http://jeffbaier.com/articles/installing-django-on-an-ubuntu-linux-server/

Installing Django on an Ubuntu Linux Server

Today I had the task of installing a development server running the Django Python framework for one of our web developers. I learned a few things and I figured a quick tutorial might help someone else out. None of this covers new ground, but perhaps another telling of the story will help someone out there. I started from scratch, with a basic install of Ubuntu 7.04 Server Edition. I did not choose any extra packages, such as the LAMP option or DNS server. If you are starting off with a LAMP server already installed, or a different version, the steps will be similiar, but you may need to adapt some commands to get them to work.
Install server software

Install Apache, Mod_Python, MySQL and MySQLdb. MySQLdb is the database bindings for MySQL. Django also supports PostgreSQL, Oracle and SQLite. If you choose to use a different database server, be sure to install the appropriate Python bindings.
sudo apt-get install apache2 libapache2-mod-python
sudo apt-get install mysql-server python-mysqldb
Install the Django source code

At this point you have a couple of options. You could “apt-get install” a Django package, install an official release or install the development version. I chose to install the development version because it contains the latest bug fixes and features. We’ll be checking out the latest version from its Subversion repository. You’ll want to be in your home directory when you do this.
cd ~/

svn co http://code.djangoproject.com/svn/django/trunk/ django_src

Python won’t recognize Django unless it is installed in the “site-packages” directory, so instead we just create a symbolic link to the source code in our home directory. Run the first command to find out the path to your “site-packages” directory. Then use it in the second command, in place of “YOUR-DIR”. Lastly, copy the django-admin.py file into /usr/local/bin so that we don’t have to qualify the command with the full path to the file.
python -c “from distutils.sysconfig import get_python_lib; print get_python_lib()”

ln -s `pwd`/django_src/django YOUR-DIR/django

sudo cp ~/django_src/django/bin/django-admin.py /usr/local/bin
Create Django’s directories

Next we need to create some directories that Django will use. Once again, create these under your home directory.
cd ~/

mkdir django_projects

mkdir django_templates

mkdir media

Then we need to create some symbolic links in your webroot. The default webroot for an Apache2 installation on Ubuntu is /var/www. We are going to create a link to the media folder in your home directory, and a link to the admin_media folder which is provided in the Django source code.
cd /var/www

sudo ln -s ~/media media

sudo ln -s ~/django_src/django/contrib/admin/media admin_media
Create a Django project

Move into your Django projects directory that we just created. We will be starting a new project using Django’s command line utility. This will give us a basic directory structure and the necessary configuration files. In my example I named the project “myproject.” Feel free to choose any name, as long as it does not conflict with any built-in Python or Django components. In particular, this means you should avoid using names like django (which will conflict with Django itself) or site (which conflicts with a built-in Python package).
cd ~/django_projects

django-admin.py startproject myproject

Edit the myproject/settings.py file and change the following sections:
Uncomment and change the ADMINS setting
ADMINS = (

(‘Your Name’, ‘your_email@domain.com’),

)
Enter your database settings. You will need your database, username and password. Most likely your database server is running on the same server, so leave DATABASE_HOST blank.
DATABASE_ENGINE = ‘mysql’ # ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’.

DATABASE_NAME = ‘django_databae’ # Or path to database file if using sqlite3.

DATABASE_USER = ‘you’ # Not used with sqlite3.

DATABASE_PASSWORD = ‘yourpassword’ # Not used with sqlite3.

DATABASE_HOST = ” # Set to empty string for localhost. Not used with sqlite3.

DATABASE_PORT = ” # Set to empty string for default. Not used with sqlite3.
Change your timezone if necesary.
# Local time zone for this installation. Choices can be found here:

# http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE

# although not all variations may be possible on all operating systems.

# If running in a Windows environment this must be set to the same as your

# system time zone.

TIME_ZONE = ‘America/Chicago’
Point Django at the template directory we created.
TEMPLATE_DIRS = (

“/home/YOUR_USERNAME/django_templates”

# Put strings here, like “/home/html/django_templates” or “C:/www/django/templates”.

)
Do the same thing for the media url and directory we created earlier.
# Absolute path to the directory that holds media.

# Example: “/home/media/media.lawrence.com/”

MEDIA_ROOT = ‘/home/YOUR_USERNAME/media/’# URL that handles the media served from MEDIA_ROOT. Make sure to use a

# trailing slash if there is a path component (optional in other cases).

# Examples: “http://media.lawrence.com”, “http://example.com/media/”

MEDIA_URL = ‘http://yourdomain.com/media/’
Set the admin media directory that we created in your webroot
# URL prefix for admin media — CSS, JavaScript and images. Make sure to use a

# trailing slash.

# Examples: “http://foo.com/media/”, “/media/”.

ADMIN_MEDIA_PREFIX = ‘/admin_media/’
And finally, add the admin application to your install applications
INSTALLED_APPS = (

‘django.contrib.auth’,

‘django.contrib.contenttypes’,

‘django.contrib.sessions’,

‘django.contrib.sites’,

‘django.contrib.admin’,

)

After making all those changes to the configuration we need to synchronize the Django database. You’ll also get a prompt asking you if you’d like to create a superuser account for the authentication system. Go ahead and do that.
django-admin.py syncdb

Edit the URL configuration file and uncomment the admin line. This will allow you to access the admin section later.
nano ~/django_projects/myproject/urls.py
# Uncomment this for admin:

(r’^admin/’, include(‘django.contrib.admin.urls’)),
Configure Apache and mod_python

In Apache2 on Ubuntu, changes to the Apache configuration are done to the /etc/apache2/httpd.conf file. We are going to configure mod_python to handle requests for the root of the site and give control to Django. However, Django doesn’t serve media files itself; it usually expects you to have a different webserver serving these files. In our case though, we want Apache to handle it, so we need to turn off mod_python for some parts of the site. You will also need to do this if you have any other folders or scripts that you want excluded from Django’s control. In my example, I have phpMyAdmin, my media folders and any URL that ends with .jpg, .gif or .png be excluded.

When deploying Django sites on mod_python, you’ll need to restart Apache each time you make changes to your Python code. However, since I’m using this as a development server, I discovered a way to avoid the hassle of having to restart the server each time. At the top of my httpd.conf, I have the line MaxRequestsPerChild 1. This forces Apache to reload everything for each request. Do not use this setting on a production server!

The only other lines you need to change are in the first block. Change “myproject.settings”, if you are using a different name for your project. Then below that be sure to change the PythonPath to point to the django_projects folder in your home directory.
sudo nano /etc/apache2/httpd.conf
MaxRequestsPerChild 1

SetHandler python-program

PythonHandler django.core.handlers.modpython

SetEnv DJANGO_SETTINGS_MODULE myproject.settings

PythonPath “[‘/home/YOUR_USERNAME/django_projects’] + sys.path”

SetHandler None

SetHandler None

SetHandler None

SetHandler None

Restart Apache and pray
sudo /etc/init.d/apache2 restart

You can now view your new Django website by visiting the root of the website you installed it at – http://yourdomain.com/ . You should see a Page not Found (404) error message generated by Django. Congratulations! Everything is working. You can also go to http://yourdomain.com/admin/ and log in with the superuser you created earlier.

Now that you’re done with the easy part, all thats left is to get started writing your Django apps. There is a tutorial on the official Django website that will walk you through creating an application.

Advertisements

27 responses to “Installing Django on an Ubuntu Linux Server

  1. hostgator promo codes November 12, 2011 at 6:15 pm

    If you are searching for a way to save on the month-to-month package deal using Hostgator why not take some time to discover a coupon codes you can implement straight to your own shopping cart application. You can locate constraints that may provide you with a 25 percent lower price right away or maybe $100 off of the first thirty day period should you have Glass windows Server colocation. Finding deals pertaining to Hostgator will be easy and quick all of which will save you double or triple digits.

  2. Jacquiline Hardware November 14, 2011 at 2:07 pm

    Hey, took me time to read all the comments, but I really enjoyed the article. It proved to be very helpful to me and I am quite sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Consequently: thanks a lot and please please go on with your good work. I’ll surely come back and check for new articles! Ciou

  3. UGG Classic Short Paisley 5831 November 18, 2011 at 3:24 am

    Thank you, I’ve just been looking for info about this topic for a long time and yours is the greatest I have found out so far. However, what concerning the bottom line? Are you sure concerning the supply?

  4. CallumCameron November 19, 2011 at 7:26 pm

    Amazing! This blog looks exactly like my old one! It’s on a completely different subject but it has pretty much the same page layout and design. Great choice of colors!

  5. Burberry Outlet UK November 20, 2011 at 2:57 am

    Surprisingly educative quite a few thanks, I believe your trusty subscribers could possibly want a superior deal more stories along these lines carry on the terrific work.

  6. Louis Vuitton Handbags Cheap November 22, 2011 at 3:18 pm

    Thanks for this great post.I am a total beauty fallower and so will exactly pursue for the fallowing ones. Have a awesome day.

  7. Louis Vuitton Bags For Cheap November 23, 2011 at 2:42 pm

    This genuinely is superb post ! I just love’d this !

  8. Louis Vuitton Replica UK November 24, 2011 at 1:21 pm

    I identified this internet site via aol and I’m actually glad about the facts you provide in your blog. Btw your sites really broken using firefox4

  9. Louis Vuitton Belt Sale November 24, 2011 at 8:31 pm

    What i come across tough would be to locate a weblog that may seize me for a minute but your blog is diverse.

  10. Replica Chanel Handbags November 25, 2011 at 8:18 am

    This genuinely is superb post ! I just love’d this !

  11. Louis Vuitton Wallets Sale November 25, 2011 at 1:42 pm

    I like your blog so considerably that I feel I have to wish you. Happy New Year in advance. Have a good and prosperous year ahead

  12. Cheap Louis Vuitton Bags November 26, 2011 at 8:55 pm

    I conceive this site has got extremely fantastic composed content articles.

  13. Louis Vuitton Bags Outlet November 28, 2011 at 6:39 am

    Valuable info. Lucky me I located your web web page by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it

  14. Hermes Sale November 28, 2011 at 12:11 pm

    Hi, I’ve read this twice now. Thanks so a lot. You’re one of those consumers I go to for blogging advises.

  15. Designer Handbags November 29, 2011 at 6:26 am

    I wasn’t aware of the a large number of ripples and depth to this story until I surfed here through Google! Fantastic job.

  16. Louis Vuitton Knockoffs Handbags November 29, 2011 at 12:05 pm

    This was extremely interesting and helpful, I have bookmarked and hope to check back to your web page again.

  17. replica louis vuitton bags November 29, 2011 at 3:41 pm

    Excellent post.I want to thank you for this informative read, I genuinely appreciate sharing this good post. Keep up your work.

  18. Designer Replica Bags November 30, 2011 at 6:49 am

    Hi, I’m presently writing a piece on this topic. If you dont have an objection I may well borrow a snippet. In case I do I’ll cite your domain and add a link.

  19. Knock Off Louis Vuitton Luggage December 6, 2011 at 4:20 am

    You could definitely see your expertise in the paintings you write. The sector hopes for even more passionate writers such as you who aren’t afraid to mention how they believe. At all times go after your heart.

  20. canada goose trillium parka December 10, 2011 at 10:19 pm

    It??s difficult to get knowledgeable people on this topic, nevertheless, you sound like there’s more you?fre dealing with! Thanks??

  21. invest liberty reserve December 12, 2011 at 5:00 pm

    An intriguing word is couturier scuttlebutt. I judge that you should correspond writer on this issue, it might not be a inhibition field but generally fill are not enough to verbalise on much topics. To the succeeding. Cheers like your Installing Django on an Ubuntu Linux Server Roshan Book.

  22. minecraft skins December 12, 2011 at 5:17 pm

    An newsworthy word is designer report. I consider that you should write much on this matter, it strength not be a taboo content but generally group are not sufficiency to mouth on much topics. To the next. Cheers like your Installing Django on an Ubuntu Linux Server Roshan Book.

  23. Constipation remedies December 13, 2011 at 3:56 pm

    An riveting discourse is designer statement. I reckon that you should pen more on this content, it power not be a prejudice somebody but mostly group are not enough to verbalise on such topics. To the succeeding. Cheers like your Installing Django on an Ubuntu Linux Server Roshan Book.

  24. Ronald Hopkins December 14, 2011 at 12:02 am

    That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here. From the minute you arrive at a tweetup, you can just feel all the positive energy. It’s inspiring and provides such a great atmosphere to unwind, mingle have a beer.

  25. LA Weight Loss December 16, 2011 at 8:21 am

    I got what you intend, thanks for putting up. Woh I am happy to feel this website through google. Thanks For Share Installing Django on an Ubuntu Linux Server Roshan Book.

  26. Timothy Plutt December 16, 2011 at 11:46 pm

    Thank you for your blog post. Velupe and I happen to be saving to buy a new e book on this subject and your article has made us to save the money. Your thoughts really responded to all our issues. In fact, a lot more than what we had known in advance of the time we came across your superb blog. I actually no longer have doubts as well as a troubled mind because you totally attended to all of our needs in this post. Thanks

  27. ebay account suspension December 17, 2011 at 9:02 pm

    Have eBay suspend your account? I am suspended for simply no purpose! I misplaced all my earnings, my business had been online dependent. I had to locate a solution. At the conclusion I am back for real.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: