gunicorn的部署和配置

一、创建配置文件

在项目的根目录下执行

vim  gunicorn.conf.py
复制以下内容:
# gunicorn.conf.py
import multiprocessing

# 服务器socket
bind = "127.0.0.1:8000"
backlog = 2048

# 工作进程 (Python 3.11优化)
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = "sync"  # Python 3.11的同步性能已经很好
worker_connections = 1000
timeout = 30
keepalive = 2

# Python 3.11性能优化
worker_tmp_dir = "/dev/shm"  # 使用内存文件系统
# 重启
max_requests = 1000
max_requests_jitter = 100
preload_app = True

# 日志
accesslog = "/root/projects/gktb/logs/gunicorn_access.log"
errorlog = "/root/projects/gktb/logs/gunicorn_error.log"
loglevel = "info"

# 进程命名
proc_name = 'gktb_gunicorn'
# 用户权限
user = "root"
group = "root"

-----------------------------

注意项目文件夹权限

---------------------------

二、安装
pip3 install gunicorn

安装后运行测试
python3 -m gunicorn --config gunicorn.conf.py 项目名.wsgi:application

查找 gunicorn 可执行文件路径
如果仍然想用 gunicorn 命令,可以找到它的安装位置:


find / -name gunicorn 2>/dev/null

检查环境变量:
echo $PATH

修改环境变量,添加新增部分:
[root@192 ~]#  cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
source /opt/rh/devtoolset-9/enable
#This is  gcc version
source /opt/rh/devtoolset-9/enable #This is  gcc version
export PATH=$path:/usr/local/python3/bin #新增的
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin

生效命令
source ~/.bashrc

测试:
gunicorn --config gunicorn.conf.py myproject.wsgi:application
--------------------------------------------------------------

三、创建服务:
vim /etc/systemd/system/gktb.service

内容:
[Unit]
Description=GKTB Django Application
After=network.target mysql.service

[Service]
WorkingDirectory=/root/projects/gktb
ExecStart=/usr/local/python3/bin/gunicorn --config gunicorn.conf.py gktb.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target


重新加载 Systemd:

sudo systemctl daemon-reload
sudo systemctl restart gktb

---------------------------------
四、安装nginx
yum install -y nginx

添加配置文件:
vim /etc/nginx/conf.d/gktb.conf


server {
    listen 80;
    server_name your-domain.com your-server-ip;

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;

    # 静态文件
    location /static/ {
        alias /root/projects/gktb/app01/static/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # 媒体文件
    location /media/ {
        alias /root/projects/gktb/app01/static/media/;
        expires 7d;
    }

    # Django应用
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 超时设置
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;
    }

    # 文件上传大小限制
    client_max_body_size 10M;

    # 日志
    access_log /var/log/nginx/gktb_access.log;
    error_log /var/log/nginx/gktb_error.log;
}

最后修改/etc/nginx/nginx.conf中的user root,权限问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路生花工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值