Install Django on Apache with mod_wsgi

OS : Ubuntu
Database : MYSQL
Django version : 1.6.4
Steps :
1. sudo apt-get install python-mysqldb
    This library enables python to use mysql
2. download & install django
3. sudo apt-get install mysql-server
4. sudo apt-get install apache2
5. sudo apt-get install libapache2-mod-wsqi
    This is an interface to communicate with python through apache
6. create anyfile.wsgi, contains :    
   import os
   import sys
   sys.path = ['/var/www/anything'] + sys.path
   os.environ['DJANGO_SETTINGS_MODULE'] = 'anything.settings'
   import django.core.handlers.wsgi
   application = django.core.handlers.wsgi.WSGIHandler()
7. create configuration file at : /etc/apache2/sites-available
   example: anything.conf. It contains the following :
   <VirtualHost *:80>
   WSGIScriptAlias / /path_to_the_wsgi_file_we_create_before
 
   serverName website.com
   Alias /static /var/www/anything/static/
 
   <Directory /var/www/anything/>
   Order allow,deny
   Allow from all
   </Directory>
   </VirtualHost>
 
   note :
   website.com : the domain name of our website
   Alias /static /var/www/anything/static/ : This is for the admin interface for django.

8. To enable the site, do :  sudo a2ensite anything.conf
 
9. create project at folder : /var/www
    sudo django-admin.py startproject anything
 
10. restart the apache : sudo apachectl restart
11. Check /etc/hosts, make sure that the ipaddress and domain name are there. If not, create it

12. create database :
      mysql -u root -p
      create database firstdb;
      exit
  
13. edit /var/www/anything/anything/settings.py , find DATABASE and change as the following:
   DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'firstdb',
        'USER': 'username',
        'PASSWORD': 'passwd',
    }
   }
   note :
   username & passwd correspond to the authentification of database that we create before

14. go back to upper folder until reach manage.py in there
      run : python manage.py syncdb

15. delete : /etc/apache2/sites-enabled/000-default
      we can regenerate the file by : < sudo a2ensite default > in the folder /etc/apache2/sites-available

16. restart apache: apachectl restart  
17. (This is optional) In order to change the interface of www.ourwebsite.com/admin, do the following:
     cp -r /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/ /var/www/anything   

No comments:

Post a Comment