[英]Apache - Error 403 Forbidden when deploying Django app
[英]403 Forbidden error for Django app on Apache
我遵循了針對Apache的Django 部署教程 ,並在那里進行了所有操作。 當我嘗試使用服務器的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)
應用程序中使用的庫(通過點凍結制作):
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; 說明 ),並從頭開始了該過程。 現在一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.