How to Install Django with Apache on CentOS 7

Table of Contents


Django is a free and open-source high-level web framework written in python that can be used for developing dynamic websites and web applications. Django allows you to build a python web application easily and quickly. Django is used by many large companies including Mozilla, Discuss, Pinterest, Instagram, and some government organizations.

Django provides many notable features:

  • Sites framework feature allows to run multiple websites with their own content.
  • Provides built-in mitigation to assist in protecting your application from SQL injection, password cracking, cross-site scripting and other web attacks.
  • Helps you to create your web applications quickly.
  • Includes a simple, lightweight, and standalone server that can be used for testing purpose.

In this tutorial, we will learn how to install Django with Apache on CentOS 7 server.


  • A server running CentOS 7.
  • A static IP address configured on your server. (This tutorial uses:
  • A non-root user with sudo privilege setup on your server.

Getting Started

Before starting, update your system with the latest version by running the following command:

sudo yum update -y

Once your system updates complete, you can proceed to the next step.

Install Apache Web Server

You will need to install the EPEL repository on your server. You can easily install the EPEL repo using the following command:

sudo yum install epel-release -y

After installing EPEL repository, you will need to install Apache and other required packages.

You can install all of this with the following command:

sudo yum install python2-pip httpd mod_wsgi -y

Once installation is completed, you can proceed to the next step.

Install Django

Before installing Django. You will also need to create the Python virtual environment. So you will need to install virtualenv first.

You can install it using the pip command:

sudo pip install virtualenv

Once virtualenv is installed, create a project directory for Django:

sudo mkdir /opt/djangoproject

Create a Python virtual environment with the following commands:

cd /opt/djangoproject
sudo virtualenv djangoprojectenv

Enable the virtual environment to install packages into the isolated environment:

sudo source djangoprojectenv/bin/activate

Install Django using pip command in the djangoprojectenv shell:

(djangoprojectenv) [root@centOS-7 djangoproject]# pip install django

Once installation is finished, you can verify the Django version with the following command:

(djangoprojectenv) [root@centOS-7 djangoproject]# django-admin --version

You should see something similar to:


Create Your First Django Project

Once Django is installed in your directory, create your first Django project using the following command:

(djangoprojectenv) [root@centOS-7 djangoproject]#django-admin.py startproject myfirstproject .

Next, you will need to modify settings.py file:

(djangoprojectenv) [root@centOS-7 djangoproject]#nano myfirstproject/settings.py

Add the following line at the end of the file:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Save the file when you are finished.

Transfer the database of your project to the SQLite database using the following command:

(djangoprojectenv) [root@centOS-7 djangoproject]#./manage.py migrate


Operations to perform:
 Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

Create a superuser for Django:

(djangoprojectenv) [root@centOS-7 djangoproject]#./manage.py createsuperuser

Answer all the questions when prompted:

Username (leave blank to use 'root'): djangoadmin
Email address: email@domain.tld
Password (again):
Superuser created successfully.

Collect all of the static content into the directory location we configured earlier by running:

(djangoprojectenv) [root@centOS-7 djangoproject]#./manage.py collectstatic

You will also need to add your server's IP address to request.py file, if you want to access Django from remote machine.

(djangoprojectenv) [root@centOS-7 djangoproject]#nano djangoprojectenv/lib64/python2.7/site-packages/django/http/request.py

Add your server IP address as shown below:

 allowed_hosts = ['localhost', '', '[::1]']

Save and close the file when you are finished.

Test your Django project by running the following command:

(djangoprojectenv) [root@centOS-7 djangoproject]#./manage.py runserver

Temporarily allow access to port 8989 via firewalld by running:

firewall-cmd --zone=public --add-port=8989/tcp

Open your web browser and type the URL, you should see your first Django page:

Django first page

You can also access the Django admin page by typing the URL on your web browser. Enter username djangoadmin and the password which you have created earlier. You should see the following page:

Django admin login page

Django admininstration page

If you want to exit from your virtual environment, run the following command:

(djangoprojectenv) [root@centOS-7 djangoproject]# deactivate

Configure Apache for Django

After creating a Django project, you will need to configure Apache web server for Django. You can do this by creating new configuration file:

sudo nano /etc/httpd/conf.d/django.conf

Add the following lines:

Alias /static /opt/djangoproject/static
<Directory /opt/djangoproject/static>
 Require all granted

<Directory /opt/djangoproject/myfirstproject>
   <Files wsgi.py>
       Require all granted

WSGIDaemonProcess myfirstproject python-path=/opt/djangoproject:/opt/djangoproject/djangoprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myfirstproject
WSGIScriptAlias / /opt/djangoproject/myfirstproject/wsgi.py

Save and close the file when you are finished, then restart Apache service and enable it to start at boot:

sudo systemctl restart httpd
sudo systemctl enable httpd

Allow access to port 80 via firewalld:

firewall-cmd --zone=public --permanent --add-port=80/tcp

Set proper ownership so httpd has permission to use the Django project directory:

sudo chown -R apache:apache /opt/djangoproject

You can access your Django site by typing the URL on your web browser without starting any service or specifying any port.


Congratulations! You have successfully installed Django with Apache on a CentOS 7 server. You can now move forward with creating your own web application using Django. This tutorial was a very basic introduction to getting Django up and running. Please be aware that the Django server should be secured properly (using TLS) before bringing it into production. Please comment below if you have any questions or ran into an issue following the tutorial.

  • Thank you very much for the article. Could you please post an article to install Django with Apache on OpenSUSE?

    Thanks, Rashmi

  • Hi there, thank you for really nice tutorial. It is clear everything but i found some problems which I don't know how to solve. I tried to configure server that I can visit webiste directly with IP address or host name without port number as said.

    I added this conf file and i corrected all paths: "/opt/djangoproject/static" to "django/project/static" etc.

    Alias /static /opt/djangoproject/static <br> <Directory /opt/djangoproject/static> <br> Require all granted <br> </Directory><br> <br> <Directory /opt/djangoproject/myfirstproject><br> <Files wsgi.py><br> Require all granted<br> </Files><br> </Directory><br> <br> WSGIDaemonProcess myfirstproject python-path=/opt/djangoproject:/opt/djangoproject/djangoprojectenv/lib/python2.7/site-packages<br> WSGIProcessGroup myfirstproject<br> WSGIScriptAlias / /opt/djangoproject/myfirstproject/wsgi.py

    httpd was restarted, port is allowed and ownership is also set, but nothing happens when i visit vps url as mentioned.

    Any idea what i did wrong?

    ps. my directory /django/ is in root. <br> root<br> - django<br> - ...<br> - etc<br> - ....<br>

  • I am having the same issue as the above poster kregg. If there is a fix or another way to get things working correctly please respond.

  • Hello D1000 and krregg,

    I would check the output of the command firewall-cmd --list-ports, it is possible that you don't actually have port 80 listening. The tutorial said to run firewall-cmd --zone=public --permanent --add-port=80/tcp which will add port 80 to the list, but it doesn't happen until firewalld is restarted. You can run the command without --permanent to have it apply immediately.

    firewall-cmd --zone=public --add-port=80/tcp

    Another item that could cause issues is your SELinux settings. If sestatus is showing enforcing, then you will probably get an error when trying to log into the django admin because Apache doesn't have write access to a directory it is trying to access. The correct way to fix this would be to adjust the security context. For the purposes of the tutorial, you can temporarily get around it by running 'setenforce 0'.


Log In, Add a Comment