How to set up Django, PHP and Apache on Amazon EC2

I just deployed an Apache server hosting both php-based WordPress blog and Django projects website, being visited from different domains/subdomains. Here is what I did.

Step 1: Setup LAMP Server on Amazon EC2

Follow the office Amazon EC2 tutorial to install LAMP server. Successfully host a WordPress Blog.

Step 2: Install Django

# Install Django from home directory
tar xzvf Django-1.6.2.tar.gz
cd Django-1.6.2
sudo python install

Step 3: Configure Apache to host Django

Open httpd.conf file to configure Apache Virtual Host.

sudo vim /etc/httpd/conf/httpd.conf

Since I want to set up django virtual server on a separate domain/subdomain, so a virtual host needed to be set up here. WSGI is used for Django living with PHP.
Towards the end of the documents, uncomment:

Name VirtualHost *:80

Then change the virtual host as following:

Note the “WSGIPythonPath /django/mysite” has to be outside “VirtualHost” tag and “WSGIScriptAlias / /django/mysite/mysite/” has to be just before Django projects directory.

Then set up the logfiles

mkdir logs
cd logs
vim apache_error.log 
vim apache_access.log

Step 3: Hello World Django

cd into a directory where you’d like to store your code, then run the following command to create mySite project: startproject mysite

Start an application called helloworld

$ python startapp helloworld

Write your code in and, for reference: Tutorials from Django
The startproject command creates a file / that contains such an application callable.
It’s used both by Django’s development server and in production WSGI deployments.

Register your helloworld application in your site:
Open, add ‘helloworld’ to INSTALLED_APPS

Step 4: Restart Apache

After making changes to the httpd.conf file, it’s a good idea to restart Apache by typing this command:

service httpd restart

Then you can access your website from and Django website from on the same server.

Remember to move contents of static directory (CSS, images) to your own static directory, for reference: Managing static files (CSS, images)



Leave a Reply

Your email address will not be published. Required fields are marked *