How to install Django Python Framework on 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 to help you create secure and scalable web applications. Its main purpose is to facilitate the creation of complex applications and the maintenance of internal structures.

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

precondition

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

Install the required packages

Django is a Python based framework, so you need to have both Python and PIP installed 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.

Django installed

You can install Django using 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 Django version is the 3.0.3

Create a Django project

At this point, Django has been successfully installed. Now is the time to build the Django app.

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

cd /opt
 django-admin startproject djangoproject

After creating a django project, change directory to djangoproject Changes are posted using 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 administrative user account to manage Django project with the following command:

python3 manage.py createsuperuser

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

Username (leave blank to use 'root'): dadmin
Email address: [email protected]
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, and in order to make Django online you must allow Django to external hosts. You can do this by adding the IP address of the server on settings.py:

nano /opt/djangoproject/djangoproject/settings.py

Change the following lines:

ALLOWED_HOSTS = ['ip_server_Anda']

Save and close the file. After that, launch the Django app with the following command:

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

You will see the following result:

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 app is now running on port 8000. You can now move on to the next step.

Configure SELinux and Firewall

Next, you will need to allow ports 8000 and 80 through the firewall. 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 the Django app

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

Install Django on CentOS 8

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

Django management on CentOS 8

Enter the admin username and password and click the button Record in a. You will see the following page:

Django Admin Web Page

Install Nginx and Gunicorn

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

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

systemctl start nginx
 systemctl enable nginx

After that, change the ownership of the directory /opt/djangoproject To Nginx as shown below:

chown -R nginx:nginx /opt/djangoproject

Create a Systemd Service File for Django

Next, create a systemd file service to manage the Django service 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

After that, start the Django service and enable it to start after restarting the system with the following command:

systemctl start django
 systemctl enable django

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

systemctl status django

You will see the following result:

? 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

Nginx configuration for Django

Next, configure Nginx as a reverse proxy for Django. To do this, 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?x61338 { 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 done. Next, test nginx for any syntax errors with the following command:

nginx -t

If there are no errors, the following will appear:

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

After that, 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: // IP_server.

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 configure Nginx as a reverse proxy for serving Django applications.

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *