BLOG

A LITTLE STORY ABOUT MY JOURNEY

Deploy Django on Apache with Virtualenv and mod_wsgi

I have recently got into Django and built a small project with it which is this blog. Then came the time for me to deploy the project on an unmanaged VPS, the process went fine yet with a few problems here and there due to the lack of a definitive guide to deployment. So I ended up checking several sources to have everything work the way I wanted. In this post, I’ll be sharing the whole process with you in detail and what will hopefully be helpful to the fellow newcomers.

Create a virtual host (Ubuntu)

Open up or create the virtualhost file for your domain

Create a virtual host (Mac)

Open up or create the virtualhost file for your domain

Create your new virtual host node which should look something like this

enable Site : a2ensite  mydomain.conf

A couple of things to notice here. WSGIScriptAlias denotes the location of wsgi file that is going to get executed when our domain gets accessed.This file will have some Python code which will run our Django app as you’ll see later. The other alias denotes the location of the static files for my django project. This contents of the static folder will contain all static files belonging to all the apps within my django project. The content will later be generated via the collectstatic command. You will also notice that I have put those files away from my home folder. /Users/kishor/Virtualenvs/alex/project/ it just a convention which I prefer especially for static content being under the “document root”.

Create the wsgi file

Now that apache points to /Users/kishor/Virtualenvs/alex/project/topclub_test/infoportal/wsgi.py  we need to create that file.

Your Python code in that file should be similar to this

site.addsitedir() will let you use the site packages and dependencies within the virtualenv you created initially for your specific project with your specific packages. sys.path.append() adds your project’s directory to the Python path. It is advised you add both the main directory holding your apps and its child that contains the settings.py file which in this case is “infoportal”.

Static files

Now we need to set our static files directory and generate the static files. Navigate to your settings.py file in your project and update the following settings:

Make sure that you have created the static folder. Now within MyProject execute collectstatic

Everything should be set now. Restart apache for the changes on your virtual host to take effect and everything should work well.

Works fine …… 😀

 

551 Total Views 1 Views Today