How To Install Django With NGINX, Gunicorn, and PostgreSQL On Ubuntu - Porntok - Xyz
How To Install Django With NGINX, Gunicorn, and PostgreSQL On Ubuntu - Porntok - Xyz
1 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
In this article we’ll learn how to install Django with NGINX, Gunicorn, and
PostgreSQL on Ubuntu.
It’s intended to be only used for development purposes so you can rapidly
focus on your application development without having to worry about the
production server. Similarly, if you are deploying an application in
production, it is recommended to use PostgreSQL (or some other popular
database server).
2 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Before we install all the packages, update your apt package manager
index by executing the following a pt command with s u d o :
3 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Now that we have all the required packages installed, we can proceed to
further steps.
4 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Django would work fine without these parameters but, it will require
making additional queries, so Django recommends doing it in hand
before. Now grant all the privileges of the djangoproject database to the
djangouser by entering:
mkdir ~/newProject
5 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
cd ~/newProject
source django_venv/bin/activate
And your shell prompt should change as shown in the image below.
6 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
The period at the end of the command will create the project in the same
directory.
nano djangoproject/settings.py
1 . . .
2 ALLOWED_HOSTS = ['your_server_domain_or_ip', 'your_server_domain_or_ip', . . .
3 . . .
7 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
1 . . .
2 DATABASES = {
3 'default': {
4 'ENGINE': 'django.db.backends.postgresql_psycopg2',
5 'NAME': '<db_name>',
6 'USER': '<db_user>',
7 'PASSWORD': '<db_password>',
8 'HOST': 'localhost',
9 'PORT': '',
10 }
11 }
12 . . .
8 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
import os
. . .
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
. . .
9 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Migrating Database
You are required to create an administrative user to use the Django web
admin interface, execute the following command to create:
10 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Password).
Create Superuser
Now allow port 8 0 0 0 in the firewall for the Django Development Server to
be accessible, by executing:
Finally, we can test our new project with the help of the Django
Development Server by executing:
11 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
12 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
To test out Gunicorn’s ability to serve pages with your Django application,
execute:
13 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Then make the following changes in the gunicorn.service file, and make
sure to replace your details below:
1 [Unit]
2 Description=gunicorn daemon
3 After=network.target
4
5 [Service]
6 User=myusername
7 Group=www-data
8 WorkingDirectory=/home/myusername/newProject
9 ExecStart=/home/myusername/newProject/django_venv/bin/gunicorn --access-logfi
10
11 [Install]
12 WantedBy=multi-user.target
14 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
We have Gunicorn set up, now we need to set up NGINX reverse proxy to
Gunicorn.
15 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
And then add the following and replace them with your details:
1 server {
2 listen 80;
3 server_name your_server_domain_or_IP;
4
5 location = /favicon.ico { access_log off; log_not_found off; }
6 location /static/ {
7 root /home/myusername/newProject;
8 }
9
10 location / {
11 include proxy_params;
12 proxy_pass http://unix:/home/myusername/newProject/djangoproject.sock
13 }
14 }
16 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
sudo nginx -t
You can now close port 8000 and allow NGINX ports, by executing:
17 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Django Webpage
Congratulations! We have successfully set up Django with NGINX,
Gunicorn, and PostgreSQL.
Conclusion
Django is a great Python web framework for developing web applications,
it comes with a handy development server that shall never be used in a
production environment. In production environments, a setup similar to
what we have shown above must be used. In this article, we have set up a
Django application with PostgreSQL for the database, Gunicorn for WSGI,
and NGINX as a reverse proxy.
We hope you found this tutorial helpful. Thank you for reading! �
18 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Search …
19 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Recent Posts
M a s t e r i n g G N O M E : Re n a m i n g A u d i o D e v i c e s i n t h e Q u i c k S e � i n g s M e n u
Favorite Sites
Python Tutorials
GoLang Tutorials
CodeForGeek
VM-Help
MySQL Tutorials
Excel Tutorials
20 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
21 of 21 15/06/2023, 8:13 PM