django notes

Notes on setting up a basic django app on a debian system using MySQL and Apache2 ...

Install the apache2 HTTP daemon
# apt-get install apache2


Install the WSGI apache2 module ( Python Web Server Gateway Interface )
# apt-get install libapache2-mod-wsgi


Install MySQL, the MySQL database development files, and the Python interface to MySQL
# apt-get install mysql-server libmysqlclient-dev python-mysqldb


Install gcc, build-essential and python headers
# apt-get install gcc build-essential python-dev


Install Python Virtual Environment Creator to avoid system Python
# apt-get install python-virtualenv


Create a virtual python environment
$ virtualenv django


Create or copy a file listing the python requirements.
e.g.
# cat requirements.txt 
Django==1.8.4                                                                                            
Markdown==2.6.2
MySQL-python==1.2.5
argparse==1.2.1
django-filter==0.11.0
djangorestframework==3.2.3
mysql==0.0.1
mysqlclient==1.3.6
wsgiref==0.1.2


And install them into the django virtual python environment
# source django/bin/activate
(django):~# pip install -r ./django/requirements.txt 


Check python modules installed into the django python virtual environment
(django):~# pip freeze
Django==1.8.4
Markdown==2.6.2
MySQL-python==1.2.5
argparse==1.2.1
django-filter==0.11.0
djangorestframework==3.2.3
mysql==0.0.1
mysqlclient==1.3.6
wsgiref==0.1.2


Create a django_project and configure the Apache HTTP daemon
(django):~# cd /var/www/sites/myproject
(django):~# django-admin startproject myproject .
(django):~# deactivate
#


Create a virtual host, eg.
# cat /etc/apache2/sites-available/000-myproject.conf 
<VirtualHost *:80>

  ServerAdmin g0@spam.bot.ipduh.com
  DocumentRoot /var/www/sites/myproject/www


 WSGIDaemonProcess wids python-path=/var/www/sites/myproject:/home/myproject/django/lib/python2.7/site-packages
 WSGIProcessGroup  myproject
 WSGIScriptAlias / /var/www/sites/myproject/myproject/wsgi.py

 Alias /static/  /var/www/sites/myproject/www/static/

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

# vim: syntax=apache ts=2 sw=2 sts=2 sr noet



Enable the new virtual apache host and restart apache
# a2ensite 000-myproject.conf
# /etc/init.d/apache2 restart


use your browser to see the django welcome page,
you should see: It worked! Congratulations ...

Configure django to use MySQL, it uses SQLite by default.

add a mysql user and a mysql database
# mysql -u root -p
mysql>create database myproject;
mysql> grant all on myproject.* to myproject_user;
mysql> set password for myproject_user=password('somepasswd');
mysql>flush privileges;
mysql> exit
Bye
#


Configure the django project to use MySQL, edit settings.py
DATABASES = {
    'default': {
  'ENGINE': 'django.db.backends.mysql',
#   'ENGINE': 'mysql.connector.django',
        'NAME': 'myproject',
        'USER': 'myproject_user',
        'PASSWORD': 'somepasswd',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}



Create the MySQL schema
(django):# python manage.py migrate


Copy the django-admin-app static files directory to /var/www/sites/myproject/www/static/
The directory that contains the admin static files for admin should be in django/lib/python2.7/site-packages/django/contrib/admin/static

Create a django super user
(django):# python manage.py createsuperuser




django - MySQL - apache2 on debian notes