CentOS 7安装配置suPHP实现PHP独立用户运行
在CentOS 7系统中,PHP通常默认以apache或nobody用户的身份运行。这种运行模式要求较高的系统权限,不仅容易引发安全隐患,还可能因共享进程而受到服务器上其他用户操作的干扰。
使用phpinfo函数查看当前的PHP运行信息,结果如下所示:

Apache运行PHP状态
从上述信息可以看出,PHP目前作为Apache服务的一部分进行运行,并未为每个脚本创建独立的进程。若希望PHP脚本在运行时能够以当前文件所属用户的身份执行,而非统一使用Apache身份,则需部署suPHP组件。下文将详细介绍在CentOS 7上安装suPHP的具体步骤。
首先,配置安装suphp所需的基础环境依赖:
yum -y groupinstall 'Development Tools' yum -y install apr-devel yum -y install httpd-devel
接下来下载suphp的安装包:
mkdir temp cd temp wget http://suphp.org/download/suphp-0.7.2.tar.gz tar zxvf suphp-0.7.2.tar.gz
随后下载并应用针对suphp的补丁程序:
wget -O patchingsuphp.patch https://www.webhostinghero.com/downloads/php/suphp.patch patch -Np1 -d suphp-0.7.2 < patchingsuphp.patch cd suphp-0.7.2 autoreconf -if
执行./configure命令进行配置:
./configure --prefix=/usr/ --sysconfdir=/etc/ --with-apr=/usr
/bin/apr-1-config --with-apache-user=apache --with-setid-mode=owner
--with-logfile=/var/log/httpd/suphp_log
完成配置后,进行编译并安装:
make make install
在Apache的配置目录下创建suphp.conf配置文件:
vi /etc/httpd/conf.d/suphp.conf
并在该文件中写入以下内容:
LoadModule suphp_module modules/mod_suphp.so
同时在/etc目录下创建suphp.conf主配置文件:
vi /etc/suphp.conf
并写入如下配置内容:
[global] ;Path to logfile logfile=/var/log/httpd/suphp.log ;Loglevel loglevel=info ;User Apache is running as webserver_user=apache ;Path all scripts have to be in docroot=/ ;Path to chroot() to before executing script ;chroot=/mychroot ; Security options allow_file_group_writeable=true allow_file_others_writeable=false allow_directory_group_writeable=true allow_directory_others_writeable=false ;Check wheter script is within DOCUMENT_ROOT check_vhost_docroot=true ;Send minor error messages to browser errors_to_browser=false ;PATH environment variable env_path=/bin:/usr/bin ;Umask to set, specify in octal notation umask=0077 ; Minimum UID min_uid=100 ; Minimum GID min_gid=100
[handlers] ;Handler for php-scripts x-httpd-suphp="php:/usr/bin/php-cgi" ;Handler for CGI-scripts x-suphp-cgi="execute:!self"
如果希望特定目录(如domainname)以指定用户(user)的身份运行,需要修改该目录的所有者属性,操作如下:
chown -R [user].[user] /var/www/html/[domainname]
最后,在Apache的配置文件中找到对应的域名虚拟主机配置,并开启suPHP支持:
SetHandler None suPHP_Engine on SetHandler x-httpd-suphp suPHP_AddHandler x-httpd-suphp
完成配置后重启Apache服务。通过访问info.php测试页面可以发现,该域名的Server API已由原来的Apache变为CGI/FastCGI,具体表现如下图所示:

至此,我们完成了在CentOS中为特定域名设置通过suPHP方式运行PHP的配置,使其不再使用默认的Apache Handler。对于其他需要类似设置的域名,按照上述步骤重复操作即可。