Bobcares

Deploy Django Applications with uwsgi and nginx on Ubuntu on 18.04

by | Jun 24, 2020

Looking for a reliable way to deploy Django Applications on uwsgi and Nginx? Here’s help.

As a part of our Server Management Services, we help our customers to deploy Django Applications in Linux servers.

Today we’ll go through the tried and tested way to install and configure all components required to support and serve Django applications on Ubuntu 18.04.

 

Deploy Django Applications with uwsgi and Nginx

 

Django is a popular Framework to develop Python applications or websites, and many developers use Nginx + uWSGI as the app/web server combination for Django deployments.

In this article, we provide a brief description of deploying Django applications with uWSGI and Nginx on Ubuntu.  Here, Django application uses uWSGI as an app server and Nginx as the webserver.

1. Initially, we create a user ‘abc’ and install python3 and virtualenv on the server.

apt update
apt install -y python3-pip
apt install build-essential libssl-dev libffi-dev python3-dev
pip3 install virtualenv

2. Then, we set up a Sample Django Application in /home/abc/project.

3. After, we create a virtual environment. Then, make sure that we are able to execute runserver on the created virtual environment.

python manage.py runserver

4. Next, we install uWSGI and also create directories to store uWSGI config files for the projects.

pip3 install uwsgi
mkdir -p /etc/uwsgi/apps-available /etc/uwsgi/apps-enabled

5. Again, we create a uWSGI configuration file project.ini in /etc/uwsgi/apps-available.

[uwsgi]
uid = abc
gid = abc
chdir = /home/abc/project
home = /home/abc/project/penv
module = project.wsgi:application
env = DJANGO_SETTINGS_MODULE=project.settings
master = true
processes = 3
socket = /run/uwsgi/project.sock
logto = /var/log/uwsgi/project.log
chown-socket = abc:abc
chmod-socket = 664
vacuum = true

6. Check if this configuration is working properly by running the following command.

cd /etc/uwsgi/apps-available/
uwsgi --ini project.ini

7. After that, we create a symlink for this in apps-enabled directory with the below command.

ln -sf /etc/uwsgi/apps-available/project.ini /etc/uwsgi/apps-enabled/

8. Then, we create a service for uwsgi. Therefore, it can keep running in the background and start when the server boots up. For that, we create  uwsgi.service in /etc/systemd/system.

9. After the creation of service, we start it and check uwsgi status.

systemctl start uwsgi

10. Now, we have connected uWSGI and the application. Next, we install Nginx and create an Nginx site configuration /etc/nginx/sites-available/sample file for the Django application.

server {
listen 80;
server_name domain.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/abc/project;
}

location / {
include uwsgi_params;
uwsgi_pass unix:/home/abc/project/project.sock;
}
}

11. We create a symlink to Nginx’s sites-enabled directory to sites-enabled by using the following command.

ln -s /etc/nginx/sites-available/sample /etc/nginx/sites-enabled

12.  We check Nginx’s configuration file and restart it.

service nginx configtest

service nginx restart

That’s it.

 

Deploy Django with Nginx and uwsgi – Common error and fix

Let’s see how our Support Engineers fix the Django deployment errors.

 

Permission issue

Some customers get errors after running the command ‘uwsgi –ini project.ini’. This is because of the improper working of uWSGI due to incorrect directory permission for the log directory.

First, we comment out logto directive in project.ini.

So we change the permission as follows.

cd /var/log

mkdir -p uwsgi

chown -R user:user uwsgi/

Then, we enable logto directive in project.ini and successfully executes the command ‘uwsgi –ini project.ini’.

This is how we fix the issue.

 

[Need assistance to fix  Django Deployment errors? – Our Support Engineers will help you.]

 

Conclusion

In short, one of the main reasons for failure to deploy Django Applications with uwsgi and Nginx is faulty permission problems. Today, we saw how our Support Engineers deployed Django Applications with uwsgi and Nginx.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF