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

Rev*_*ech 6 apache django wsgi raspbian

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

我修改了httpd.conf以包含该行:

WSGIScriptAlias / /var/the-1/django/The1/apache/django.wsgi
Run Code Online (Sandbox Code Playgroud)

我创建了实际文件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()
Run Code Online (Sandbox Code Playgroud)

但是当我重新启动服务器时,我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下命令重启Apache:

/usr/local/apache2/bin/apachectl -k restart
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用命令:

sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)

我没有收到错误(虽然我认为他们并没有真正做同样的事情).

如果我运行apache2ctl -M,则会出现以下两行(以及其他行):

alias_module (shared)
wsgi_module (shared)
Run Code Online (Sandbox Code Playgroud)

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

我已经尝试完全卸载并重新安装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>
Run Code Online (Sandbox Code Playgroud)

编辑2(解决方案):我的上述配置文件存在一些问题.我错过了mod_wsgi.so的LoadModule行,我还必须注释掉其中一个部分.第一个解决方案中的注释会稍微介绍一下.

小智 19

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

/etc/init.d/httpd configtest 
Run Code Online (Sandbox Code Playgroud)

或者sudo apache2ctl -t

并检查模块是否已启用.

sudo a2enmod wsgi
Run Code Online (Sandbox Code Playgroud)