阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS安装PHP-5.6.4+扩展安装+安全配置+性能配置

146次阅读
没有评论

共计 17132 个字符,预计需要花费 43 分钟才能阅读完成。

注:以下所有操作均在 CentOS 6.5 x86_64 位系统下完成。

# 准备工作 #

前段时间 PHP 官方发布了一个重要的安全升级公告,修复了两个 unserialize 函数的严重漏洞,目前受影响的版本有:

  • <5.4.36
  • <5.5.20
  • <5.6.4

这里我们直接下载 5.6.4 的版本进行安装配置,并且在这之前需要先把 MySQL 和 Apache 已经安装好,最好 Nginx 也先安装好,具体见:《CentOS 6.5 安装 MySQL 5.6.10 及安全配置》、《CentOS 安装 Apache-2.4.10 及安全配置》。

#PHP 的安装 #

开始下载 PHP 并进行编译安装:

# wget http://cn2.php.net/distributions/php-5.6.4.tar.gz
# tar zxf php-5.6.4.tar.gz
# cd php-5.6.4
# export LD_LIBRARY_PATH=/usr/local/mysql/lib
# ./configure --prefix=/usr/local/php-5.6.4 --with-config-file-path=/usr/local/php-5.6.4/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-apxs2=/usr/local/apache/bin/apxs --enable-fpm --enable-shared --enable-zip --with-bz2 --enable-ftp --with-jpeg-dir --with-png-dir --with-freetype-dir --with-libxml-dir --with-xmlrpc --with-zlib-dir --with-gd --with-gmp --enable-gd-native-ttf --with-curl --with-regex=php --with-pic --with-xsl --enable-wddx --with-iconv --with-gettext --with-pear --enable-ctype --enable-calendar --enable-mbstring --enable-bcmath --enable-sockets  --enable-exif --disable-rpath  --with-mcrypt --with-mhash  --with-openssl --enable-sysvsem --enable-sigchild --enable-sysvshm --enable-soap  --disable-fileinfo --enable-opcache=no
...
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

# make
...
Build complete.
Don't forget to run 'make test'.

# make install
...
Installing PHP SAPI module:       apache2handler
/usr/local/apache-2.4.10/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libphp5.la /usr/local/apache-2.4.10/modules
/usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache-2.4.10/modules/
install .libs/libphp5.so /usr/local/apache-2.4.10/modules/libphp5.so
install .libs/libphp5.lai /usr/local/apache-2.4.10/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /usr/local/src/php-5.6.4/libs'chmod 755 /usr/local/apache-2.4.10/modules/libphp5.so
[activating module `php5' in /usr/local/apache-2.4.10/conf/httpd.conf]
Installing PHP CLI binary:        /usr/local/php-5.6.4/bin/
Installing PHP CLI man page:      /usr/local/php-5.6.4/php/man/man1/
Installing PHP FPM binary:        /usr/local/php-5.6.4/sbin/
Installing PHP FPM config:        /usr/local/php-5.6.4/etc/
Installing PHP FPM man page:      /usr/local/php-5.6.4/php/man/man8/
Installing PHP FPM status page:   /usr/local/php-5.6.4/php/fpm/
Installing PHP CGI binary:        /usr/local/php-5.6.4/bin/
Installing PHP CGI man page:      /usr/local/php-5.6.4/php/man/man1/
Installing build environment:     /usr/local/php-5.6.4/lib/php/build/
Installing header files:          /usr/local/php-5.6.4/include/php/
Installing helper programs:       /usr/local/php-5.6.4/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php-5.6.4/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php-5.6.4/lib/php/
[PEAR] Archive_Tar    - installed: 1.3.12
[PEAR] Console_Getopt - installed: 1.3.1
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       - installed: 1.2.3
[PEAR] PEAR           - installed: 1.9.5
Wrote PEAR system config file at: /usr/local/php-5.6.4/etc/pear.conf
You may want to add: /usr/local/php-5.6.4/lib/php to your php.ini include_path
/usr/local/src/php-5.6.4/build/shtool install -c ext/phar/phar.phar /usr/local/php-5.6.4/bin
ln -s -f /usr/local/php-5.6.4/bin/phar.phar /usr/local/php-5.6.4/bin/phar
Installing PDO headers:          /usr/local/php-5.6.4/include/php/ext/pdo/

# ln -s /usr/local/php-5.6.4/ /usr/local/php

给 PHP 的 upload 和 session 添加目录支持:

# mkdir -p /data/php/upload
# mkdir -p /data/php/session

至此,PHP 已经安装完毕。

#PHP-FPM 的启动 / 关闭 / 重启 #

自 PHP-5.3.3 开始,php 源码中包含了 php-fpm,不需要再单独通过布丁的方式来安装 php-fpm,在编译的时候加入参数 –enable-fpm 即可。

FPM(FastCGI Process Manager) 用于替换 PHP FastCGI 的大部分附加功能,对于高负载网站非常有用。

首先将 bin/php-fpm 加入到系统 PATH 中:

# vim /etc/profile

export PHP_HOME=/usr/local/php
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin

$ source /etc/profile

$ php -version
PHP 5.6.4 (cli) (built: Jan  3 2015 11:16:17)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

然后复制默认的配置文件:

# cp /usr/local/src/php-5.6.4/php.ini-production /usr/local/php-5.6.4/etc/php.ini
# cp /usr/local/php-5.6.4/etc/php-fpm.conf.default /usr/local/php-5.6.4/etc/php-fpm.conf

接下来修改默认配置文件:

# vim /usr/local/php/etc/php.ini

date.timezone = Asia/Shanghai

# vim /usr/local/php/etc/php-fpm.conf

[global]
pid = /usr/local/php-5.6.4/var/run/php-fpm.pid
error_log = /usr/local/php-5.6.4/var/log/php-fpm.log
log_level = error

[www]
user = www
group = www
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0666

添加启动脚本,之后可以使用 service 来启动 php-fpm 程序,并且设置开机自启动:

# cp /usr/local/src/php-5.6.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# service php-fpm
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}
# chkconfig php-fpm on

启动 php-fpm:

# service php-fpm start
Starting php-fpm  done

如果启动的时候出现错误(实际操作中发现存在该情况):

Starting php-fpm /usr/local/php-5.6.4/sbin/php-fpm: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
 failed

则将 MySQL 的 so 库文件 copy 到系统下:

# cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

再来重新启动 PHP,这次启动成功。

#PHP+Nginx#

现在来编辑 nginx.conf 以支持 php 解析,查看当前 php 环境是否可以正常运行:

# vim /usr/local/nginx/conf/nginx.conf

http {
    server {listen       80;
        server_name  localhost;
        root         html;
        index        index.html index.php;
        location ~ \.php$ {try_files      $uri = 404;
            include        fastcgi.conf;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}

然后编写一个简单的 php 文件,用来显示当前 PHP 环境信息:

# vim /usr/local/nginx-1.6.2/html/phpinfo.php

<?php
phpinfo();
?>

# chown -R www:www /usr/local/nginx-1.6.2

启动 Nginx,并打开浏览器访问地址 http://youripaddress/phpinfo.php 应该可以看到:

CentOS 安装 PHP-5.6.4+ 扩展安装 + 安全配置 + 性能配置

至此,Nginx 与 PHP 已经可以正常协同工作。

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2016-12/138047p2.htm

#PHP 的扩展安装 #

很多时候我们还需要给 PHP 安装各种扩展支持,比如 memcache、redis、mongodb 等,下面用几个例子来说明下。

1、安装 memcache 扩展:

# wget http://pecl.php.net/get/memcache-2.2.7.tgz
# tar zxf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/local/php-5.6.4/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.6.4/bin/php-config
# make && make install
Installing shared extensions:     /usr/local/php-5.6.4/lib/php/extensions/no-debug-zts-20131226/

2、安装 memcached 扩展(支持 SASL):

1)首先可以使用 yum 安装 SASL 环境:

# yum install cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib

2)然后下载并安装 libmemecached:

# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
# tar zxf libmemcached-1.0.18.tar.gz
# cd libmemcached-1.0.18
# ./configure --prefix=/usr/local/libmemcached-1.0.18 --enable-sasl
# make && make install

3)最后再安装 memcached 扩展:

# wget http://pecl.php.net/get/memcached-2.2.0.tgz
# tar zxf memecached-2.2.0.tgz
# cd memecached-2.2.0
# /usr/local/php-5.6.4/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.6.4/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached-1.0.18 --enable-memcached-sasl
# make && make install
Installing shared extensions:     /usr/local/php-5.6.4/lib/php/extensions/no-debug-zts-20131226/

注:前面安装的 libmemcached 必须支持 SASL,否则可能出现如下错误:

configure: error: no, libmemcached sasl support is not enabled. Run configure with --disable-memcached-sasl to disable this check

3、安装 redis 扩展:

# wget http://pecl.php.net/get/redis-2.2.7.tgz
# tar zxf redis-2.2.7.tgz
# cd redis-2.2.7
# /usr/local/php-5.6.4/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.6.4/bin/php-config
# make && make install
Installing shared extensions:     /usr/local/php-5.6.4/lib/php/extensions/no-debug-zts-20131226/

扩展安装完之后,还要打开 php.ini 文件编辑如下:

# vim /usr/local/php-5.6.4/etc/php.ini

extension=memcache.so
extension=memcached.so
extension=redis.so

#PHP 的安全配置# 

1、控制脚本访问权限。由于 PHP 默认配置允许 PHP 脚本程序访问服务器上的任意文件,为避免 PHP 脚本访问不该访问的文件,需要设置 PHP 只能访问网站目录或其他必须可访问的目录。比如:

# vim /usr/local/php/etc/php.ini

open_basedir=/data/php/uploads:/data/www/proj1:/data/www/proj2

2、禁止使用 PHP 危险函数,这些函数都是 PHP 木马常用的,比如:

# vim /usr/local/php/etc/php.ini

disable_functions = dl,assert,exec,popen,system,passthru,shell_exec,proc_close,proc_open,pcntl_exec

3、关闭注册全局变量(PHP-5.3.* 和 PHP-5.4.* 中已废除)

register_globals = Off

4、开启 magic_quotes_gpc(PHP-5.3.* 和 PHP-5.4.* 中已废除),由于 magic_quotes_gpc 会把引用的数据中包含单引号 ’ 和双引号 ” 以及反斜线 \ 自动加上反斜线,自动转译符号,确保数据操作的正确运行,magic_quotes_gpc 的设定值将会影响通过 Get/Post/Cookies 获得的数据,可以有效的防止 SQL 注入漏洞。

magic_quotes_gpc = On

5、关闭错误信息提示,因为这些错误信息可能泄漏服务器的路径信息和数据库信息等。

display_errors = Off

6、开启错误日志记录,可以考虑跟 Web 服务器的日志放在一起,比如:

log_errors = On
error_log = /data/logs/php/php_error.log

7、禁止访问远程文件,因为访问 URL 远程资源使得 PHP 应用程序的漏洞变得更加容易被利用,关闭之,如果要访问远程服务器建议采用其他方式比如 libcurl 库。

allow_url_fopen = Off
allow_url_include = Off

8、开启 PHP 安全模式(PHP-5.3.* 和 PHP-5.4.* 中已废除)

safe_mode = On

9、补上 Nginx 文件解析漏洞。

cgi.fix_pathinfo = 0

10、确保 PHP(FastCGI)以非 root 权限启动。如果是 php-cgi 进程,需要 su 道普通用户再启动;php-fpm 进程默认已是非 root 用户进行,配置中配置即可,不能修改为 root 运行。比如这里:

root     28953  0.0  1.1 196060  5736 ?        Ss   12:21   0:00 php-fpm: master process (/usr/local/php-5.6.4/etc/php-fpm.conf)
www      28954  0.0  1.0 196060  5504 ?        S    12:21   0:00 php-fpm: pool www
www      28955  0.0  1.0 196060  5504 ?        S    12:21   0:00 php-fpm: pool www
root     28974  0.0  0.1 103252   836 pts/0    S+   12:22   0:00 grep php-fpm

注:这里只有 master 是 root 用户权限,其他两个 pool 中的进程都是 www 用户,这是正确的。

#PHP 的性能配置 #

性能配置主要是为了让 PHP 能够运行得更好,这里很多时候需要根据业务的需求和当前系统的配置来设置,以下的配置只作为参考作用。

1、配置上传文���大小限制(一般不超过 2MB)

# vim /usr/local/php/etc/php.ini

file_uploads = On
upload_tmp_dir = /data/php/upload
upload_max_filesize = 5M
post_max_size = 8M

max_execution_time = 30
max_input_time = 60
memory_limit = 32M

2、使用阿里云的 OCS(memcache)来代替文件作为 session 的存储(这里需要前面安装 memcached 的扩展库,并且支持 SASL),比如:

# vim /usr/local/php/etc/php.ini

;session.save_handler = files
;session.save_path = "/tmp"
;session.save_path = "/data/php/session"
;session.gc_maxlifetime = 1440

session.save_handler = memcached
session.save_path = "something.m.cnszalist3pub001.ocs.aliyuncs.com:11211"
session.gc_maxlifetime = 3600

[memcached]
memcached.use_sasl = On
memcached.sess_binary = On
memcached.sess_sasl_username = "yourusername"
memcached.sess_sasl_password = "yourpassword"
memcached.sess_locking = Off
memcached.sess_prefix = "memc.sess.key."

注:使用其他的诸如 memcache 或 redis 缓存也是类似上面的配置。

阿里云的 OCS 是分布式集群统一对外提供服务,实现了负载均衡且无单点故障。对比自建 Memcached 最大的区别就是“账号密码鉴权”。因为 OCS 是对外提供服务的,所以需要有白名单、流控、账号密码鉴权等安全机制。

由于 OCS 实现了标准的 memcached 协议,采用 SASL 鉴权流程,这也是为什么前面我们在添加 memcached 扩展的时候需要引入 SASL 支持。

之后可以编写一个简单的 OCS 的 php 代码来进行测试,比如:

# vim ocs.php

<?php
$mem = new Memcached('ocs');
$mem->setOption(Memcached::OPT_COMPRESSION, false);
$mem->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$mem->addServer("something.m.cnszalist3pub001.ocs.aliyuncs.com", 11211);
$mem->setSaslAuthData("yourusername", "yourpassword");

$key = 'key';
$mem->set($key, 'ocs cache value');
$cache = $mem->get($key);
if (empty($cache)) {echo 'Oh, No!';
} else {echo "Thanks God, the cache value is'{$cache}'";
}
?>

在 CentOS 7.x / Fedora 21 上面体验 PHP 7.0  http://www.linuxidc.com/Linux/2015-05/117960.htm 

CentOS 6.3 安装 LNMP (PHP 5.4,MyySQL5.6) http://www.linuxidc.com/Linux/2013-04/82069.htm 

在部署 LNMP 的时候遇到 Nginx 启动失败的 2 个问题 http://www.linuxidc.com/Linux/2013-03/81120.htm 

CentOS 7 下安装配置 PHP7 跟 LAMP 及 MongoDB 和 Redis  http://www.linuxidc.com/Linux/2016-10/136521.htm

《细说 PHP》高清扫描 PDF+ 光盘源码 + 全套教学视频 http://www.linuxidc.com/Linux/2014-03/97536.htm 

CentOS 6 中配置 PHP 的 LNMP 的开发环境  http://www.linuxidc.com/Linux/2013-12/93869.htm 

PHP 的详细介绍 :请点这里
PHP 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-12/138047.htm

注:以下所有操作均在 CentOS 6.5 x86_64 位系统下完成。

# 准备工作 #

前段时间 PHP 官方发布了一个重要的安全升级公告,修复了两个 unserialize 函数的严重漏洞,目前受影响的版本有:

  • <5.4.36
  • <5.5.20
  • <5.6.4

这里我们直接下载 5.6.4 的版本进行安装配置,并且在这之前需要先把 MySQL 和 Apache 已经安装好,最好 Nginx 也先安装好,具体见:《CentOS 6.5 安装 MySQL 5.6.10 及安全配置》、《CentOS 安装 Apache-2.4.10 及安全配置》。

#PHP 的安装 #

开始下载 PHP 并进行编译安装:

# wget http://cn2.php.net/distributions/php-5.6.4.tar.gz
# tar zxf php-5.6.4.tar.gz
# cd php-5.6.4
# export LD_LIBRARY_PATH=/usr/local/mysql/lib
# ./configure --prefix=/usr/local/php-5.6.4 --with-config-file-path=/usr/local/php-5.6.4/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-apxs2=/usr/local/apache/bin/apxs --enable-fpm --enable-shared --enable-zip --with-bz2 --enable-ftp --with-jpeg-dir --with-png-dir --with-freetype-dir --with-libxml-dir --with-xmlrpc --with-zlib-dir --with-gd --with-gmp --enable-gd-native-ttf --with-curl --with-regex=php --with-pic --with-xsl --enable-wddx --with-iconv --with-gettext --with-pear --enable-ctype --enable-calendar --enable-mbstring --enable-bcmath --enable-sockets  --enable-exif --disable-rpath  --with-mcrypt --with-mhash  --with-openssl --enable-sysvsem --enable-sigchild --enable-sysvshm --enable-soap  --disable-fileinfo --enable-opcache=no
...
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

# make
...
Build complete.
Don't forget to run 'make test'.

# make install
...
Installing PHP SAPI module:       apache2handler
/usr/local/apache-2.4.10/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libphp5.la /usr/local/apache-2.4.10/modules
/usr/local/apr/build-1/libtool --mode=install install libphp5.la /usr/local/apache-2.4.10/modules/
install .libs/libphp5.so /usr/local/apache-2.4.10/modules/libphp5.so
install .libs/libphp5.lai /usr/local/apache-2.4.10/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /usr/local/src/php-5.6.4/libs'chmod 755 /usr/local/apache-2.4.10/modules/libphp5.so
[activating module `php5' in /usr/local/apache-2.4.10/conf/httpd.conf]
Installing PHP CLI binary:        /usr/local/php-5.6.4/bin/
Installing PHP CLI man page:      /usr/local/php-5.6.4/php/man/man1/
Installing PHP FPM binary:        /usr/local/php-5.6.4/sbin/
Installing PHP FPM config:        /usr/local/php-5.6.4/etc/
Installing PHP FPM man page:      /usr/local/php-5.6.4/php/man/man8/
Installing PHP FPM status page:   /usr/local/php-5.6.4/php/fpm/
Installing PHP CGI binary:        /usr/local/php-5.6.4/bin/
Installing PHP CGI man page:      /usr/local/php-5.6.4/php/man/man1/
Installing build environment:     /usr/local/php-5.6.4/lib/php/build/
Installing header files:          /usr/local/php-5.6.4/include/php/
Installing helper programs:       /usr/local/php-5.6.4/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php-5.6.4/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php-5.6.4/lib/php/
[PEAR] Archive_Tar    - installed: 1.3.12
[PEAR] Console_Getopt - installed: 1.3.1
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       - installed: 1.2.3
[PEAR] PEAR           - installed: 1.9.5
Wrote PEAR system config file at: /usr/local/php-5.6.4/etc/pear.conf
You may want to add: /usr/local/php-5.6.4/lib/php to your php.ini include_path
/usr/local/src/php-5.6.4/build/shtool install -c ext/phar/phar.phar /usr/local/php-5.6.4/bin
ln -s -f /usr/local/php-5.6.4/bin/phar.phar /usr/local/php-5.6.4/bin/phar
Installing PDO headers:          /usr/local/php-5.6.4/include/php/ext/pdo/

# ln -s /usr/local/php-5.6.4/ /usr/local/php

给 PHP 的 upload 和 session 添加目录支持:

# mkdir -p /data/php/upload
# mkdir -p /data/php/session

至此,PHP 已经安装完毕。

#PHP-FPM 的启动 / 关闭 / 重启 #

自 PHP-5.3.3 开始,php 源码中包含了 php-fpm,不需要再单独通过布丁的方式来安装 php-fpm,在编译的时候加入参数 –enable-fpm 即可。

FPM(FastCGI Process Manager) 用于替换 PHP FastCGI 的大部分附加功能,对于高负载网站非常有用。

首先将 bin/php-fpm 加入到系统 PATH 中:

# vim /etc/profile

export PHP_HOME=/usr/local/php
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin

$ source /etc/profile

$ php -version
PHP 5.6.4 (cli) (built: Jan  3 2015 11:16:17)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

然后复制默认的配置文件:

# cp /usr/local/src/php-5.6.4/php.ini-production /usr/local/php-5.6.4/etc/php.ini
# cp /usr/local/php-5.6.4/etc/php-fpm.conf.default /usr/local/php-5.6.4/etc/php-fpm.conf

接下来修改默认配置文件:

# vim /usr/local/php/etc/php.ini

date.timezone = Asia/Shanghai

# vim /usr/local/php/etc/php-fpm.conf

[global]
pid = /usr/local/php-5.6.4/var/run/php-fpm.pid
error_log = /usr/local/php-5.6.4/var/log/php-fpm.log
log_level = error

[www]
user = www
group = www
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0666

添加启动脚本,之后可以使用 service 来启动 php-fpm 程序,并且设置开机自启动:

# cp /usr/local/src/php-5.6.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# service php-fpm
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}
# chkconfig php-fpm on

启动 php-fpm:

# service php-fpm start
Starting php-fpm  done

如果启动的时候出现错误(实际操作中发现存在该情况):

Starting php-fpm /usr/local/php-5.6.4/sbin/php-fpm: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
 failed

则将 MySQL 的 so 库文件 copy 到系统下:

# cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

再来重新启动 PHP,这次启动成功。

#PHP+Nginx#

现在来编辑 nginx.conf 以支持 php 解析,查看当前 php 环境是否可以正常运行:

# vim /usr/local/nginx/conf/nginx.conf

http {
    server {listen       80;
        server_name  localhost;
        root         html;
        index        index.html index.php;
        location ~ \.php$ {try_files      $uri = 404;
            include        fastcgi.conf;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}

然后编写一个简单的 php 文件,用来显示当前 PHP 环境信息:

# vim /usr/local/nginx-1.6.2/html/phpinfo.php

<?php
phpinfo();
?>

# chown -R www:www /usr/local/nginx-1.6.2

启动 Nginx,并打开浏览器访问地址 http://youripaddress/phpinfo.php 应该可以看到:

CentOS 安装 PHP-5.6.4+ 扩展安装 + 安全配置 + 性能配置

至此,Nginx 与 PHP 已经可以正常协同工作。

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2016-12/138047p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计17132字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中