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.
0 Comments