Backend Development 8 min read

Deploying a Django Application on CentOS with uWSGI and Nginx

This guide walks through setting up a CentOS 7 server, creating a non‑root user, installing Python 3, MySQL, and required packages, configuring a Django project, and deploying it with uWSGI behind Nginx for production use.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Deploying a Django Application on CentOS with uWSGI and Nginx

The tutorial uses a Tencent Cloud CentOS 7 64‑bit server (any similar Linux works) and starts by creating a new non‑root user, assigning it to the adm and sudo groups, and setting a password.

<code>useradd -m username # create user
passwd username # set password
usermod -a -G adm username
usermod -a -G sudo username
exit # logout after permission setup</code>

After logging in with the new account, Python 3 and other components are installed. The script below downloads and runs a ready‑made installer for Python 3.6 on CentOS 7.

<code>wget https://raw.githubusercontent.com/LunacyZeus/Python3.6-for-Centos7.0/master/install.sh && sh install.sh</code>

pip is upgraded and the requests library is installed to verify the Python environment.

<code>python3 -m pip install --upgrade pip
python3 -m pip install requests</code>

Next, the Django project is cloned (e.g., to /home/username/myblog ) and a virtual environment is created.

<code>git clone <yourcode>
cd myblog
python3 -m pip install virtualenv
virtualenv venv
. venv/bin/activate
pip install -r requirements.txt
python manage.py collectstatic</code>

MySQL is set up: the root password is changed and remote access can be granted.

<code>mysql -u root -p
> set password for 'root'@'localhost' = password('password');
> grant all privileges on *.* to root@'%' identified by 'password';</code>

After creating the Django database and updating settings.py with the new credentials, database migrations and a superuser are created, then the development server is started.

<code>python manage.py migrate
python manage.py createsuperuser
python manage.py runserver 0.0.0.0:8001</code>

The article then explains the relationship between Nginx, uWSGI, and Django, highlighting why Nginx is used as the external HTTP interface, how it forwards dynamic requests to uWSGI, and the benefits regarding security, load balancing, and static‑file handling.

uWSGI is installed inside the virtual environment and a simple test application is used to verify it works.

<code>(venv) pip install uwsgi</code>
<code># test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return b"Hello World"

uwsgi --http :8000 --wsgi-file test.py</code>

When http://<public-ip>:8000 returns “Hello World”, uWSGI is functional.

A production‑grade uwsgi.ini file is created to point to the Django project, define the socket, number of workers, and other options.

<code>[uwsgi]
socket = /tmp/www.wangzhy.com.socket
chdir = /home/username/myblog
module = blog.wsgi
master = true
processes = 4
vacuum = true</code>

uWSGI is started with the configuration file, optionally in the background.

<code>uwsgi --ini uwsgi.ini
# background
uwsgi --ini mysite_uwsgi.ini --logto mysite.log &</code>

Nginx is installed on CentOS 7 via the official repository, started, and a new site configuration ( blog.conf ) is added under /etc/nginx/conf.d to serve static/media files and proxy dynamic requests to the uWSGI socket.

<code>sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
systemctl start nginx.service</code>
<code>server {
    listen      80;
    server_name 127.0.0.1 <yoursite.com>;
    charset     utf-8;
    client_max_body_size 75M;
    location /media  { alias /home/username/myblog/media; }
    location /static { alias /home/username/myblog/static; }
    location / {
        include     uwsgi_params;
        uwsgi_pass unix:/tmp/www.wangzhy.com.socket;
    }
}</code>

The configuration is tested and Nginx is restarted.

<code>nginx -t
service nginx restart</code>

Finally, the uWSGI service is launched and the site becomes accessible through the server’s public IP.

<code>uwsgi --ini uwsgi.ini</code>

All steps together provide a complete, production‑ready deployment of a Django application on a CentOS server using uWSGI and Nginx.

backendPythonDeploymentDjangoNginxCentOSuwsgi
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.