Install Django Python Framework on Rocky Linux 8 / CentOS 8

Django is a free and open-source high-level web framework used to develop Python Web Applications. Django comes with a set of tools that help you to build secure and scalable web applications. Its main purpose is to facilitate the creation of complex applications and maintain internal structure.

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

Precondition

  • Server with CentOS 8 operating system.
  • Log in as root or user with privileges sudo

Install Required Packages

Django is a Python based framework, so you need to install Python and PIP on your system. To be able to install it, run the following command:

dnf install python36 python3-pip -y

Once both packages are installed, proceed to the next step.

Install Django on Rocky Linux 8

You can install Django with the PIP command as shown below:

pip3 install Django

After installing Django, check the Django version with the following command:

django-admin --version

You will see the Django version in the following output:

3.0.3
 

at the time of writing this article, the version of Django is version 3.0.3

Create a Django Project

At this point, Django has been successfully installed. Now, it’s time to build a Django app.

You can create Django applications using the django-admin command in the directory /optas shown below:

cd /opt
 django-admin startproject djangoproject

Once the django project is created, change the directory to djangoprojectand migrate changes with the following command:

cd djangoproject
 python3 manage.py migrate

You will 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 need to create an admin user account to manage your Django project with the following command:

python3 manage.py createsuperuser

You will be asked to provide your username, email and password. You can provide it as per your choice as shown below:

Username (leave blank to use 'root'): dadmin
Email address:  admin@example.com 
Password: 
Password (again): 
Superuser created successfully.
 

Once done, you can move on to the next step.

Start the Django App

By default, Django applications can only be accessed from localhost only, to make Django connect to the internet, you must allow Django for external hosts. You can do this by adding your server IP in IP settings.py:

nano /opt/djangoproject/djangoproject/settings.py

Change the following line:

ALLOWED_HOSTS = ['ip_server_Anda']
 

Save and close the file. Then, run the Django application with the following command:

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

You will 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 move on to the next step.

Configure SELinux and Firewall on Rocky Linux 8

Next, you have to allow ports 8000 and 80 through firewalld. You can allow them with the following command:

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

Next, configure SELinux with the following command:

setsebool httpd_can_network_connect on -P

Once done, you can move on to the next step.

Access Django Applications

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

Install Django of CentOS 8

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

Provide the admin username, password and click the button Log  in . You will see the following page:

Install Nginx and Gunicorn on Rocky Linux 8

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

First, install Nginx with the following command:

dnf install nginx -y

Next, install Gunicorn using the PIP command as shown below:

pip3 install gunicorn

Once both packages are installed, start the Nginx service and enable it to start after system reboot with the following command:

systemctl start nginx
 systemctl enable nginx

Next, change directory ownership /opt/djangoprojectto Nginx as shown below:

chown -R nginx:nginx /opt/djangoproject

Create File Systemd Service For Django

Next, create a systemd service file to manage Django services with the following command:

nano /etc/systemd/system/django.service

Add the following line:

[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 with the following command:

systemctl daemon-reload

Next, start the Django service and enable it to start after system reboot with the following command:

systemctl start django
 systemctl enable django

You can now check the status of the Django service with the following command:

systemctl status django

You will 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 UTC; 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, configure Nginx as a reverse proxy for Django. To do so, create a new Nginx configuration file with the following command:

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

Add the following line:

server {
    listen 80;
    server_name your-server-ip

    location =https://cdn.linuxid.net/favicon.ico?x87109 { 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’re done. Then, test nginx for any syntax errors with the following command:

nginx -t

If there are no errors, the following output will appear:

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 the following command:

systemctl status nginx

You will 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 Django applications using the URL http://server_IP_address.

Conclusion

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

Be the first to comment

Leave a Reply

Your email address will not be published.


*