Django - Apache Setup


Advertisements

So far, in our examples, we have used the Django dev web server. But this server is just for testing and is not fit for production environment. Once in production, you need a real server like Apache, Nginx, etc. Let's discuss Apache in this chapter.

Serving Django applications via Apache is done by using mod_wsgi. So the first thing is to make sure you have Apache and mod_wsgi installed. Remember, when we created our project and we looked at the project structure, it looked like −

myproject/
   manage.py
   myproject/
      __init__.py
      settings.py
      urls.py
      wsgi.py

The wsgi.py file is the one taking care of the link between Django and Apache.

Let's say we want to share our project (myproject) with Apache. We just need to set Apache to access our folder. Assume we put our myproject folder in the default "/var/www/html". At this stage, accessing the project will be done via 127.0.0.1/myproject. This will result in Apache just listing the folder as shown in the following snapshot.

Django Apache Setup

As seen, Apache is not handling Django stuff. For this to be taken care of, we need to configure Apache in httpd.conf. So open the httpd.conf and add the following line −

WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py
WSGIPythonPath /var/www/html/myproject/

<Directory /var/www/html/myproject/>
   <Files wsgi.py>
      Order deny,allow
      Allow from all
   </Files>
</Directory>

If you can access the login page as 127.0.0.1/myapp/connection, you will get to see the following page −

Login Page

Useful Video Courses


Video

Build a Stock Market Web App With Python and Django

Best Seller

39 Lectures 3.5 hours

John Elder

Video

Build A Flashcard Website With Python and Django

36 Lectures 2.5 hours

John Elder

Video

Build a Weather App With Python and Django

28 Lectures 2 hours

John Elder

Video

Build An API With The Django Rest Framework Using Python

Best Seller

20 Lectures 1 hours

John Elder

Video

Build A Dental Website With Python and Django

Most Popular

35 Lectures 3 hours

John Elder

Video

Build Python Django Real Project: Django Web Development

Best Seller

79 Lectures 10 hours

Rathan Kumar

Advertisements