Django Apache Setup - Django WSGI | W3schools

Till now, in all our modules, we have been using the Django dev web server. But Django dev web server is used for testing purpose only. There will no involvement of the production environment. In such a scenario where you are required to get involved in the production then you should be using real servers such as Apache, Nginx, and several others. Well, this chapter will all be dedicated to Apache server only.

Also Read: Django Template System

Serving Django applications via Apache is performed with the help of mod_wsgi. So, it is important that you pre-install Apache and mod_wsgi before we proceed ahead. We hope you remember the structure of the project. Well, it looks like the following:

myproject/

  manage.py

  myproject/

     __init__.py

     settings.py

     urls.py

     wsgi.py

[post_middile_section_ad]

You can consider the wsgi.py file as the link between Django and Apache.

In here, we will be sharing our already created myproject project with Apache. For that purpose, you need to set the path so that Apache can access the myproject folder. For instance, you can consider the default path of the myproject folder as /var/www/html. Now, to access the project, enter the URL – http://127.0.0.1/myproject. The below screenshot will help you to understand how Apache will list out the folders involved.

django apache setup

You can clearly see that Apache is not involved in any of the Django’s stuff. But, if you want Apache to take care of these kinds of stuff too then you are required to configure Apache in httpd.conf. The httpd.conf file needs to be altered with the following content:

[post_middile_section_ad]

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>

Now enter the given URL – http://127.0.0.1/myapp/connection to get to the login page. The screen will appear as the following:

django apache login page