Home / How To / How to install Django Python Framework on CentOS 8

How to install Django Python Framework on CentOS 8



Django is a free, open source and high-level web framework used to develop Python web applications. It comes with a set of tools to help you build secure and scalable web applications. Its main goal is to facilitate the creation of complex applications and take care of the internal structure.

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

Prerequisites

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

Install mandatory packages

Django is a Python-based framework so you need to install Python and PIP in your system. You can install them by running the following command:

  dnf install python36 python3-pip-y 

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

Install Django

You can install Django with PIP command as shown below:

  pip3 install Django 

After installing Django, you can check the version of Django with the following command:

  django-admin - version 

You should see the Django version in the following output:

  3.0.3

Creating 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 with the django-admin command in the / opt directory as shown below:

  cd / opt 
django-admin start project djangoproject

Once the django project has been created, change the directory to djangoproject and migrate the changes with the following command:

  cd djangoproject 
python3 manage.py migrate

You should get the following output: [1

9659016] Steps to perform:
Apply all migrations: admin, authorization, content types, sessions
Run Migrations:
Applying content types.0001_initial … OK
Applying autor.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 content types.0002_remove_content_type_name … OK
Apply authentic.0002_alter_permission_name_max_length … OK
Apply authentic.0003_alter_user_email_max_length … OK
Apply authentic.0004_alter_user_username_opts … OK
Apply authentic.0005_alter_user_last_login_null … OK
Apply autor.0006_require_contenttypes_0002 … OK
Apply authentic.0007_alter_validators_add_error_messages … OK
Apply authentic.0008_alter_user_username_max_length … OK
Apply authentic.0009_alter_user_last_name_max_length … OK
Apply authent.0010_alter_group_name_max_length … OK
Apply autor.0011_update_proxy_permissions … OK
Apply sessions.0001_initial … OK

Then you have to create an administrator account to manage the Django project. You can create it with the following command:

  python3 manage.py createuperuser 

You will be asked to enter your username, email address and password. You can specify them according to your choice as shown below:

  Username (leave blank to use & # 39; root & # 39;): dadmin
Email Address: [email protected]
Password:
Password (again):
The super user was successfully created.

When you're done, you can move on to the next step.

Starting Django application

By default, the Django application cannot be accessed from remote hosts. So you have to allow Django for external hosts. You can do this by adding your server IP to settings.py: vud19659008achtenano /opt/djangoproject/djangoproject/settings.py Chapter19659009vardChange the following line:

  ALLOWED_HOSTS = ['your-server-ip']

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

  cd / opt / djangoproject 
python3 manage.py runserver 0.0.0.0:8000 Chapter19659009 ?? You should see the following output:

  View file changes with StatReloader
Performing system checks ...

System control did not identify any problems (0 silent).
03 March 2020 - 02:31:19
Django version 3.0.3 with settings & # 39; djangoproject.settings & # 39;
Launch the development server at http://0.0.0.0:8000/
Exit the server with CONTROL-C.

The Django application has now been started and run on port 8000.

Right now the Django application starts and runs on port 8000. You can now proceed to the next step.

Configuring SELinux and Firewall

Next, you must 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 - load

Then configure SELinux with the following command:

  setsebool httpd_can_network_connect on -P 

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

Go to the Django application

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

 Django application

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

 Django admin login

Enter your admin username, password and click Log in the button. You should see the following page:

 Django admin dashboard

Installing Nginx and Gunicorn

In this section we will install Gunicorn to create and manage the Django service and Nginx to serve the Django application . [19659002] First install Nginx with the following command:

  dnf install nginx-y 

Then install the Gunicorn with the PIP command shown below:

  pip3 install gunicorn 

When both packages are installed, start Nginx service and enable it after restarting the system with the following command:

  systemctl start nginx 
systemctl enable nginx

Then change the ownership of the / opt / djangoproject directory to Nginx as shown below: [19659008] chown -R nginx: nginx / opt / django project

Creating a Systemd service file for Django

Then create a systemd service file for handling the Django service with the following command:

  nano / etc / systemd / system / django .serviceebrit19659009 ?? Add the following ng lines: 

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

[Service]
User = nginx
Group = nginx
WorkingDirectory = / opt / django project
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 system daemon with the following command:

  systemctl daemon-reload 

Then start the Django service and enable it to start after system startup 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 should see the following output:

? django.service - django daemon
Loaded: loaded (/etc/systemd/system/django.service; disabled; vendor setting: disabled)
Active: active (running) since Mon 2020-03-02 22:27:51 EST; 3min 32s ago
Main PID: 960 (django)
Details: 4 (limit: 25028)
Memory: 95.2 M
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>

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

Configure Nginx for Django

Then you must 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.confebrit19659009??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 / django project;
}

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 done. Then test nginx for all syntax errors with the following command:

  nginx -t 

If all goes well you should get the following output:

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

Then restart the Nginx service to implement the changes:

  systemctl start nginx 

You can also verify Nginx with the following command:

  systemctl status nginx 

You should get the following output: [19659069]? nginx.service - Nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor setting: disabled)
Active: active (running) since Mon 2020-03-02 22:28:13 EST; 4min 14s ago
Process: 984 ExecStart = / usr / sbin / nginx (code = terminated, status = 0 / SUCCESS)
Process: 982 ExecStartPre = / usr / sbin / nginx-t (code = terminated, status = 0 / SUCCESS)
Process: 980 ExecStartPre = / usr / bin / rm -f /run/nginx.pid (code = terminated, status = 0 / SUCCESS)
Main PID: 985 (nginx)
Details: 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

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

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

Conclusion

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


Source link