Post Date: May 11, 2018
Last Updated: September 22, 2026
Introduction
Django is a popular Python web framework used to develop web applications. For production deployments, Django applications are commonly served through an application server such as uwsgi, with Nginx acting as the front-end web server.
Nginx handles incoming HTTP requests and forwards application requests to uWSGI through a Unix socket. uWSGI then communicates with the Django application and returns the response to Nginx.
In this article, we will explain how to configure uwsgi with Nginx for a Django application on RHEL 7.4.
Note: This article uses Python 2.7 and a legacy RHEL 7.4 deployment approach from 2018. Python 2.7 is end-of-life, and modern Django applications should use a supported Python version and a current deployment approach.
Prerequisites
Before configuring uwsgi and Nginx, make sure the following are available:
- RHEL 7.4 or a compatible CentOS 7 system.
- Root or sudo access.
- Python 2.7.
- pip.
- Nginx.
- Django.
- uWSGI.
- A configured Django application.
- A domain name pointing to the server.
Implementation
Step 1: Update the Server
Update the installed packages:
yum -y update
Install the required development packages:
yum install gcc openssl-devel bzip2-devel python-devel python-setuptools python-pip
Step 2: Install Python 2.7
Download the required Python version:
cd /usr/src wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
Extract the downloaded archive:
tar -xvf Python-2.7.13.tar.xz
Navigate to the extracted directory:
cd Python-2.7.13/
Configure Python:
./configure
Compile and install it:
make install
Verify the installed version:
python2.7 -V
Expected output:
Python 2.7.13
Step 3: Install and Configure Nginx
Install Nginx using yum:
yum install nginx
Enable Nginx to start automatically after a server reboot:
systemctl enable nginx
Start the Nginx service:
systemctl start nginx
Check the service status:
systemctl status nginx
If a firewall is enabled, make sure HTTP and HTTPS traffic is allowed on ports:
80 443
You can verify Nginx by accessing:
http://your_server_ip_address/
The default Nginx welcome page should be displayed.
Step 4: Install Django and uwsgi
Install Django using pip:
pip install django
Install uwsgi:
pip install uwsgi
Verify the uwsgi installation:
uwsgi --version
Step 5: Configure the Django Website
In this example, the Django application is hosted at:
/var/www/pheonix/
and the domain is:
web.pheonixsolutions.com
Create an Nginx configuration file:
vi /etc/nginx/conf.d/web.pheonixsolutions.com.conf
Add the following configuration:
upstream web_pheonix {
server unix:///var/www/pheonix/web_pheonix.sock;
}
server {
listen 80;
server_name web.pheonixsolutions.com;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/pheonix/media;
}
location /static {
alias /var/www/pheonix/static;
}
location / {
uwsgi_pass web_pheonix;
include /etc/nginx/uwsgi_params;
}
}
Configuration Explanation
The upstream block defines the uwsgi application server:
upstream web_pheonix {
server unix:///var/www/pheonix/web_pheonix.sock;
}
The Unix socket provides communication between Nginx and uWSGI.
The server block defines the domain:
server_name web.pheonixsolutions.com;
Static and media files are served directly by Nginx:
location /media {
alias /var/www/pheonix/media;
}
location /static {
alias /var/www/pheonix/static;
}
Other requests are forwarded to uWSGI:
location / {
uwsgi_pass web_pheonix;
include /etc/nginx/uwsgi_params;
}
Step 6: Configure uwsgi_params
Open the Nginx uWSGI parameters file:
vi /etc/nginx/uwsgi_params
Add the following parameters:
uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
These parameters allow Nginx to pass the required request information to the uWSGI application server.
For additional information, refer to the uwsgi Django and Nginx documentation.
Step 7: Create the uwsgi Configuration
Create a uwsgi configuration file:
vi web_pheonix.ini
Add:
[uwsgi] # Django-related settings # Base directory chdir = /var/www/pheonix/ # Django's WSGI file module = pheonix.wsgi # Master process master = true # Number of worker processes processes = 10 # Unix socket socket = /var/www/pheonix/web_pheonix.sock # Socket permissions chmod-socket = 666 # Clear environment on exit vacuum = true # Optional settings # max-requests = 5000 # limit-as = 128 # daemonize = /var/log/uwsgi-emperor.log
Important Settings
The chdir option specifies the Django project directory:
chdir = /var/www/pheonix/
The module option specifies the Django WSGI module:
module = pheonix.wsgi
The number of uwsgi worker processes is configured using:
processes = 10
The socket must match the socket configured in Nginx:
socket = /var/www/pheonix/web_pheonix.sock
Step 8: Test the uwsgi Configuration
Run uWSGI using the configuration file:
uwsgi --ini web_pheonix.ini
This allows you to verify whether uWSGI can successfully start the Django application.
If there are configuration or application errors, resolve them before proceeding.
Step 9: Configure uWSGI Emperor Mode
uwsgi Emperor mode allows a central uwsgi process to monitor a directory containing application configuration files.
Create the vassals directory:
mkdir -p /etc/uwsgi/vassals
Copy or place the Django application’s uWSGI configuration file inside the vassals directory.
For example:
cp web_pheonix.ini /etc/uwsgi/vassals/
Start uwsgi in Emperor mode:
uwsgi --emperor /etc/uwsgi/vassals
The Emperor process monitors the configuration files in the vassals directory and manages individual uwsgi application instances.
When a vassal configuration changes, the Emperor can restart the corresponding application instance.
Step 10: Verify the Website
Once Nginx and uwsgi are running, access:
http://web.pheonixsolutions.com
Nginx receives the request and forwards it to uwsgi through:
/var/www/pheonix/web_pheonix.sock
uWSGI then passes the request to the Django application.
Step 11: Start uwsgi Automatically After Reboot
Create a startup script:
vi /opt/scripts/uwsgi_restart.sh
Add:
#!/bin/bash /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals
Make the script executable:
chmod +x /opt/scripts/uwsgi_restart.sh
Add the script to /etc/rc.local:
vi /etc/rc.local
Add:
/opt/scripts/uwsgi_restart.sh
This allows the uwsgi Emperor process to start when the server boots.
Note: On RHEL 7, systemd is the preferred service-management mechanism. The
rc.localmethod shown above reflects the original legacy deployment approach.
Conclusion
Configuring Nginx with uWSGI provides a structured way to deploy Django applications. In this setup, Nginx acts as the front-end web server, while uWSGI handles communication with the Django application through a Unix socket.
The overall workflow is:
- Install Python and required dependencies.
- Install Nginx.
- Install Django and uwsgi.
- Configure Nginx for the Django domain.
- Configure the uwsgi parameters.
- Create the uwsgi application configuration.
- Test the uwsgi application.
- Configure uwsgi Emperor mode.
- Configure automatic startup.
This setup allows Nginx to efficiently serve static content while forwarding dynamic Django requests to uwsgi.
Legacy deployment note: Python 2.7 and RHEL 7.4 are no longer current platforms. For a new deployment, use a supported Python version, supported Django release, and a modern service-management approach such as systemd.
FAQ
1. What is uwsgi?
uwsgi is an application server that can run Python web applications and communicate with web servers such as Nginx using protocols such as the uwsgi protocol.
2. Why use Nginx with uwsgi?
Nginx can handle incoming HTTP requests, static files, client connections, and other web-server responsibilities while forwarding dynamic application requests to uwsgi.
3. What is the purpose of the Unix socket?
The Unix socket provides communication between Nginx and uwsgi on the same server.
In this example:
/var/www/pheonix/web_pheonix.sock
The same socket must be configured in both Nginx and uWSGI.
4. What is uwsgi Emperor mode?
Emperor mode allows uwsgi to manage multiple application instances, called vassals, from a central configuration directory.
In this example:
/etc/uwsgi/vassals
Related Article
Talk to our experts
Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.