Enjoy your day!
There was a need to configure on your server with PHP under Apache support for Django. I thought it would not be difficult and everything will go like clockwork, but it turned out not quite. The fact is that I decided to set up the latest version of the third python patch file. Then the pitfalls began to emerge.
The main problems encountered:
- Python version on Ubuntu Server 12.04 - 2.7, and I wanted to use the third version
- Django supports Python version starting from 3.2, while mod_wsgi in stable version does not support version 3.2.
- Lack of complete guidance for solving these problems.
Actually, mainly because of the third point, I spent quite a long time and later decided to write this article.
Let's get started
I’ll say right away that I assume that apache2 and PHP are already on the server. We only add functionality to what we already have.
For a start, I decided for myself that it would not be bad to put the most recent version of python - 3.3 (this server is more for training purposes, and I want to learn on all the newest ones), but in Ubuntu 12.04, you can only put 3.2 by default. To add support for the latest version, add a new
repository :
sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update
Install Python 3.3 and Python 3.3-dev (we will need it to compile mod_wsgi a bit later):
sudo apt-get install python3.3 sudo apt-get install python3.3-dev
The next step is to install mod_wsgi, it is needed for a bunch of python and apache. The one available in the distribution kit, available with the aptitude command, will not work, as it does not support Python version 3.3. But the developers of the module are working on this and the repository already has the necessary code, so we just have to compile the latest version from the
sources and add the address to the newly installed python when compiling:
hg clone https://code.google.com/p/modwsgi/ cd ./modwsgi/mod_wsgi ./configure --with-python=/usr/bin/python3.3 make sudo make install
Now move on to installing Django 1.6. The installation from the repository is not suitable for us again, because version 1.3 is there, which is much smaller than 1.6 and, moreover, does not support Python 3, so we re-install it not on a straight line:
wget https://www.djangoproject.com/download/1.6/tarball/ tar xzvf index.html cd Django-1.6 sudo python setup.py install
And so, now we have a pre-installed apache, the last python is installed, the django and now we have to configure all this. First, create a folder for our site and a jango application:
mkdir -p ~/public_html/domain1.com cd ~/public_html/domain1.com django-admin.py startproject MyProject
')
Create a virtual host and WSGI file
Create a virtual host:
sudo nano /etc/apache2/sites-available/domain1.com
Inside this file we write:
<VirtualHost *:80> ServerName domain1.com ServerAlias www.domain1.com WSGIScriptAlias / /home/username/public_html/domain1.com/MyProject.wsgi </VirtualHost>
Create a WSGI file:
nano ~/public_html/domain1.com/MyProject.wsgi
We write down the settings for our site:
import os import sys sys.path.append('~/public_html/domain1.com/') os.environ['DJANGO_SETTINGS_MODULE'] = 'MyProject.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
Apache setup
Now you need to edit the httpd.conf file:
sudo nano /etc/apache2/httpd.conf
We write to it:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so WSGIScriptAlias / /home/username/public_html/domain1.com/MyProject/MyProject/wsgi.py WSGIPythonPath /home/username/public_html/domain1.com/MyProject <Directory /home/username/public_html/domain1.com> <Files wsgi.py> Order deny,allow Require all granted </Files> </Directory>
Almost done
Turn on the virtual host, restart the Apache:
sudo a2ensite domain1.com sudo service apache2 restart
Now your new site should start and be happy to inform you that: “It worked!”, And: “Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [appname]. ”.

That's all! I hope you will succeed much faster than I did. Enjoy your coding!