【问题标题】:Publish Django project with gunicorn and NGINX使用 gunicorn 和 NGINX 发布 Django 项目
【发布时间】:2020-07-14 14:52:57
【问题描述】:

我正在尝试使用 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

Gunicorn 和 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

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

停止服务器 CTRL + C

testGunicorn gunicorn --bind 0.0.0.0:8000 config.wsgi

再次在浏览器上测试 我再次可以看到 Django 页面

服务器停止 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

系统链接已创建,请检查 file /run/gunicorn.sock 输出:/run/gunicorn.sock: socket

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

现在我得到权限被拒绝

我已经将文件夹 test_project 的权限设置为用户 sama 和组 www-data,但没有任何影响。怎么了?

【问题讨论】:

    标签: python django sockets nginx gunicorn


    【解决方案1】:

    我认为这里没有充分的理由使用 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
    

    【讨论】:

    • 谢谢,这行得通。但我有第二个问题。这是我的 NGINX 文件listen 80; listen [::]:80; server_name my_secret_domainname; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/sama/websites/test_project; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } } 但我无法通过浏览器访问域,但是当我输入子域时我可以访问。我没有配置任何子域
    • 这是一个不同的问题;请这样问。
    猜你喜欢
    • 2013-12-08
    • 2019-06-19
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多