How to install Django Python Framework on CentOS 8

How to install Django Python Framework on CentOS 8

Django is a free, open source, advanced web framework for developing Python web applications. It comes with a set of tools to help you build secure and scalable web applications. Its main goal is to simplify the creation of complex applications and maintain internal structures.

In this tutorial, we will learn how to install Django on CentOS 8 and configure Nginx as a Django reverse proxy.

prerequisites

  • Server 8 running CentOS.
  • A root password is configured on your server.

Install required packages

Django is a Python-based framework, so you need to install Python and PIP on your system. You can install them by running:

dnf install python36 python3-pip -y

Once both packages are installed, you can move on to the next step.

Install Django

You can install Django using PIP commands as follows:

pip3 install Django

After installing Django, you can check the version of Django using:

django-admin --version

You should see the Django version in the following output:

3.0.3

Create a Django project

At this point, Django is installed. Now it’s time to create a new Django application.

You can create a new Django application using the django-admin command in the / opt directory as follows:

cd /opt django-admin startproject djangoproject

After creating the django project, change directory to djangoproject and use the following command to migrate the changes:

cd djangoproject python3 manage.py migrate

You should get the following output:

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 admin.0003_logentry_add_action_flag_choices... 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 auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

Next, you will need to create an administrator user account for managing Django projects. You can create it using:

python3 manage.py createsuperuser

You will be asked for your username, email and password. You can provide them according to your choice as follows:

Username (leave blank to use 'root'): dadmin
Email address: [email protected]
Password: 
Password (again): 
Superuser created successfully.

When you are done, you can proceed to the next step.

Start the Django application

By default, Django applications cannot be accessed from remote hosts. So you will need to allow Django for external hosting. You can do this by adding the server IP in settings.py:

nano /opt/djangoproject/djangoproject/settings.py

Change the following lines:

ALLOWED_HOSTS = ['your-server-ip']

Save and close the file. Then, use the following command to start the Django application:

cd /opt/djangoproject python3 manage.py runserver 0.0.0.0:8000

You should see the following output:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 03, 2020 - 02:31:19
Django version 3.0.3, using settings 'djangoproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Django application is now started and runs on port 8000. 

At this point, the Django application is now up and running on port 8000. You can now proceed to the next step.

Configure SELinux and firewall

Next, you will need to allow ports 8000 and 80 to pass through Firewalld. You can allow them with:

firewall-cmd --permanent --add-port=8000/tcp firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload

Next, configure SELinux using the following command:

setsebool httpd_can_network_connect on -P

When you are done, you can proceed to the next step.

Accessing Django applications

You can access the Django application by visiting the URL http: // your-server-ip: 8000. You should see the following page:

You can also access the Django admin interface using the URL http: // your-server-ip: 8000 / admin. You should see the following page:

Django admin login

Provide your administrator username, password, and click recording in Button. You should see the following page:

Django admin console

Install Nginx and Gunicorn

In this section, we will install Gunicorn to create and manage Django services, and install Nginx to serve Django applications.

First, install Nginx using the following command:

dnf install nginx -y

Next, install Gunicorn using the PIP command as follows:

pip3 install gunicorn

Once both packages are installed, start the Nginx service and use the following command to start it after the system restarts:

systemctl start nginx systemctl enable nginx

Next, change the ownership of the / opt / djangoproject directory to Nginx as follows:

chown -R nginx:nginx /opt/djangoproject

Create a system service file for Django

Next, use the following command to create a systemd service file for managing Django services:

nano /etc/systemd/system/django.service

Add the following lines:

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

[Service]
User=nginx
Group=nginx
WorkingDirectory=/opt/djangoproject
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:application

[Install]
WantedBy=multi-user.target

Save and close the file, then reload the systemd daemon using the following command:

systemctl daemon-reload

Next, start the Django service and use the following command to start it after the system restarts:

systemctl start django systemctl enable django

You can now check the status of the Django service using:

systemctl status django

You should see the following output:

? django.service - django daemon
   Loaded: loaded (/etc/systemd/system/django.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-02 22:27:51 EST; 3min 32s ago
 Main PID: 960 (django)
    Tasks: 4 (limit: 25028)
   Memory: 95.2M
   CGroup: /system.slice/django.service
           ??960 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a>
           ??964 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a>
           ??965 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a>
           ??966 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a>

Mar 02 22:27:51 centos8 systemd[1]: Started django daemon.
Mar 02 22:27:52 centos8 django[960]: [2020-03-02 22:27:52 -0500] [960] [INFO] Starting django 20.0.4
Mar 02 22:27:52 centos8 django[960]: [2020-03-02 22:27:52 -0500] [960] [INFO] Listening at: unix:/opt/djangoproject/djangoproject.sock (960)
Mar 02 22:27:52 centos8 django[960]: [2020-03-02 22:27:52 -0500] [960] [INFO] Using worker: sync
Mar 02 22:27:52 centos8 django[960]: [2020-03-02 22:27:52 -0500] [964] [INFO] Booting worker with pid: 964
Mar 02 22:27:52 centos8 django[960]: [2020-03-02 22:27:52 -0500] [965] [INFO] Booting worker with pid: 965
Mar 02 22:27:52 centos8 django[960]: [2020-03-02 22:27:52 -0500] [966] [INFO] Booting worker with pid: 966
h pid: 966

Configure Nginx for Django

Next, you need to configure Nginx as a Django reverse proxy. To do this, use the following command to create a new Nginx configuration file:

nano /etc/nginx/conf.d/django.conf

Add the following lines:

server {
    listen 80;
    server_name your-server-ip

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /opt/djangoproject;
    }

    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:/opt/djangoproject/djangoproject.sock;
    }
}

Save and close the file when you are finished. Then, test if there are syntax errors with nginx using:

nginx -t

If everything works, you should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, restart the Nginx service to implement the changes:

systemctl start nginx

You can also verify Nginx with:

systemctl status nginx

You should get the following output:

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-02 22:28:13 EST; 4min 14s ago
  Process: 984 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 982 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 980 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 985 (nginx)
    Tasks: 3 (limit: 25028)
   Memory: 5.5M
   CGroup: /system.slice/nginx.service
           ??985 nginx: master process /usr/sbin/nginx
           ??986 nginx: worker process
           ??987 nginx: worker process

Mar 02 22:28:12 centos8 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 02 22:28:12 centos8 nginx[982]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 02 22:28:12 centos8 nginx[982]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 02 22:28:13 centos8 systemd[1]: Started The nginx HTTP and reverse proxy server.

You can now access the Django application using the URL http: // your-server-ip.

in conclusion

In this guide, we learned how to install Django on CentOS 8. We also learned how to use Gunicorn to create and manage Django services, and how to configure Nginx as a reverse proxy to serve Django applications.

Sidebar