Django Web Hosting in Apache Server

In this post, we’ll discuss how to host a Django application in Apache Web server. Django has become my favorite web development tool in recent times, it provides a lot of tools that help develop an application in the shortest amount of time possible. However, deploying the Django application can be a bit tricky, so let’s get started with how to host Django web application in an Apache server running on Ubuntu.

django-web-hosting-apache-server
Django web hosting in Apache

Prerequisites: (We’ll discuss these in detail later in the tutorial)
1. Your Django Web Application
2. Apache web server
3. Django Installed
3. Python installed in your web server
4. mod_wsgi installed for Apache
5. Mysqlclient or mysql-connector-python (If database is mysql) 
Optional: Virtual Environment
Note: We will also go through the checklist for Django Web Server Hosting so that you can verify your configuration matches the required configuration.

Hosting Django Web Application in Apache Server

1. Assuming your Django application is ready, we will transfer it to the required server. For the sake of this tutorial, we will assume the root directory of the project (where manage.py is located) is root/django-app and settings.py,wsgi.py are located in root/django-app/django-app/.
If you don’t have Apache installed, you can install it using: apt install apache2

2. If you don’t already have python installed in you server, then go ahead and install it.
Note that your django application’s python version, server’s python version and the version of mod_wsgi python must be matching. In the current date, you are most probably using Django version > 1.5 and probably using python > 3.
So, for python > 3 compatible set up, install required component using the following commands:
Python: apt install python3
Django: apt install django==<your-django-project-version>
mod_wsgi: apt install libapache2-mod-wsgi-py3

If you need to set up for python 2.xx versions, then:
Python: apt install python
Django: apt install django==<your-django-project-version>
mod_wsgi: apt install libapache2-mod-wsgi

3: If you have mysql as the backend database for your application, then you need to install a mysqlclient. So install mysqlclient using:
apt install mysqlclient

If you have problems installing mysqlclient, then install mysql-connector:
apt install mysql-connector-python and then set the database engine in settings.py to  mysql.connector.django. Also change the other details to match the credentials of your live database.

4. We have already installed mod_wsgi in step 2, now we just create a virual-host for our web application and use the power of wsgi to serve requests to our Django app.
Create your website’s virtual-host or edit and add the following:

        ServerName djangosite.com
        ServerAlias www.djangosite.com
        ServerAdmin webmaster@localhost

        DocumentRoot /root/django-site

        <Directory /root/django-site>
                Require all granted
        </Directory>

        <Directory /root/django-site/django-site>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess daemon1 python-path=/root/django-site
        WSGIProcessGroup daemon1
        WSGIScriptAlias / /root/django-site/django-site/wsgi.py
        WSGIPassAuthorization On

Note that we are assuming all the required dependencies are installed for system wide  users and not in a virtual environment. If you have installed the required dependencies in virtual-environment, then you will need to add a python-home option with WSGIDaemonProcess like this:
WSGIDaemonProcess daemon1 python-path=/root/django-site python-home=/root/your-venv-path

We are done, restart the Apache server and your Django web application is ready to be served! and yeah a gentle reminder, set Debug = False in settings.py when you go live, don’t let everybody see those nasty error reports!
I have had a lot of trouble with deploying Django apps in server in the past, the most common problem was version mismatch. So make sure your Python version, django version, mod_wsgi version are all compatible with one another. Cheers! Happy coding.

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Sharing is caring!

3 thoughts on “Django Web Hosting in Apache Server”

  1. Hello, Dear ladies! The best Escort agency offer adult erotic sex escort Job as a Escorts Girl Job in Noida. Dear girls, welcome to our agency who provide you the best Female Escorts Job in Noida which exist from many year! We are escort agency, but we work with different areas in India with luxury worldwide models. If you really want and serious about what we offer like: sex love Call Girls job in Noida?

    The number of clients and works depends on the appearance of the girl, services, the quality of real photographs and selfies, and of course on a positive attitude as Escorts Girl Job in Noida. We have the highest prices in Delhi escort and the highest percentage for girls. In escort companions in the Delhi, Russian girls have 60% of the income from the transaction, for example for an hour it is 15000-30000 Rs. so Join as a Female Escorts Job in Noida. We are also open for Call Girls job in Noida.

Leave a Comment

Your email address will not be published.