403 Apach上Django应用程序的禁止错误

2024-09-30 16:28:14 发布

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

我已经为Apache跟踪了Django的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>

我对[我的项目名称]做了以下更改/设置.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通信和端口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

该应用程序是在python3.7.1上开发的,但是在3.6.8上,基于开发服务器,它似乎工作得很好。你知道吗

我不知道在这一点上我做错了什么。感谢您的帮助。你知道吗


Tags: namepyprojecthomedatamywwwstatic
1条回答
网友
1楼 · 发布于 2024-09-30 16:28:14

万一对谁有帮助,我已经设法解决了这个问题。 在做了一些修改之后,我认为问题在于,WSGI想要一个不同版本的python应用程序,结果是不兼容的,没有加载它就导致了错误。你知道吗

我克服它的方法是:我在干净的Ubuntu服务器上(通过PPA deadsnakes;instruction)安装了python3.7作为默认值,然后从头开始这个过程。现在一切正常了。你知道吗

相关问题 更多 >