How to deploy a django project with Apache

In this guide, we will talk about how to deploy a Django project using Apache and its module, mod_wsgi. This two have a main role in the deploying process.
We will handle the client requests using Apache, which will be the one of delegating the requests that need logic to the Django app through mod_wsgi.

Setting a virtual environment

First, we will install pip
$ sudo easy_install pip
Next, we need to install the virtualenv command to create these environments.
$ sudo pip install virtualenv
When virtualenv gets installed we can create a Python virtual environment by typing:
$ virtualenv exampleve
Then, to be able to use it, we will have to activate it:
$ source exampleve/bin/activate
Once we activate our virtual environment we are ready to add our Django project. All the dependencies we install will be located in our "exampleve" folder.

Setting static files directives

Once our Django project is created, we need to specify the directory for both static and media files on the settings project and then generate them. First, we are going to specify the directory where the files are going to be generated. We will do that by editing the settings.py file from our project as follows:
import os


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Next, we are going to collect our static files by typing the following command:
$ python manage.py collectstatic
A folder with the static files should show up on our project

Configuring mod_wsgi

Once we have finished developing Django project we are ready to deploy it. First, we need to install the apache module mod_wsgi. If you are on MAC, be sure that XCode is updated. If you are on Linux (in this guide we show how to do it specifically in Debian distributions), be sure that apache is installed. You can install it by typing the following command:
Linux:
sudo apt-get update & sudo apt-get install apache2
Then we are going to install mod_wsgi. We will use pip for that:
$ sudo pip install mod_wsgi
Once it gets installed we will need to include it into apache’s modules by manually editing the httpd.conf file located in /etc/apache2/httpd.conf.
To do that we are going to run:
mod_wsgi-express module-location
Take the path that outputs and modify Apache configuration (httpd.conf) so it has a LoadModule line for wsgi_module with that path. Here is an example of the line we have to include:
LoadModule wsgi_module /Library/Python/2.7/site-packages/mod_wsgi/server/mod_wsgi-py27.so
Then we can check if mod_wsgi is successfully loaded by looking into Apache's loaded modules:
$ apachectl -t -D DUMP_MODULES | grep wsgi
wsgi_module should show up on the output.
Once we get mod_wsgi installed and activated, we have to take a look at our Apache server’s httpd.conf file. At the end of the file, we can see that it imports all the .conf files from “/etc/apache2/other/”. To take advantage of this, we will create a new file there, which can be called django.conf for example, with the following configuration:
WSGIScriptAlias / /path/to/wsgi.py
 
WSGIDaemonProcess example python-home=/path/to/virtualenviroment/ python-path=/path/to/project/
WSGIProcessGroup example

<directory>
     <files wsgi.py="">
        Require all granted
     </files>
</directory>   

   Alias "/static/" "/Users/andres/Desktop/example/static/"

   <directory>
     Require all granted
     Options +Indexes
   </directory>
  • WSGIScriptAlias indicates the path to the directory to our wsgi.py file on our Django project
  • WSGIDaemonProcess indicates the path to our virtual environment and the path to our project. Both grouped under the WSGIProcessGroup name
Finally, we just have to restart the apache server one more time to implement the changes made and that’s it!
$ sudo apachectl restart

Congratulations! 

Everything should be set now. Go to see your Django project working.
Mountain View
If you want a sample project you can check the link below where you have the default Django project.

Comments

  1. I'm completly lost, where do i run this "mod_wsgi-express module-location"

    ReplyDelete
  2. Attend The Best Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Best Python Training in Bangalore.

    ReplyDelete
  3. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me.

    QlikView Online Training

    QlikView Classes Online

    QlikView Training Online

    Online QlikView Course

    QlikView Course Online

    ReplyDelete
  4. Very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    angular js training in chennai

    angular js training in tambaram

    full stack training in chennai

    full stack training in tambaram

    php training in chennai

    php training in tambaram

    photoshop training in chennai

    photoshop training in tambaram

    ReplyDelete
  5. Great article with excellent information, found very impressive and helpful thank you
    typeerror nonetype object is not subscriptable

    ReplyDelete
  6. Fantastic blog with unique content and information provided was very valuable waiting for next blog update thank you .
    Ethical Hacking Course in Bangalore 360DigiTMG

    ReplyDelete
  7. Excellent blog information shared was very informative looking forward for next blog thank you.
    Data Analytics Course Online

    ReplyDelete
  8. I will very much appreciate the writer's choice for choosing this excellent article suitable for my topic. Here is a detailed description of the topic of the article that helped me the most.
    unindent does not match any outer indentation level

    ReplyDelete
  9. I'm glad I found this blog! Occasionally, students want to know the keys to writing productive literary essays. Your first-class knowledge of this great job can become a suitable foundation for these people. Good
    unindent does not match any outer indentation level python

    ReplyDelete
  10. Nice Information Your first-class knowledge of this great job can become a suitable foundation for these people. I did some research on the subject and found that almost everyone will agree with your blog.
    Cyber Security Course in Bangalore

    ReplyDelete
  11. Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
    Cyber Security Training in Bangalore

    ReplyDelete
  12. ice Information Your first-class knowledge of this great job can become a suitable foundation for these people. I did some research on the subject and found that almost everyone will agree with your blog.salesforce training in chennai

    software testing training in chennai

    robotic process automation rpa training in chennai

    blockchain training in chennai

    devops training in chennai

    ReplyDelete
  13. It’s very helpful for us, thank you so much for sharing such an amazing article. Visit Ogen Infosystem for top Website Designing and PPC Services in Delhi at an affordable price.
    PPC Company in Delhi

    ReplyDelete
  14. I was very happy to find this site. I really enjoyed reading this article today and think it might be one of the best articles I have read so far. I wanted to thank you for this excellent reading !! I really enjoy every part and have bookmarked you to see the new things you post. Well done for this excellent article. Please keep this work of the same quality.
    Data Science Course in Bangalore

    ReplyDelete
  15. Appslure is an award-winning mobile app development company building feature-packed and interactive mobile applications for startups, medium and large enterprises.

    ReplyDelete
  16. I really enjoy every part and have bookmarked you to see the new things you post. Well done for this excellent article. Please keep this work of the same quality.
    Artificial Intelligence course in Chennai

    ReplyDelete

Post a Comment