Django 2.0.2 + Python3 + Apache + mod_wsgi

Steps to deploy a Django 2.0.2 project using apache server and mod_wsgi.

I was recently trying to deploy a python 3.5 web application on an EC2 Amazon Linux AMI. I thought it should be a quick process, however it took me some time to make sure all dependencies are pointing to python-3.5 on the machine. Here are some things I did to finally deploy the django project.

NOTE: I am doing this on Amazon Linux and not ubuntu

Check which python version is being used by default

$ python --version

If there are multiple python versions installed on your system, configure your machine to use python3

The following command will ask you to the select the python version you want your machine to point to.

$ update-alternatives --config python

Create a symbolic link for pip/pip3 to point to pip-3.5

I am using Python3.5  so I had to point pip to use pip-3.5.

$ sudo ln -s /usr/bin/pip3.5 /usr/bin/pip

If you need to remove existing symbolic link, just delete /usr/bin/pip

Install httpd

$ sudo yum install httpd-devel

Install mod_wsgi using pip3 command

$ sudo pip3 install --target=/usr/local/lib64/python3.5/site-packages/ mod_wsgi

Add mod_wsgi module to /etc/httpd/modules/

$ sudo ln -s /usr/local/lib64/python3.5/site-packages/mod_wsgi/server/ /usr/lib64/httpd/modules/ 

Create file /etc/httpd/conf.d/wsgi.conf with following contents:

Apache will load wsgi whenever it is started

LoadModule wsgi_module modules/

Restart apache

$ sudo service httpd restart

Configure /etc/httpd/conf/httpd.conf to listen to your host on port 80

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot /var/www/html/projects/
    ServerName dns-address
    ErrorLog <path/to/store/apache_error.log>
    CustomLog <path/to/store/apache_access.log>
    WSGIScriptAlias / /var/www/html/projects/

Restart apache

$ sudo service httpd restart
This entry was posted in Django and tagged , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Django 2.0.2 + Python3 + Apache + mod_wsgi

  1. Yi-Wei Chang says:

    Do you need to modify the permission of the project directories?

    • avnee says:

      If you have any files (eg. database file – db.sqlite) that need to be read/written you need to set 664 permission on those files/folders.

      You also need to make your user under which Apache is running (eg. www-data or apache) own the project directory.

Leave a Reply to Yi-Wei Chang Cancel reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s