Table of Contents
Python installation
Installation of django related libraries
nginx installation & configuration (processing static requests and proxying dynamic requests to uwsgi)
uwsgi installation & configuration
The title turns off the DEBUG mode of setting.py
Home Operation and Maintenance Nginx How to deploy centos+nginx+uwsgi to launch django project

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

May 15, 2023 am 08:13 AM
nginx django uwsgi

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]#
Copy after login

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
Copy after login

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

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

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文件
        }

}
Copy after login

uwsgi installation & configuration

pip install uwsgi
Copy after login

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
Copy after login

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
Copy after login

The title turns off the DEBUG mode of setting.py

vim /opt/yunwei/yunwei/setting.py

DEBUG = True #改为 DEBUG = False
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

See all articles