Published on by nick
This post is very similar to the one that includes virtualenv. So if you want to just get this going without virtual Python environments, use these instructions. However, most will agree, you should probably set up virtualenv, as this is considered the "pro" way to do it.
Thanks to Ayman Farhat for the starting points.
First thing you need is a server running Ubuntu 12.04. I created an account at Digital Ocean and then made myself a droplet with Ubuntu 12.04 64-bit. This VM is ready to go with Python 2.7 pre-installed.
I'm running as root on the VM so I will leave out sudo but keep in mind that if you aren't running as root, the sudo command will be required in some cases.
Start by updating the repository:
apt-get update
I use vim throughout, so make sure your editor is installed:
apt-get install vim
Then let's install PIP:
apt-get install python-pip python-dev build-essential
You may need to run with --fix-missing if the command doesn't work.
Let's update PIP:
pip install --upgrade pip
You will also need MySQL or another SQL database. Check Django's docs for databases. I'm using MySQL for this tutorial:
apt-get install mysql-server
If required, follow the prompts to set up your MySQL database. Later on, we will create a database and user, if required, for the Django project.
At this point you are ready to install Django or upload existing Django projects and then install Django. Let's run through this briefly.
To install Django with PIP:
pip install Django==1.5.1
To verify Django was installed, launch the interactive python shell and run these commands:
>>> import django >>> print(django.get_version()) 1.5.1
Let's create a project now. Create and change directory into where you want to store your projects. I chose /home/django_projects. Then, use Django to create the project files:
mkdir /home/django_projects cd /home/django_projects django-admin.py startproject mysite
Pretty much the first thing you want to do next is set up your database.
First, log in to MySQL and create a database:
mysql -u root -p******
At the MySQL command prompt:
CREATE DATABASE mysite; SHOW DATABASE;
Then edit your project's settings.py file (from where you were):
vim mysite/mysite/settings.py
NOTE: Sometimes it's easier to cd into the base of the project and then work from there. The commands would be this instead:
cd /home/django_projects/mysite vim mysite/settings.py
Edit the settings.py file with the database information we want:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Using 'mysql' for this tutorial. 'NAME': 'mysite', # Database name. 'USER': 'root', 'PASSWORD': '******', 'HOST': '', # Empty for localhost. 'PORT': '', # Empty string for default. } }
Be sure to read the section notes on MySQL on the Django website. This gives information about installing the Python module for MySQL, MySQLdb.
Briefly, you need to run these commands:
apt-get install python-mysqldb pip install mysql-python apt-get install libmysqlclient-dev
For the INSTALLED_APPS to function in Django, you need database tables for them:
python manage.py syncdb
Now, let's install and configure Apache and mod_wsgi:
apt-get install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 apt-get install libapache2-mod-wsgi
This should automatically restart Apache but just to be sure, issue this command:
service apache2 restart
We need to now create a virtual host and set up WSGI. First, let's create the site in the Apache configuration:
vim /etc/apache2/sites-available/mysite
The contents of this file should look like this:
<VirtualHost *:80> ServerAdmin [email protected] ServerName mysite.com ServerAlias www.mysite.com WSGIScriptAlias / /var/www/mysite/index.wsgi Alias /static/ /var/www/mysite/static/ <Location "/static/"> Options -Indexes </Location > </VirtualHost >
Now we need to create the WSGI file that's accessed for our Django project:
mkdir /var/www/mysite vim /var/www/mysite/index.wsgi
In it, add these lines:
import os import sys import site # Add the site-packages of the chosen virtualenv to work with site.addsitedir('~/.virtualenvs/mysiteenv/local/lib/python2.7/site-packages') # Add the app's directory to the PYTHONPATH sys.path.append('/home/django_projects/mysite') sys.path.append('/home/django_projects/mysite/mysite') os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
Let's set where static files go for Django:
mkdir /var/www/mysite/static vim /var/www/mysite/mysite/settings.py
Make sure these lines are set:
STATIC_ROOT = '/var/www/mysite/static/' STATIC_URL = '/static/'
Next, we need to tell Django to collect the static files for use on the site:
python manage.py collectstatic
Restart Apache again and you should be good to go:
service apache2 restart
Drop me a line...
[email protected]
Follow me on Twitter...
@nicorellius
Share on
Comments
Comments powered by Disqus