Table of Contents
- Getting Started
- Install Apache Web Server
- Install Django
- Create Your First Django Project
- Configure Apache for Django
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: 192.168.15.193)
- A non-root user with
sudoprivilege setup on your server.
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.
Before installing Django. You will also need to create the Python virtual environment. So you will need to install
You can install it using the
sudo pip install virtualenv
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
(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@example.com Password: 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', '192.168.15.193', '[::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 0.0.0.0:8989
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
http://192.168.15.193:8989, you should see your first Django page:
You can also access the Django admin page by typing the URL
http://192.168.15.193:8989/admin on your web browser. Enter username
djangoadmin and the password which you have created earlier. You should see the following 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> <Directory /opt/djangoproject/myfirstproject> <Files wsgi.py> Require all granted </Files> </Directory> 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
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
http://192.168.15.193/admin 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.