在文档根目录内为 Apache 启用单个 CGI 脚本?

在文档根目录内为 Apache 启用单个 CGI 脚本?

是否可以启用存储在 Apache 2.4 文档根目录中的单个 CGI 脚本?

我希望类似以下的事情能够发生:

<VirtualHost *:80>
    DocumentRoot "PATH/TO/htdocs/localhost"
    ServerName localhost
    ErrorLog "logs/localhost/errors.log"
    CustomLog "logs/localhost/access.log" common

    ScriptAlias "/python/" "PATH/TO/htdocs/localhost/python/index.py"
    <Files "PATH/TO/htdocs/localhost/python/index.py">
      Options +ExecCGI
      AddHandler cgi-script .py
      Require all granted
    </Files>
</VirtualHost>

或者我真的应该把它放在 CGI-bin 中吗?

答案1

Apache 确实不鼓励在 htdocs 中放置脚本。在我的例子中,这是本地开发机器上的后备虚拟主机,因此我可以使用以下方法:

PATH/TO/htdocs/localhost/index.html
PATH/TO/scripts/localhost/index.py

指定DirectoryIndex表示我更喜欢 python 文件而不是静态 html 文件。前者随后移动到如上所示的新位置。

<VirtualHost *:80>
    DocumentRoot "PATH/TO/htdocs/localhost"
    ServerName localhost
    ErrorLog "logs/localhost/errors.log"
    CustomLog "logs/localhost/access.log" common

    DirectoryIndex index.py index.php index.html

    ScriptAlias "/python/" "PATH/TO/scripts/localhost/python/index.py"
    <Files "PATH/TO/scripts/localhost/python/index.py">
      Options +ExecCGI
      AddHandler cgi-script .py
      Require all granted
    </Files>
</VirtualHost>

相关内容