Friday, April 16, 2010

Installing Django (Python Framework) on an Ubuntu Linux Server

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

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

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:
  1. Uncomment and change the ADMINS setting



    6
    7
    8
    9
    10
    ADMINS = (
     
         ('Your Name', 'your_email@domain.com'),
     
    )
  2. 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.

  3. Change your timezone if necesary.



    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # 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'
  4. 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".
     )
  5. Do the same thing for the media url and directory we created earlier.



    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    # 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/'
  6. Set the admin media directory that we created in your webroot



    45
    46
    47
    48
    49
    50
    51
    # 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/'
  7. And finally, add the admin application to your install applications



    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    INSTALLED_APPS = (
     
        'django.contrib.auth',
     
        'django.contrib.contenttypes',
     
        'django.contrib.sessions',
     
        'django.contrib.sites',
     
        'django.contrib.admin',
     
    )
      
Then go to the directory which have file 
manage.py  
and run command on terminal 
(please first remember the creating database name "django" in mysql)  
#manage.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
1
2
3
   # Uncomment this for admin:
     (r'^admin/', include('django.contrib.admin.urls')),
 

Configure Apache and mod_python

 sudo nano /etc/apache2/httpd.conf
 
MaxRequestsPerChild 1
<location "/">
 
    SetHandler python-program
 
    PythonHandler django.core.handlers.modpython
 
    SetEnv DJANGO_SETTINGS_MODULE myproject.settings
 
    PythonPath "['/home/YOUR_USERNAME/django_projects'] + sys.path"
 
</location>
 
<location "/media">
 
    SetHandler None
 
</location>
 
<location "/admin_media">
 
    SetHandler None
 
</location>
 
<location "/phpmyadmin">
 
    SetHandler None
 
</location >
 
<locationmatch ".(jpg|gif|png)$">
 
    SetHandler None
 
</locationmatch> 

Restart Apache and pray

sudo /etc/init.d/apache2 restart

No comments:

Post a Comment