Skip to content

Commit

Permalink
Add uwsgi options
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Aug 10, 2023
1 parent c271b6a commit 7f95b4c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 9 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

生产环境中会额外用到:Nginx、uWSGI、PostgreSQL、Memcached。以下流程在 Debian 12 测试过。

1. 安装依赖:`apt install python3-venv nginx uwsgi-plugin-python3 postgresql memcached uwsgi`
1. 安装依赖:`apt install python3-dev python3-venv nginx postgresql memcached`
1. (建议)本地连接 PostgreSQL 无需鉴权:修改 `/etc/postgresql/15/main/pg_hba.conf`,将 `local all all peer` 一行改为 `local all all trust`,然后执行 `systemctl reload postgresql`
1. 创建数据库:`su postgres``psql``create user hackergame; create database hackergame;`, `\c hackergame`, `grant create on schema public to hackergame;`
1. 克隆代码:`cd /opt``git clone https://github.com/ustclug/hackergame.git`
Expand All @@ -31,10 +31,16 @@
1. Static 目录初始化:`./manage.py collectstatic`
1. Google 与 Microsoft app secret 写入数据库:`./manage.py setup`
1. 退出 venv:`deactivate`
1. uWSGI 配置文件:`cp conf/uwsgi-apps/hackergame.ini /etc/uwsgi/apps-available/hackergame.ini``ln -s /etc/uwsgi/apps-available/hackergame.ini /etc/uwsgi/apps-enabled/hackergame.ini``systemctl restart uwsgi`
- **注意**:编辑 `/etc/logrotate.d/uwsgi`,修改 `rotate 5``rotate -1`,否则 logrotate 会丢弃较早的 uwsgi 日志
1. uWSGI 相关配置文件:`cp conf/systemd/[email protected] /etc/systemd/system/`, `cp conf/logrotate/uwsgi /etc/logrotate.d/`, `systemctl daemon-reload`, `systemctl enable --now [email protected]`
1. Nginx 配置文件:`cp conf/nginx-sites/hackergame /etc/nginx/sites-available/hackergame``ln -s /etc/nginx/sites-available/hackergame /etc/nginx/sites-enabled/hackergame``systemctl reload nginx`

### uWSGI 运行情况检查

可以使用 [`uwsgitop`](https://uwsgi-docs.readthedocs.io/en/latest/StatsServer.html#uwsgitop) 来查看 uWSGI 运行情况。

1. 安装 `pip install uwsgitop`
1. 执行 `uwsgitop /run/uwsgi/app/hackergame/stats.socket` 查看。

## 运行

注:运行所有以 `./manage.py` 开头的命令都需要先进入 venv 和设置环境变量。
Expand Down
9 changes: 9 additions & 0 deletions conf/logrotate/uwsgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"/var/log/uwsgi/app/*.log" {
copytruncate
daily
rotate -1
compress
delaycompress
missingok
notifempty
}
16 changes: 16 additions & 0 deletions conf/systemd/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Hackergame Platform (%i)
After=syslog.target

[Service]
ExecStart=/opt/hackergame/.venv/bin/uwsgi --ini /opt/hackergame/conf/uwsgi-apps/%i.ini --ini /opt/hackergame/conf/uwsgi.ini --set-placeholder appname=%i --daemonize /var/log/uwsgi/app/%i.log
LogsDirectory=uwsgi/app
RuntimeDirectory=uwsgi/app/%i
Restart=always
KillSignal=SIGQUIT
Type=forking
User=www-data
Group=www-data

[Install]
WantedBy=multi-user.target
6 changes: 4 additions & 2 deletions conf/uwsgi-apps/hackergame-docker.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[uwsgi]
socket=/run/uwsgi/app/hackergame/socket
chdir=/opt/hackergame
plugin=python3
#plugin=python3,gevent_python3
module=frontend.wsgi:application
env=DJANGO_SETTINGS_MODULE=conf.settings.hackergame
master=true
processes=64
#processes=16
gevent=1024
gevent-monkey-patch=true
vacuum=true
home=/usr/local
uid=www-data
Expand Down
6 changes: 4 additions & 2 deletions conf/uwsgi-apps/hackergame.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[uwsgi]
socket=unix:///run/uwsgi/app/hackergame/socket
chdir=/opt/hackergame
plugin=python3
#plugin=python3,gevent_python3
module=frontend.wsgi:application
env=DJANGO_SETTINGS_MODULE=conf.settings.hackergame
master=true
processes=16
#processes=16
gevent=1024
gevent-monkey-patch=true
vacuum=true
home=.venv
stats=/run/uwsgi/app/hackergame/stats.socket
Expand Down
6 changes: 4 additions & 2 deletions conf/uwsgi-apps/hgtest.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[uwsgi]
socket=unix:///run/uwsgi/app/hgtest/socket
chdir=/opt/hgtest
plugin=python3
#plugin=python3,gevent_python3
module=frontend.wsgi:application
env=DJANGO_SETTINGS_MODULE=conf.settings.hgtest
master=true
processes=64
#processes=16
gevent=1024
gevent-monkey-patch=true
vacuum=true
home=.venv
stats=/run/uwsgi/app/hgtest/stats.socket
Expand Down
11 changes: 11 additions & 0 deletions conf/uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[uwsgi]
master = true
workers = 2
no-orphans = true
pidfile = /run/uwsgi/app/%(appname)/pid
socket = /run/uwsgi/app/%(appname)/socket
chown-socket = www-data
chmod-socket = 660
log-date = true
uid = www-data
gid = www-data
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cryptography==41.0.2
defusedxml==0.7.1
Django==4.2.4
django-allauth==0.51.0
gevent==23.7.0
idna==3.4
importlib-metadata==4.12.0
jmespath==0.10.0
Expand All @@ -29,4 +30,5 @@ six==1.16.0
sqlparse==0.4.4
typing-extensions==4.3.0
urllib3==1.26.12
uwsgi==2.0.22
zipp==3.8.1

0 comments on commit 7f95b4c

Please sign in to comment.