How to Install Django on CentOS 7

how to install django on centos 7

In this tutorial, we will show you how to install Django on CentOS 7. Django is a free and open-source, Python-based web application framework. It is a set of useful components that help developers to easily and quickly create their applications.

how to install django framework on centos 7

Django is built and maintained by experienced developers and used by some of the world’s biggest companies and governments. Also, some of the most popular websites such as Instagram, Mozilla, Pinterest, The Washington Times, and Disqus are using Django. Installing Django on CentOS 7 is a fairly easy task if you follow the steps below carefully.

There are several ways to install Django on a CentOS machine.

  • Install from yum packages
  • Install through pip
  • Install through pip in a virtual environment
  • Install from Git

This time, we will install Django with pip in a virtual environment.

Prerequisites

CentOS 7 based VPS
SSH access with root privileges

Step 1: Login to the server and update

ssh root@IP_Address -p Port_number
yum update

Step 2: Enable EPEL repository

yum install epel-release

Step 3: Install Python and pip

yum install python-devel python-setuptools python-pip
pip install –upgrade pip

Step 4: Install virtual environment

pip install virtualenv

Step 5: Create a virtual environment

We will install a virtual environment on a system user, if you don’t have a system user other than root, please create one first. In this example, we will use a system user called “rose”.

su - rose
cd ~
virtualenv django

The command will create a virtual environment in /home/rose/django

Step 6: Install Django in virtualenv

Still as user “rose”, we are now going to install Django in the virtual environment we created earlier

source ~/django/bin/activate

As you can see on the screen, your SSH terminal prompt is changed

installing django on centos 7

You are now in the virtual environment, let’s proceed with Django installation

pip install django

Step 7: Create a Django project

Django has been installed under user “rose”, now let’s create a project

cd ~
django-admin startproject one

The command will create a project named “one”, and this will also create a “one” directory in /home/rose/
Next, run the following commands to start the new project

cd one
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver 0.0.0.0:8000
guide to installing django on centos 7

When creating the superuser, you will be asked for a password and email, please provide them with the credentials as you wish.

Now, open your favorite web browser and navigate to your IP_address:8000. In this example, we install Django on 192.168.1.231 so we need to go to http://192.168.1.231:8000

how to install django on centos 7

If you see an error message like the following, then you need to edit the ALLOWED_HOSTS value in settings.py file

DisallowedHost at / Invalid HTTP_HOST header: '192.168.1.231'. You may need to add u'192.168.1.231' to ALLOWED_HOSTS.

To edit the ALLOWED_HOSTS value in settings.py file, you can run the following command.

nano ~/one/one/settings.py
ALLOWED_HOSTS = ['192.168.1.231','127.0.0.1','yourdomain.com']

Save the file and exit, then rerun the application again:

python manage.py runserver 0.0.0.0:8000

You can reach Django administration page at http://192.168.1.231:8000/admin, use the credentials you chose when creating Django superuser

To manage the Django application better, we need to install gunicorn. Gunicorn is a python web server gateway interface HTTP server. It is a pre-fork worker model, ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with a number of web frameworks, simply implemented, light on server resources, and fairly fast.

pip install gunicorn
deactivate
exit

Now we will create a systemd service file to start and stop the application server.

nano /etc/systemd/system/gunicorn.service

Then, insert the following lines to the systemd service file.

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=rose
Group=nginx
WorkingDirectory=/home/rose/one
ExecStart=/home/rose/django/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/rose/one/one.sock one.wsgi:application

[Install]
WantedBy=multi-user.target

Save and exit nano, then issue the following command to reload systemd service file.

systemctl daemon-reload

Now, we can start-stop-restart Django application using systemctl command

systemctl start gunicorn

If you want to run it on boot, we need to enable it

systemctl enable gunicorn

Gunicorn has been successfully configured, now if you want to access the application using your domain name and remove the port number in your favorite web browser address bar, we need to install and configure a webserver. This time, we will install and configure nginx to proxy pass to gunicorn.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
yum install nginx

Let’s create an nginx server block file, make sure you change yourdomain.com to your actual domain name.

nano /etc/nginx/conf.d/yourdomain.com.conf
server {
listen 80;
server_name yourdomain.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/rose/one;
}

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/rose/one/one.sock;
}
}

Save and exit once finished.

nginx -t</pre
systemctl start nginx
systemctl enable nginx

To give permissions to nginx to access your Django application, we have to add nginx user to a user group that run Django

usermod -a -G rose nginx

Then, we also need to change the directory permission of Django user’s home.

chmod 710 /home/rose
systemctl restart nginx
how to install django framework on centos 7

That’s it, you should be able to access your Django application at http://yourdomain.com now.

Of course, you don’t have to Install Django on CentOS 7 if you use one of our Django VPS Hosting plans, in which case you can simply ask our expert Linux admins to install Django on your CentOS VPS for you. They are available 24/7 and will take care of your request immediately.

PS. If you liked this post on how to install Django on CentOS 7, please share it with your friends on the social networks or simply leave a reply in the comments sections. Thanks.

Leave a Comment