当前位置: 移动技术网 > 网络运营>服务器>Linux > CentOS 7上为PHP5安装suPHP的方法(彭哥)

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

2020年05月12日  | 移动技术网网络运营  | 我要评论

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

通过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身份运行,那么修改目录所有者属性为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为某个域名访问设置通过suphp的方式来运行,而不用默认的apache handler运行。其他域名需要设置,按照以上步骤操作一遍即可。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网