环境:
系统:centos7.9
python:3.11
django:5.2
1、安装
pip install gunicorn==23.0.0
2、写配置文件
# 创建Gunicorn配置文件
vim /home/igo/projects/igo-ecommerce/gunicorn.conf.py
```
Gunicorn配置示例:
```python
# gunicorn.conf.py
import multiprocessing
# 服务器配置
bind = "127.0.0.1:8000"
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = "sync"
worker_connections = 1000
max_requests = 1000
max_requests_jitter = 100
timeout = 30
keepalive = 2
# 日志配置
accesslog = "/home/igo/logs/gunicorn_access.log"
errorlog = "/home/igo/logs/gunicorn_error.log"
loglevel = "info"
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
# 进程配置
daemon = False
pidfile = "/home/igo/logs/gunicorn.pid"
user = "igo"
group = "igo"
tmp_upload_dir = None
# 安全配置
limit_request_line = 4094
limit_request_fields = 100
limit_request_field_size = 8190
2、创建systemd服务
# 创建Gunicorn服务文件
sudo vim /etc/systemd/system/igo-gunicorn.service
服务配置示例:
```ini
[Unit]
Description=Gunicorn instance to serve IGO E-commerce
After=network.target mysql.service redis.service
[Service]
User=igo
Group=igo
WorkingDirectory=/home/igo/projects/igo-ecommerce
Environment="PATH=/home/igo/projects/igo_env/bin"
Environment="DJANGO_SETTINGS_MODULE=config.settings.production"
ExecStart=/home/igo/projects/igo_env/bin/gunicorn --config gunicorn.conf.py config.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
```
3、启动服务
# 重新加载systemd配置
sudo systemctl daemon-reload
# 启动Gunicorn服务
sudo systemctl start igo-gunicorn
sudo systemctl enable igo-gunicorn
# 检查服务状态
sudo systemctl status igo-gunicorn