在 mod_wsgi 上部署 django

在 mod_wsgi 上部署 django

问题:
当我尝试访问 localhost:80 时出现 404 错误。

[root@alarmpi public_html]# tail -n 1 /var/log/httpd/error_log 
[Sat Jun 04 23:09:50.235251 2016] [wsgi:error] [pid 2323:tid 1973417008] [client ::1:45230] Target WSGI script not found or unable to stat: /home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/apache/wsgy.py    

系统:
在 Raspberry Pi 2 上使用全新安装的 Arch Arm。

[root@alarmpi alarm]# apachectl -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::ba27:ebff:fed4:595a. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.20 (Unix)
Server built:   Apr 18 2016 12:41:21
Server's Module Magic Number: 20120211:57
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture:   32-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/bin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"
[root@alarmpi hamstergangster.eu]#apachectl -k restart       
[root@alarmpi hamstergangster.eu]# tail -n 5 /var/log/httpd/error_log 
[Sat Jun 04 22:19:50.348445 2016] [mpm_event:notice] [pid 327:tid 1995890688] AH00494: SIGHUP received.  Attempting to restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::ba27:ebff:fed4:595a. Set the 'ServerName' directive globally to suppress this message
[Sat Jun 04 22:19:50.403560 2016] [lbmethod_heartbeat:notice] [pid 327:tid 1995890688] AH02282: No slotmem from mod_heartmonitor
[Sat Jun 04 22:19:50.409304 2016] [mpm_event:notice] [pid 327:tid 1995890688] AH00489: Apache/2.4.20 (Unix) mod_wsgi/4.4.22 Python/3.5.1 configured -- resuming normal operations
[Sat Jun 04 22:19:50.409539 2016] [core:notice] [pid 327:tid 1995890688] AH00094: Command line: '/usr/bin/httpd'
[root@alarmpi hamstergangster.eu]# source venv/bin/activate
(venv) [root@alarmpi hamstergangster.eu]# python       
Python 3.5.1 (default, Mar  6 2016, 10:14:04) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 9, 6, 'final', 0)

配置:
/home/home/alarm具有 777 权限/home/alarm/public_htmml一切里面也有 777 权限;不是权限问题。

TL;DR 虚拟环境中的标准新 Django 项目。精确命令:

mkdir -p /home/alarm/public_html/hamstergangster.eu
cd /home/alarm/public_html/hamstergangster.eu
virtualenv venv -p python3.5
source venv/bin/activate
pip install django
django-admin startproject hamstergangster
cd hamstergangster
django-admin startapp helloapp #configuring helloapp to take care of / and return HttpResponse("Hello World")
./manage.py makemigrations
./manage.py migrate

在开发服务器上运行良好。

/home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/apache
__init__.py  override.py  wsgi.py

我认为不需要 wsgi.py 内容,因为它根本没有加载。

/etc/httpd/conf/httpd.conf
#Lines lines lines...
LoadModule wsgi_module modules/mod_wsgi.so
#Lines lines lines...
<VirtualHost *:80>
        ServerName localhost
        WSGIScriptAlias / /home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/wsgy.py

        <Directory /home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/apache/">
        <Files wsgi.py>
        Require all granted
        </Files>
        </Directory>

</VirtualHost>

所有内容都放在一个大的 httpd.conf 中(默认)

我尝试过的教程如下: http://pastebin.com/NTSC1Kj3 我从 django 的官方文档中了解到如何使用 virtualenvs

那么,我该怎么做才能让我心爱的“Hello World”出现在我的 Lynx 屏幕上呢?请注意,我是一名开发人员,而不是系统管理员,我忽略了一些相当明显的问题。
我已经检查了/home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/apache/wsgi.py路径每个部分的存在性和权限(通过将它们设置为 777)。

答案1

如果错误提示找不到文件

Target WSGI script not found or unable to stat: /home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/apache/wsgy.py

,尝试cat一下

cat /home/alarm/public_html/hamstergangster.eu/hamstergangster/hamstergangster/apache/wsgy.py

这帮助我注意到我犯的语法错误 - 它是wsgi.py,而不是。我在进入目录wsgy.py时没有注意到这一点:Dcd

相关内容