【问题标题】:Running Python 3 Script on Apache 2.4 Web Server在 Apache 2.4 Web 服务器上运行 Python 3 脚本
【发布时间】:2019-11-17 23:23:09
【问题描述】:

我在 localhost 上安装并运行了 Apache 2.4。我正在尝试通过编辑 httpd.conf 让 CGI 工作,以便它能够运行我的 python 脚本。

我现在想要它做的就是能够打印“Hello World”,所以我知道它正在工作。

print("hello world")

这是我提交这篇文章时的 httpd.conf 文件的副本。

Define SRVROOT "/apache24ah64"

ServerRoot "${SRVROOT}"


LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule allowmethods_module modules/mod_allowmethods.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule include_module modules/mod_include.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule setenvif_module modules/mod_setenvif.so

<IfModule unixd_module>
User daemon
Group daemon

</IfModule>

ServerAdmin admin@example.com
<Directory C:\Apache24\htdocs>
Options +ExecCGI
AddHandler cgi-script .cgi py
</Directory>
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
    Options Indexes FollowSymLinks FollowSymLinks ExecCGI

    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/ "${SRVROOT}/cgi-bin/"

</IfModule>

<IfModule cgid_module>

</IfModule>

<Directory "${SRVROOT}/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule headers_module>
    RequestHeader unset Proxy early
</IfModule>

<IfModule mime_module>
    TypesConfig conf/mime.types

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddHandler cgi-script .cgi .py

</IfModule>

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


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

我查看了文档和几个不同的教程,但我无法找到问题所在。

python 脚本位于 C:\Apache24\htdocs Python 3.7 版 阿帕奇 2.4

对 httpd.conf 文件的更改:

  1. 选项索引 FollowSymLinks ExecCGI

  2. AddHandler cgi-script .cgi .py

  3. LoadModule cgid_module modules/mod_cgid.so

【问题讨论】:

    标签: python python-3.x apache


    【解决方案1】:

    我有一个像 index.py 这样运行的 python 脚本:

    DirectoryIndex index.py
    
    <Directory "/var/www/xyz">
        Options +ExecCGI
        AddHandler cgi-script .py
    </Directory>
    

    我认为您的论点是正确的,但它们需要在 &lt;Directory&gt; 部分中。

    【讨论】:

    • &lt;Directory C:\Apache24\htdocs&gt; Options +ExecCGI AddHandler cgi-script .cgi py &lt;/Directory&gt;我已经添加了
    • 我错过了,您是否尝试将路径放在目录行中的引号中:“C:\Apache24\htdocs”?
    • 加引号还是不行。不过感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 2018-01-30
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2014-09-13
    • 2016-12-08
    相关资源
    最近更新 更多