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

WBOY
Release: 2023-05-15 08:13:13
forward
1161 people have browsed it

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

Installation of django related libraries

#在原项目处导出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!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template