CentOS 7上为PHP5安装suPHP的方法(彭哥)

CentOS 7上PHP默认是以apache或者nobody的身份运行的,这种方式下由于PHP运行需要的权限比较大,会有安全隐患,还可能会受到服务器其他用户影响。

通过phpinfo查看PHP信息如下:

CentOS 7上为PHP5安装suPHP的方法(彭哥) Linux 第1张

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身份运行,那么修改目录所有者属性为user,如下:

chown -R [user].[user] /var/www/html/[domainname]

最后在Apache配置文件中找到相应域名,并开启suphp:

<FilesMatch ".+.ph(p[345]?|t|tml)$">
SetHandler None
</FilesMatch>
<IfModule mod_suphp.c>
suPHP_Engine on
<FilesMatch ".php[345]?$">
SetHandler x-httpd-suphp
</FilesMatch>
suPHP_AddHandler x-httpd-suphp
</IfModule>

最后重启Apache文件。通过info.php测试可以发现运行该域名的Server API已经由Apache变成CGI/FastCGI了,如下图所示:

CentOS 7上为PHP5安装suPHP的方法(彭哥) Linux 第2张

到这里我们即完成了CentOS为某个域名访问设置通过suphp的方式来运行,而不用默认的Apache Handler运行。其他域名需要设置,按照以上步骤操作一遍即可。

参与评论