Now you can Subscribe using RSS

Submit your Email

Wednesday

Django Web Hosting in Apache Server

Vishal Shrestha
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.

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

WORK

Research and Development at : Ekghanti

Mobile Technology Lead at : Vimvox Lab

For Business info : My Portfolio Site

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha