Apache 上 Django 应用程序的 403 禁止错误
403 Forbidden error for Django app on Apache
我已经关注 this deploy tutorial 的 Django for Apache,并按照其中的说明进行了所有操作。当我尝试使用服务器的 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 上它似乎工作得很好,基于开发服务器。
我不知道我现在做错了什么。感谢您的帮助。
如果它对任何人有帮助,我已经设法解决了这个问题。
扯了些头发后,我认为问题出在实际上,WSGI想要一个不同版本的应用程序python,结果发现不兼容,导致无法加载它。
我克服它的方法:我在干净的 Ubuntu 服务器上默认安装了 python 3.7(通过 PPA deadsnakes;instruction),然后从头开始这个过程。现在一切正常。
我已经关注 this deploy tutorial 的 Django for Apache,并按照其中的说明进行了所有操作。当我尝试使用服务器的 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 上它似乎工作得很好,基于开发服务器。
我不知道我现在做错了什么。感谢您的帮助。
如果它对任何人有帮助,我已经设法解决了这个问题。 扯了些头发后,我认为问题出在实际上,WSGI想要一个不同版本的应用程序python,结果发现不兼容,导致无法加载它。
我克服它的方法:我在干净的 Ubuntu 服务器上默认安装了 python 3.7(通过 PPA deadsnakes;instruction),然后从头开始这个过程。现在一切正常。