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.
I'm completly lost, where do i run this "mod_wsgi-express module-location"
ReplyDeletethanks for this excellent article
ReplyDeletepython Training in Bangalore | Python Training institute in Bangalore
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.
ReplyDeleteAmazing write-up!!!Useful posts,Thanks for sharing.
ReplyDeletepython training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery
This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me.
ReplyDeleteQlikView Online Training
QlikView Classes Online
QlikView Training Online
Online QlikView Course
QlikView Course Online
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.
ReplyDeleteBig Data Online Training
Big Data Classes Online
Big Data Training Online
Online Big Data Course
Big Data Course Online
After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
ReplyDeleteMulesoft certification Online Training in bangalore
Mulesoft certification courses in bangalore
Mulesoft certification classes in bangalore
Mulesoft certification Online Training institute in bangalore
Mulesoft certification course syllabus
best Mulesoft certification Online Training
Mulesoft certification Online Training centers
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
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.
ReplyDeleteangular 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
Thanks for your informative article,Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
ReplyDeleteoracle training in chennai
oracle training in porur
oracle dba training in chennai
oracle dba training in porur
ccna training in chennai
ccna training in porur
seo training in chennai
seo training in porur
This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me..
ReplyDeletesap training in chennai
sap training in velachery
azure training in chennai
azure training in velachery
cyber security course in chennai
cyber security course in velachery
ethical hacking course in chennai
ethical hacking course in velachery
After reading your article I was amazed. I know that you explained it very well. Keep Sharing.
ReplyDeletejava training in chennai
java training in annanagar
aws training in chennai
aws training in annanagar
python training in chennai
python training in annanagar
selenium training in chennai
selenium training in annanagar
Great article with excellent information, found very impressive and helpful thank you
ReplyDeletetypeerror nonetype object is not subscriptable
Fantastic blog with unique content and information provided was very valuable waiting for next blog update thank you .
ReplyDeleteEthical Hacking Course in Bangalore 360DigiTMG
Excellent blog information shared was very informative looking forward for next blog thank you.
ReplyDeleteData Analytics Course Online
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.
ReplyDeleteunindent does not match any outer indentation level
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
ReplyDeleteunindent does not match any outer indentation level python
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.
ReplyDeleteCyber Security Course in Bangalore
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!
ReplyDeleteCyber Security Training in Bangalore
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
ReplyDeletesoftware testing training in chennai
robotic process automation rpa training in chennai
blockchain training in chennai
devops training in chennai
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.
ReplyDeletePPC Company in Delhi
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.
ReplyDeleteData Science Course in Bangalore
Appslure is an award-winning mobile app development company building feature-packed and interactive mobile applications for startups, medium and large enterprises.
ReplyDeleteI 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.
ReplyDeleteArtificial Intelligence course in Chennai