Configure Django and Apache2 on Ubuntu 16

In this article I am going to explain how I installed Django and Apache2 onto Ubuntu 16, I spent the best part of a week researching and compiling my notes from serveral different sources.  I hope this will help some of you.  My setup consists of a virtual machine running a fresh install of Ubuntu 16 and Apache2 and a Django project created in Python version 3.6.6.  Their is no guarantee that this guide will work for everyone, but if you do need help please get in contact and I shall do my best to assit.

Ubuntu 16.04 does not come with the version of Python that we need, so we need to add a new repository in order to install Python 3.6.8.

Once we have added the PPA as shown above we can now install the software that we need, issue the below commands one at a time.

The next part is to install pip version 3.6, for this we issue the following commands one at a time.

we will now create some symbolic links so that when we issue python3 it will point to our python3.6 installation, you may get an error saying that they exists, this is ok.

Now we need to install mod-wsgi for Apache2 this will allow our Python code to work with Apache2, issue the below commands

Uploading Project Files to Server

we have now installed the software needed, we now need to upload our project files to the server.  I am on a Windows pc so I used WinScp to upload my project files to my Linux server within my home directory.  Once you have uploaded your project files we need to give permisisons to Apache2 on some of the files,  replace MyBlog  with your project name.

we now need to modify your projects file, in the ALLOWED_HOSTS = [] you will need to put your server IP in or domain name in. towwards the bottom of this file you will need to add the following.

Now we create our Python Virtual Envioment, CD into your project directory.

Creating the Virtual Host

We are now at the point where we can create the virtual host for our Django Project.

within this file add the following between <VirtualHost> remember to change your user and project name

we can now enable our site

Install mod_wsgi

get the load path

now we need to update the wsgi.load like so, you will need to copy the LoadModule output from the above command.

we can now restart Apache2

Installing Project Dependencies

we now need to install our project dependencies, by running the following commnad from within our project, and collecting the static files

if all being well you should now be able to navigate to your site










Leave a Reply

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