使用gunicorn和NGINX发布Django项目

2024-06-08 12:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用NGINX和gunicorn发布我的Django项目,但是gunicorn拒绝了我的许可

我的最后一步是我所做的

mkdir /home/sama/websites
cd /home/sama/websites
mkdir test_project
cd test_project

为此项目创建虚拟环境 virtualenv --python=/usr/bin/python3.8 venv

虚拟设备激活 source venv/bin/activate

古尼康公司;Django安装

pip3 install gunicorn
pip3 install django

创建项目

cd ..
django-admin startproject config test_project

允许防火墙连接到端口8000 sudo ufw allow 8000

运行第一个测试 python manage.py runserver 0.0.0.0:8000

在webbrowser上打开我的服务器ip myIP:8000 我可以看到Django基本页面

停止服务器 CTRL+C

testGunicorn gunicorn --bind 0.0.0.0:8000 config.wsgi

网络浏览器的测试增益 我再次看到了Djangos页面

服务器停止 CTRL+C

退出虚拟环境 deactivate

配置Gunicorn 在中创建2个文件 /etc/systemd/system/

gunicorn.socket和gunicorn.service

编辑此文件 sudo nano /etc/systemd/system/gunicorn_test_project.socket

/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn daemon

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=sama
Group=www-data
WorkingDirectory=/home/sama/websites/test_project
ExecStart=/home/sama/websites/test_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock config.wsgi:application

[Install]
WantedBy=multi-user.target

测试插座

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

systemlink已创建,请检查它 file /run/gunicorn.sock 输出:/run/gunicorn.sock:socket

curl --unix-socket /run/gunicorn.sock localhost

现在我的许可被拒绝了

我已经将文件夹test_项目的权限设置为user sama和group www data,但没有任何效果。怎么了


Tags: 项目djangoruntestprojecthomeetcsudo
1条回答
网友
1楼 · 发布于 2024-06-08 12:25:16

我看不出有什么好的理由在这里使用systemd套接字激活

只需使用服务文件并将Gunicorn绑定到HTTP

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=sama
Group=www-data
WorkingDirectory=/home/sama/websites/test_project
ExecStart=/home/sama/websites/test_project/venv/bin/gunicorn  access-logfile -  workers 3  bind 0.0.0.0:8000 config.wsgi:application

[Install]
WantedBy=multi-user.target

相关问题 更多 >