search
HomeOperation and MaintenanceNginxHow to deploy centos+nginx+uwsgi to launch django project

My Django project is called yunwei. The main apps are rabc and web. The entire project is placed under /opt/.
is as follows:

[root@test-code opt]# ls
django_virt  nginx  redis  redis-6.2.6  yunwei
[root@test-code opt]# ls yunwei/
manage.py  rbac  static  templates  uwsgi  web  yunwei
[root@test-code opt]# ls yunwei/uwsgi/
cut_log.sh  log  uwsgi.ini  uwsgi.log  uwsgi.pid  uwsgi.sock
[root@test-code opt]#

How to deploy centos+nginx+uwsgi to launch django project

/opt/yunwei/ is The root directory of my django project, manage.py is in this directory, there is a subdirectory yunwei with the same name under /opt/yunwei/, and there is a configuration file setting.py below

Python installation

If the python version used is 2, there is no need to reinstall it. If it is 3, then you need to reinstall it

#在原项目处导出django项目安装的库存入文件
pip freeze > install.txt

Put the file where you need to deploy it On the server, and install the package in the file

#在待部署的服务器上执行
pip install -r install.txt

nginx installation & configuration (processing static requests and proxying dynamic requests to uwsgi)

nginx installation reference: linux installation nginx

The nginx configuration file is as follows

[root@test-code opt]# cat /opt/nginx/conf.d/django.conf 
server {
       listen 8881;
       server_name localhost;
       server_tokens off;       
    location /static {
                root  /opt/yunwei;
                index  index.html index.htm;
                }
    location / {
            include uwsgi_params; #nginx加载uwsgi模块
            uwsgi_buffer_size 16k;
            uwsgi_busy_buffers_size 24k;
            #如果你后端的需要超过60秒时间处理请求,那么一定要加上下面三个超时时间的设置,不然60s之后nginx断开链接报超时
            uwsgi_send_timeout 600;        # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
            uwsgi_connect_timeout 600;   # 指定连接到后端uWSGI的超时时间。
            uwsgi_read_timeout 600;        # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。 
            uwsgi_pass unix:/opt/yunwei/uwsgi/uwsgi.sock; #nginx对应的uwsgi socket文件
        }

}

uwsgi installation & configuration

pip install uwsgi

Create a uwsgi directory under the django project directory/opt/yunwei/ to store uwsgi-related files.

cd /opt/yunwei/ && mkdir uwsgi
touch uwsgi/uwsgi.ini

Create configuration file

[root@test-code yunwei]# cat uwsgi/uwsgi.ini 
[uwsgi]
#django项目的根目录,即manage.py所在的目录
chdir=/opt/yunwei
#django项目的wsgi,yuwnei的项目名
module=yunwei.wsgi:application
socket=/opt/yunwei/uwsgi/uwsgi.sock
#这里是我的python虚拟环境,可以不配置
home=/opt/django_virt
#进程数
workers=5
#pid文件路径
pidfile=/opt/yunwei/uwsgi/uwsgi.pid
#IP端口
socket = 0.0.0.0:8000
master=true
#退出清理文件
vacuum=true
#启用线程
enable-threads=true
#序列化接受的内容,如果可能的话
thunder-lock=true
#设置自中断时间,如果后端处理一些请求时间比较长,这个一定要设置大一点
harakiri=3600 #
socket-timeout=3600 #这个是和nginx搭配部署时的设置
http-timeout=3600 #这个是单独部署时的设置
#设置缓冲
post-buffering=65535
buffer-size = 6553600
#后台守护方式运行,日志路径
daemonize=/opt/yunwei/uwsgi/uwsgi.log

The title turns off the DEBUG mode of setting.py

vim /opt/yunwei/yunwei/setting.py

DEBUG = True #改为 DEBUG = False

When the debug mode is True, django will handle static static requests by itself, now it is nginx To process these requests, so after completing the above steps for False

, you only need to start uwsgi and nginx, and access the log file of

#uwsgi 启动命令 
uwsgi --ini /opt/yunwei/uwsgi/uwsgi.ini
#uwsgi 重启命令 
uwsgi --reload /opt/yunwei/uwsgi/uwsgi.ini

uwsgi in /opt/yunwei through the nginx listen port /uwsgi/uwsgi.log

You can use ss -tnulp | grep uwsgi to kill -9 process ID to kill the process

#启动nginx
nginx
#重启
nginx -s reload
#关闭
nginx -s stop
#检测配置文件是否正确
nginx -t

The above is the detailed content of How to deploy centos+nginx+uwsgi to launch django project. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX vs. Apache: A Comparative Analysis of Web ServersNGINX vs. Apache: A Comparative Analysis of Web ServersApr 21, 2025 am 12:08 AM

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX Unit's Advantages: Flexibility and PerformanceNGINX Unit's Advantages: Flexibility and PerformanceApr 20, 2025 am 12:07 AM

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.