Django Wrangler
How to set up Django on Debian-based linux with Apache and MySQL.
Django is a fairly lofty python-based web framework. Setting up Django is a little more prickly than bog-standard apache with php.
This step-by-step guide explains the setup securely using a python virtual environment. Assumes a basic knowledge of Django itself. I recommend this tutorial and looking through someone else's work for beginners.
0. ./static/ and the MySQL db
A standard Django project directory structure looks like
i@pc:/var/www/mysite.tld$ ls
mysite siteapp manage.py
The two most important files for us will be manage.py and mysite/settings.py.
Open mysite/settings.py and edit the following
DEBUG = False
...
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.mysql',
'NAME' : 'mysitedb', # edit
'USER' : 'mysiteuser', # edit
'PASSWORD': 'password1', # edit
'HOST' : '',
'PORT' : '',
}
}
...
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
...
We want DEBUG = False to make sure our static-file handling is good, and so that viewers cannot see debug error messages. Now, we have to create the database and user with mysql
i@pc:/var/www/mysite.tld$ mysql -u root -p
Enter password:
mysql> CREATE DATABASE mysitedb;
mysql> CREATE USER 'mysiteuser'@'localhost' IDENTIFIED BY 'password1';
mysql> GRANT ALL PRIVILEGES ON mysitedb.* TO 'mysiteuser'@'localhost';
1. Virtual Environments
Now we need to create a python virtual environment. This separate python install allows for installing specific-version modules without conflicting one's global python. It also serves as a mild security measure.
i@pc:/var/www/mysite.tld$ sudo apt-get install virtualenv
...
i@pc:/var/www/mysite.tld$ virtualenv myenv
...
i@pc:/var/www/mysite.tld$ ls
myenv mysite siteapp manage.py
i@pc:/var/www/mysite.tld$ ls myenv
bin include lib local share
So, as you can see it's essentially just another python install. In order to use it, rather than our global python, we must
i@pc:/var/www/mysite.tld$ source myenv/bin/activate
(myenv) i@pc:/var/www/mysite.tld$
To exit, simply enter deactivate. Now we are ready to start installing modules to our virtual environment. Usually most python applications come with requirements. Some common ones that we will need are
(myenv) i@pc:/var/www/mysite.tld$ pip install django MySQL-python
Note: If you have trouble installing MySQL-python try:
sudo apt-get install libmysqlclient-dev
2. Initialising Django
Right now, our database is still empty. Django, with its models, fills database structures for us automatically.
(my env) i@pc:/var/www/mysite.tld$ /manage.py makemigrations
(my env) i@pc:/var/www/mysite.tld$ /manage.py migrate
With any luck, the database structure will be created. Now we need to create a superuser.
(my env) i@pc:/var/www/mysite.tld$ /manage.py createsuperuser
To get apache to serve Django's static files, it needs to clone them into /var/www/mysite.tld/static/, do this by
(my env) i@pc:/var/www/mysite.tld$ /manage.py collectstatic
(my env) i@pc:/var/www/mysite.tld$ ls
myenv mysite siteapp static manage.py
We can now test the page with
(my env) i@pc:/var/www/mysite.tld$ /manage.py runserver 0.0.0.0:8000
and visiting http://<server ip>:8000 in your browser. Still, we want to use apache with vhosts to serve our site.
3. Configuring Apache vhost
This part is easy, create mysite.tld.conf in /etc/apache2/sites-available/ with the following content
<VirtualHost *:80>
ServerAdmin you@youremail.com
DocumentRoot /var/www/mysite.tld
ServerName mysite.tld
ServerAlias www.mysite.tld
Alias /static /var/www/mysite.tld/static
<Directory /var/www/mysite.tld/static>
Require all granted
</Directory>
<Directory /var/www/mysite.tld/mysite/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-path=/var/www/mysite.tld:/var/www/mysite.tld/myenv/lib/python2.7/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite.tld/mysite/wsgi.py
</VirtualHost>
Enable and reload as normal
i@pc:/var/www/mysite.tld$ sudo a2ensite mysite.tld.conf
i@pc:/var/www/mysite.tld$ sudo service apache2 reload
Now you should be able to access your site at http://mysite.tld.
4. Possible Issues
There are a lot of moving parts to set this up, so if just one breaks there is a high potential for minor issues. Here's a list of a few.
- Permissions. You may need to chmod to be able to post/upload.
- Incorrectly installed python modules. Search for the errors.
- Folder locations. Especially if you have file uploads.
Try enabling DEBUG = True and limiting ALLOWED_HOSTS = ['<your ip>'] for clues to Django errors.