【问题标题】:403 Forbidden error for Django app on ApacheApache 上的 Django 应用程序出现 403 禁止错误
【发布时间】:2019-08-13 23:49:52
【问题描述】:

我已经关注了 Django for Apache 的 this deploy tutorial,并且做了所有的事情。当我尝试使用服务器的 ip 进入页面时,我得到了

Forbidden
You don't have permission to access / on this server.
Apache/2.4.29 (Ubuntu) Server at <my-ip> Port 80

使用python manage.py runserver 0.0.0.0:8000 运行应用程序正常,我可以访问该应用程序。

通过我得到的 apache2 日志:

Current thread 0x00007fc84606fbc0 (most recent call first):
[Wed Aug 14 01:11:51.141895 2019] [core:notice] [pid 31839:tid 140498145049536] AH00051: child pid 32108 exit signal Aborted (6), possible coredump in /etc/apache2
$preter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

我的配置文件:

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias /static /home/ernest/[my-project-name]/static
        <Directory /home/ernest/[my-project-name]/static>
                Require all granted
        </Directory>

        Alias /media /home/ernest/[my-project-name]/datagather/uploaded_files
        <Directory /home/ernest/[my-project-name]/datagather/uploaded_files>
                Require all granted
        </Directory>

        <Directory /home/ernest/[my-project-name]/[my-project-name]/>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIScriptAlias / /home/ernest/[my-project-name]/[my-project-name]/wsgi.py
        WSGIDaemonProcess my_app python-path=/home/ernest/[my-project-name] python-home=/home/[my-project-name]/venv
        WSGIProcessGroup my_app

</VirtualHost>

我已对 [my-project-name]/settings.py 进行了以下更改:

DEBUG = False

ALLOWED_HOSTS = ['[my-server-ip]', 'localhost']

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'datagather/uploaded_files/')
MEDIA_URL = '/datagather/uploaded_files/'

我还使用

设置了适当的权限
sudo chown :www-data [my-project-name]/db.sqlite3
sudo chmod 664 [my-project-name]/db.sqlite3
sudo chown :www-data [my-project-name]/
sudo chown -R :www-data [my-project-name]/datagather/uploaded_files/
sudo chmod -R 775 [my-project-name]/datagather/uploaded_files

它看起来像这样:

ls -la
total 1384
drwxrwxr-x 10    666 www-data    4096 Aug 14 01:05 .
drwx------  5 ernest ernest      4096 Aug 14 00:40 ..
drwxrwxr-x  6 ernest ernest      4096 Aug 13 23:25 [dir-in-project]
drwxrwxr-x  6 ernest ernest      4096 Aug 13 23:25 [dir-in-project]
drwxrwxr-x  7 ernest ernest      4096 Aug 13 23:56 datagather
-rw-rw-r--  1 ernest www-data  212992 Aug 13 23:17 db.sqlite3
-rw-rw-r--  1 ernest ernest         0 Aug 13 23:17 __init__.py
-rw-rw-r--  1 ernest ernest       642 Aug 13 23:17 manage.py
drwxrwxr-x  3 ernest ernest      4096 Aug 14 01:30 [my-project-name]
-rw-rw-r--  1 ernest ernest       183 Aug 13 23:17 requirements.txt
-rw-rw-r--  1 ernest ernest   1152635 Aug 13 23:34 simple_logs.log
drwxrwxr-x  8 ernest ernest      4096 Aug 13 23:25 static
drwxrwxr-x  7 ernest ernest      4096 Aug 13 23:25 [dir-in-project]
drwxrwxr-x  8 ernest ernest      4096 Aug 13 23:25 [dir-in-project]
drwxr-xr-x  7 root   root        4096 Aug 14 01:08 venv

当前端口使用情况如下(之前启用了 http-traffic 和端口 80)

sudo lsof -i -P -n | grep LISTEN

systemd-r   846 systemd-resolve   13u  IPv4  15597      0t0  TCP 127.0.0.53:53 (LISTEN)
sshd        976            root    3u  IPv4  18092      0t0  TCP *:22 (LISTEN)
sshd        976            root    4u  IPv6  18103      0t0  TCP *:22 (LISTEN)
apache2    3265            root    4u  IPv6 870246      0t0  TCP *:80 (LISTEN)
apache2    3280        www-data    4u  IPv6 870246      0t0  TCP *:80 (LISTEN)
apache2    3281        www-data    4u  IPv6 870246      0t0  TCP *:80 (LISTEN)

应用中使用的库(使用 pip freeze 制作):

Django==2.2.3
et-xmlfile==1.0.1
jdcal==1.4.1
numpy==1.16.4
openpyxl==2.6.2
pandas==0.24.2
python-dateutil==2.8.0
pytz==2019.1
six==1.12.0
sqlparse==0.3.0
Unidecode==1.1.1
xlrd==1.2.0

该应用是在 Python 3.7.1 上开发的,但在 3.6.8 上它似乎工作得很好,基于开发服务器。

我现在不知道我做错了什么。任何帮助表示赞赏。

【问题讨论】:

标签: python django apache2 http-status-code-403


【解决方案1】:

如果它对任何人有帮助,我已经设法解决了这个问题。 扯了扯头发,我想问题出在实际上是WSGI想要一个不同版本的python的应用程序,结果不兼容,导致没有加载它。

我克服它的方法:我在干净的 Ubuntu 服务器上默认安装了 python 3.7(通过 PPA deadsnakes;instruction),然后从头开始这个过程。现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 2013-07-19
    • 1970-01-01
    • 2013-08-29
    • 2015-08-19
    • 2013-10-17
    • 2014-06-13
    • 1970-01-01
    相关资源
    最近更新 更多