django + gunicorn + systemd + apache 禁止 403?

django + gunicorn + systemd + apache 禁止 403?

我已经关注指导和使用 guniconrn 和 apache 部署我的 django。

但我不太明白指南上说:

(除上述内容外,您仍然需要为静态文件和媒体文件指定别名。)

但无论如何,这些都是我的配置。我的 django 应用程序位于/var/www/html/django-project/helloapp

/etc/systemd/system/gunicorn.service:

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

[Service]
PIDFile=/run/gunicorn/pid
User=lau
Group=lau
RuntimeDirectory=gunicorn
WorkingDirectory=/var/www/html/django-project/helloapp
ExecStart=/usr/bin/gunicorn --pid /run/gunicorn/pid   \
          --bind unix:/run/gunicorn/socket helloapp.wsgi
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

/etc/systemd/system/gunicorn.socket:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn/socket

[Install]
WantedBy=sockets.target

/etc/tmpfiles.d/gunicorn.conf:

d /run/gunicorn 0755 lau www-data -

/etc/apache2/apache2.conf:

<Directory /var/www/html/django-project/helloapp>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
</Directory>

/etc/apache2/sites-available/000-default.conf:

<VirtualHost *:80>
ProxyPass /static/ !
ProxyPass /media/ !
ProxyPass / http://localhost:8000/

</VirtualHost>

/etc/apache2/ports.conf:

Listen 80
Listen 8000

重新启动 Apache 后,我得到 403http://127.0.0.1/或者http://127.0.0.1:8000

Forbidden

You don't have permission to access / on this server.
Apache/2.4.25 (Ubuntu) Server at localhost Port 8000

有任何想法吗?

我在 Ubuntu 17.04

编辑:

我看到一些 python 进程正在运行但不确定它是否正确:

$ ps -wef | grep python
root      2418     1  0 06:16 ?        00:00:00 /usr/bin/python3 /usr/share/apt-xapian-index/update-apt-xapian-index-dbus
lau      25488 28678  0 23:49 pts/2    00:00:00 grep --color=auto python
lau      30605     1  0 18:30 ?        00:00:02 /var/www/html/django-project/helloapp/env/bin/python /var/www/html/django-project/helloapp/env/bin/gunicorn --workers 3 --bind unix:/var/www/html/django-project/helloapp.sock helloapp.wsgi:application
lau      30609 30605  0 18:30 ?        00:00:00 /var/www/html/django-project/helloapp/env/bin/python /var/www/html/django-project/helloapp/env/bin/gunicorn --workers 3 --bind unix:/var/www/html/django-project/helloapp.sock helloapp.wsgi:application
lau      30610 30605  0 18:30 ?        00:00:00 /var/www/html/django-project/helloapp/env/bin/python /var/www/html/django-project/helloapp/env/bin/gunicorn --workers 3 --bind unix:/var/www/html/django-project/helloapp.sock helloapp.wsgi:application
lau      30612 30605  0 18:30 ?        00:00:00 /var/www/html/django-project/helloapp/env/bin/python /var/www/html/django-project/helloapp/env/bin/gunicorn --workers 3 --bind unix:/var/www/html/django-project/helloapp.sock helloapp.wsgi:application

答案1

我知道这是一个古老的问题,原作者可能已经解决了这个问题,但据我所知,您已经在端口 8000 上启动了 Gunicorn,并告诉 apache 监听同一个端口。由于两个应用程序正在监听同一个端口,因此可能会产生冲突。根据您进程的输出判断,gunicorn 已在运行。从Listen 8000apache 配置文件中删除它并重新启动 apache 就足够了。这应该可以解决问题。

相关内容