搭建 Apache + php (cgi) 环境
背景陈述
我曾经用Apache + php建立过环境,但只用过模块版的php来进行环境搭建,因此我将这个步骤留下来。
因为想要先用普通的 cgi 运行,所以这篇文章使用了 cgi。与模块版相比,模块版的运行速度更快,但实用性较低。
如果使用Web服务器来使用cgi版本,应该使用php-fpm。
在工作中,我通常会从源代码进行安装,但由于本次只需要确认设置文件而不需要将其作为Web服务器公开,所以我们将使用yum进行安装。
为了在安装后能够清理干净,我们会在Docker容器上进行操作。
环境
主机操作系统:亚马逊Linux发行版2(Karoo)
容器操作系统:CentOS Linux发行版7.9.2009(Core)
Docker版本20.10.4
Apache/2.4.6
PHP 5.4.16(命令行界面)
步驟
准备货柜
创建并启动容器。
sudo docker run -tid -v /opt/app/:/var/www/html -p 80:80 centos:centos7
我会进入容器。
86 是容器的ID。
sudo docker exec -ti 86 bash
安装Apache
首先,执行 yum update。
yum update -y
然后,安装Apache。
yum install -y httpd
启动 Apache,并确认测试页面是否显示。
httpd -k start
当测试页面显示出来后,可以了。
PHP 安装
接下来安装PHP。
yum install -y php
为了确认 PHP 的运行情况,将文件放置在文档根目录进行检查。
vi /var/www/html/index.php
<?php echo phpinfo(); ?>
重启 Apache。
httpd -k stop
httpd -k start
当在浏览器中显示 phpinfo 时,一切正常。
通过查看 phpinfo,您可以确认当前的 PHP 是以模块方式还是 CGI 方式运行的。

目前,我們正使用模塊版運行,將逐步轉換為CGI版。
切换到CGI版
停止加载模块版PHP。
首先,需要禁止加载模块版的PHP。
请按照下面的方式进行注释处理。
#<IfModule prefork.c>
# LoadModule php5_module modules/libphp5.so
#</IfModule>
编辑 php.conf
默认情况下,php.conf 的内容如下所示。
#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
#
# Allow php to handle Multiviews
#
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
# SetHandler application/x-httpd-php-source
#</FilesMatch>
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"
我已经按下面的方式进行了编辑。
#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.(php|phps)$>
SetHandler php54-cgi
</FilesMatch>
#
# Allow php to handle Multiviews
#
#AddType text/html .php
Action php54-cgi /cgi-bin/php54
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
# SetHandler application/x-httpd-php-source
#</FilesMatch>
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
#php_value session.save_handler "files"
#php_value session.save_path "/var/lib/php/session"
以下是更改的内容。
-
- 因为.php文件被分配给了application/x-httpd-php的处理器,所以将其分配给php54-cgi处理器。(.phps也同样分配)
确保当请求php54-cgi处理器时调用/cgi-bin/php54。
CGI二进制文件的位置摆放
首先,您需要确认在 httpd.conf 文件中将 cgi 二进制文件放置在哪个目录下。然后,将 /cgi-bin/php54 路径写入 php.conf 文件中配置。
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
我明白了只需要将/var/www/cgi-bin目录下配置即可。
另外,还要确认 CGI 二进制文件的位置。
which php-cgi
/usr/bin/php-cgi
使用符号链接将CGI二进制文件放置在目标位置。
cd /var/www/cgi-bin/
ln -s /usr/bin/php-cgi php54
允许符号链接的设置
因为通过符号链接来放置 CGI 二进制文件,所以需要在 httpd.conf 中配置允许符号链接的设置。
<Directory "/var/www/cgi-bin">
AllowOverride None
Options FollowSymLinks
Require all granted
</Directory>
允许使用CGI的设置
这次我们将使用默认的文档根目录 /var/www/html,因此将其设置在这个目录中。
以下提供一种可能的中文翻译:正在描述选项 FollowSymLinks Includes ExecCGI。
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
请确认CGI正在运行中
只要在浏览器中输入URL,并且显示如下内容即可。

最终
这次作为学习的一部分,我尝试使用CGI来运行PHP。
下一步,我会使用更实用的php-fpm来撰写文章。
感谢您阅读到最后。