django - 无效的命令 'WSGIScriptAlias' ,可能拼写错误或由未包含在服务器配置中的模块定义

标签 django apache wsgi raspbian

我正在尝试为生产设置 Django 服务器。在我的浏览器中,如果我输入与我的服务器对应的 IP 地址,我会得到默认的 Apache 页面“它可以工作!”而不是 Django 相关的页面。

我已修改 httpd.conf 以包含以下行:

WSGIScriptAlias / /var/the-1/django/The1/apache/django.wsgi

我创建了实际的文件 django.wsgi,它看起来像:
import os
import sys

path = '/var/the-1/django/The1'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'The1.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

但是当我重新启动服务器时,我收到错误:
AH00526: Syntax error on line 506 of /usr/local/apache2/conf/httpd.conf:
Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configuration

当我尝试使用以下命令重新启动 Apache 时:
/usr/local/apache2/bin/apachectl -k restart

但是,如果我使用命令:
sudo service apache2 restart

我没有收到错误(尽管我认为他们并没有真正做同样的事情)。

如果我运行 apache2ctl -M,会出现以下两行(除其他外):
alias_module (shared)
wsgi_module (shared)

所以我相信这些模块运行正常。

我已经尝试过完全卸载并重新安装 libapache2-mod-wsgi。我正在使用的服务器是运行 Raspbian 的 Raspberry Pi。这是我第一次设置服务器,所以我对 Apache 或如何设置 Django 知之甚少。

编辑 : 下面是我的 httpd.conf 文件(注意因为实际文件太长,我已经删除了所有注释):
ServerRoot "/usr/local/apache2"

Listen 80

LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule wsgi_module modules/mod_wsgi.so

<IfModule unixd_module>
User daemon
Group daemon
</IfModule>

ServerAdmin you@example.com
ServerName [SERVER_IP_ADDRESS]:80 # Removed for security reasons

#<Directory />
#    AllowOverride none
#    Require all denied
#</Directory>

DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" common
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
</IfModule>

<IfModule cgid_module>
</IfModule>

<Directory "/usr/local/apache2/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
        TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>

<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>

<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

<VirtualHost *:80>
    ServerName [SERVER_IP_ADDRESS]:80 # Removed for security reasons
    WSGIScriptAlias / /var/the-1/django/The1/apache/django.wsgi
    <Directory /var/the-1/django/The1/apache>
        ###<Files django.wsgi>
            Order allow,deny
            Allow from all
        ###</Files>
    </Directory>
</VirtualHost>

编辑 2(解决方案) : 我上面的配置文件有一些问题。我缺少 mod_wsgi.so 的 LoadModule 行,我还必须注释掉其中一个部分。第一个解决方案中的评论稍微经历了一点。

最佳答案

首先,您可能想使用检查配置

/etc/init.d/httpd configtest 

或者
须藤 apache2ctl -t

并检查模块是否启用。
sudo a2enmod wsgi

关于django - 无效的命令 'WSGIScriptAlias' ,可能拼写错误或由未包含在服务器配置中的模块定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390637/

相关文章:

python - celery 周期性任务不执行

python - Django 模型与用户模型的关系

python - Django 调试服务器的代码覆盖率

PHP的exec()无法访问网络驱动器

Apache mod_rewrite 子域到子文件夹(通过内部重定向)

apache - Tomcat 7 部署的 Web 应用程序 404 状态

python - 从 WSGI 脚本获取 URL

python - Django WSGI多线程和连接问题

Django:以编程方式更改应用程序语言(不带/i18n/setlang/view)

python - Python Web 框架、WSGI 和 CGI​​ 如何组合在一起