聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长

CentOS 7.2 安装 PHP 7 记录

2016-12-02 08:50 浏览: 2828816 次 我要评论(0 条) 字号:

PHP 7 极大地改进了性能,在一些WordPress基准测试当中,性能可以达到PHP 5.6的3倍。为了体验到更好的性能,尝试在自己的VPS使用最新的PHP 7,以下为整理的安装方法,期望对需要安装的小伙伴们有所帮助。

使用Yum安装PHP 7

php 7.0有两个源可以选择,一个是webtatic提供的,也就是php70w,那个w指得就是webtatic,另一个可以使用remi源。这里主要以webtatic进行展开讲解。

webtatic源

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum update
yum list | grep php7

查看具体的包信息:

[root@localhost ~]# yum list | grep php7
php70w-cli.x86_64                       7.0.13-1.w7                    @webtatic
php70w-common.x86_64                    7.0.13-1.w7                    @webtatic
php70w-fpm.x86_64                       7.0.13-1.w7                    @webtatic
php70w-gd.x86_64                        7.0.13-1.w7                    @webtatic
php70w-mcrypt.x86_64                    7.0.13-1.w7                    @webtatic
php70w-mysqlnd.x86_64                   7.0.13-1.w7                    @webtatic
php70w-opcache.x86_64                   7.0.13-1.w7                    @webtatic
php70w-pdo.x86_64                       7.0.13-1.w7                    @webtatic
php70w-pear.noarch                      1:1.10.1-1.w7                  @webtatic
php70w-pecl-imagick.x86_64              3.4.2-0.1.w7                   @webtatic
php70w-pecl-redis.x86_64                3.0.0-1.w7                     @webtatic
php70w-pgsql.x86_64                     7.0.13-1.w7                    @webtatic
php70w-process.x86_64                   7.0.13-1.w7                    @webtatic
php70w-xml.x86_64                       7.0.13-1.w7                    @webtatic
php70w.x86_64                           7.0.13-1.w7                    webtatic 
php70w-bcmath.x86_64                    7.0.13-1.w7                    webtatic 
php70w-dba.x86_64                       7.0.13-1.w7                    webtatic 
php70w-devel.x86_64                     7.0.13-1.w7                    webtatic 
php70w-embedded.x86_64                  7.0.13-1.w7                    webtatic 
php70w-enchant.x86_64                   7.0.13-1.w7                    webtatic 
php70w-imap.x86_64                      7.0.13-1.w7                    webtatic 
php70w-interbase.x86_64                 7.0.13-1.w7                    webtatic 
php70w-intl.x86_64                      7.0.13-1.w7                    webtatic 
php70w-ldap.x86_64                      7.0.13-1.w7                    webtatic 
php70w-mbstring.x86_64                  7.0.13-1.w7                    webtatic 
php70w-mysql.x86_64                     7.0.13-1.w7                    webtatic 
php70w-odbc.x86_64                      7.0.13-1.w7                    webtatic 
php70w-pdo_dblib.x86_64                 7.0.13-1.w7                    webtatic 
php70w-pecl-apcu.x86_64                 5.1.7-1.w7                     webtatic 
php70w-pecl-apcu-devel.x86_64           5.1.7-1.w7                     webtatic 
php70w-pecl-imagick-devel.x86_64        3.4.2-0.1.w7                   webtatic 
php70w-pecl-mongodb.x86_64              1.1.8-2.w7                     webtatic 
php70w-pecl-xdebug.x86_64               2.4.1-1.w7                     webtatic 
php70w-phpdbg.x86_64                    7.0.13-1.w7                    webtatic 
php70w-pspell.x86_64                    7.0.13-1.w7                    webtatic 
php70w-recode.x86_64                    7.0.13-1.w7                    webtatic 
php70w-snmp.x86_64                      7.0.13-1.w7                    webtatic 
php70w-soap.x86_64                      7.0.13-1.w7                    webtatic 
php70w-tidy.x86_64                      7.0.13-1.w7                    webtatic 
php70w-xmlrpc.x86_64                    7.0.13-1.w7                    webtatic

安装之前我们先学习下每个包的含义:

包名 包含内容 解释
php70w mod_php, php70w-zts mod_php模式运行PHP,服务器使用Apache的使用
php70w-bcmath 支持大整数计算的扩展
php70w-cli php-cgi, php-pcntl, php-readline CLI SAPI,命令行接口,该 CLI SAPI 模块主要用作 PHP 的开发外壳应用。
php70w-common php-api, php-bz2, php-calendar, php-ctype, php-curl, php-date, php-exif, php-fileinfo, php-filter, php-ftp, php-gettext, php-gmp, php-hash, php-iconv, php-json, php-libxml, php-openssl, php-pcre, php-pecl-Fileinfo, php-pecl-phar, php-pecl-zip, php-reflection, php-session, php-shmop, php-simplexml, php-sockets, php-spl, php-tokenizer, php-zend-abi, php-zip, php-zlib 常用包合集
php70w-dba dba数据库支持
php70w-devel 同php70w,包含头文件和链接库,供开发用
php70w-embedded php-embedded-devel Embed SAPI,容许你在C/C++语言中调用PHP/ZE提供的函数。
php70w-enchant 各种拼写库的抽象层
php70w-fpm fastcgi模式运行PHP,使用Nginx的推荐使用
php70w-gd 图形扩展GD库
php70w-imap 收取邮件类
php70w-interbase php_database, php-firebird InterBase数据库支持
php70w-intl 国际化扩展,是ICU库的一个包装器。
php70w-ldap ldap验证支持
php70w-mbstring 国际化与字符编码支持
php70w-mcrypt 常用加密算法的封装
php70w-mysql php-mysqli, php_database 同最新的mysqldnd
php70w-mysqlnd php-mysqli, php_database mysqldnd即mysql native driver简写,即是由PHP源码提供的mysql驱动连接代码.它的目的是代替旧的libmysql驱动.
php70w-odbc php-pdo_odbc, php_database 支持ODBC方式连接数据库
php70w-opcache php70w-pecl-zendopcache 脚本加速扩展
php70w-pdo php70w-pdo_sqlite, php70w-sqlite3 SQLite数据库支持
php70w-pdo_dblib php70w-mssql SQL Server数据库支持
php70w-pear PHP扩展与应用库,pecl 是通过pear包安装的
php70w-pecl-apcu APC 是将要被遗弃的脚本加速扩展
php70w-pecl-imagick ImageMagick支持
php70w-pecl-redis 让PHP支持Redis
php70w-pecl-xdebug Xdebug作为PHP调试工具,提供了丰富的调试函数,也可将Xdebug安装配置为zend studio、editplus调试PHP的第三方插件,通过开启自动跟踪(auto_trace)和分析器功能,可以直观的看到PHP源代码的性能数据,以便优化PHP代码。
php70w-pgsql php-pdo_pgsql, php_database PostgreSQL数据库支持
php70w-phpdbg PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境。
php70w-process php-posix, php-sysvmsg, php-sysvsem, php-sysvshm 进程控制扩展
php70w-pspell 拼写检查扩展
php70w-recode
php70w-snmp SNMP协议,供监控使用
php70w-soap SOAP 协议扩展
php70w-tidy 网页代码分析和纠错的工具
php70w-xml php-dom, php-domxml, php-wddx, php-xsl 方便解析XML的扩展
php70w-xmlrpc XML-RPC 协议扩展

比较容易混淆的概念:mod_php与FastCGI

任何一种Web服务器(Apache、Nginx等)都是被设计成向用户发送html、图片等静态资源的,Web服务器自身并不能解释任何动态脚本(PHP、Python等)。

mod_php

以Apache模块方式运行PHP。当PHP以模块的方式运行在Apache中时,PHP解释器被“内嵌”在Apache的进程里。Apache不会调用任何外部的PHP进程,因此这种方式使Apache与PHP能更好的通信。但是,当以这种方式运行PHP的时候,哪怕Apache提供的仅仅是静态的资源(如HTML),Apache的每个子进程也都会载入 mod_php,导致了比正常情况下更多的内存开销。以这种方式运行的另一个缺点是,它仅能与Apache一起配合工作。

优点:

  • 易于安装和更新
  • 容配置

缺点:

  • 仅能与Apache一起工作
  • 增加了Apache子进程内存开销
  • 当更改php.ini文件后,需要重启Apache

FastCGI

FastCGI是交互程序与Web服务器通用的协议接口,是早期CGI(Common Gateway Interface)的一个变种。相对于CGI来说,FastCGI减少了和Web服务器交互的开销,同时一次可以处理更多的请求。Apache可以以mod_fcgid的形式使用FastCGI。其他Web服务器,如lighttpd, nginx, Cherokee,甚至微软的IIS也都能使用FastCGI。使用FastCGI,可以同时设置多个版本的PHP,这在某些情况下非常有用。FastCGI还利用suexec来支持不同的用户用自己的PHP的实例。这个特性对于在共享环境下提高安全性尤其重要。FastCGI在保证性能的同时,也减少了Web服务器的内存开销。

优点:

  • 兼容多数Web服务器
  • 比mod_php占内存小
  • 更多的配置项,包括多版本PHP和suexec

缺点

  • 配置复杂
  • 不被大家所熟知

核心内容的安装:

  • Apache服务器:
    yum install php70w php70w-opcache
  • nginx服务器:
    yum install php70w-fpm php70w-opcache

扩展安装:(按需扩展)

yum install php70w-common php70w-pear php70w-pecl-imagick php70w-gd php70w-process php70w-mcrypt php70w-intl php70w-mbstring php70w-recode php70w-tidy php70w-xml php70w-soap php70w-xmlrpc php70w-mysqlnd php70w-pdo php70w-pecl-redis php70w-pgsql

至此,安装PHP的工作就简单的完成了,可以使用systemd管理相关服务:

systemctl enable php-fpm
systemctl start php-fpm

当然,这还没要,要能够使PHP正常运行还需要进一步的配置。

1、修复可能存在的漏洞

为了避免遭受恶意脚本注入的攻击,在没有文件存在的情况下,阻止 Nginx 将请求发送到后端的 PHP-FPM 模块。
将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0 。PHP 配置文件路径:/etc/php.ini。

2、修改为socket方式通信提升性能

PHP 默认配置文件使用的是监听 9000 端口进行通信,针对小型单一、没有做负债均衡的服务器,可以使用 unix socket 方式通信。

编辑/etc/php-fpm.d/www.conf 文件:

listen = 127.0.0.1:9000
,修改为
listen = /var/run/php-fpm/php-fpm.sock

;listen.owner = nobody
;listen.group = nobody

修改为(nginx执行的用户,可在 /etc/nginx/nginx.conf 中查看)

listen.owner = nginx
listen.group = nginx

如果与nginx.conf中的用户不一致,则会导致无法访问/var/run/php-fpm/php-fpm.sock。

3、设定php-fpm的执行用户及规划网站目录

添加用户:

groupadd www
useradd -s /sbin/nologin -g www www

将/etc/php-fpm.d/www.conf  中:

user = apache
group = apache

修改为:

user = www
group = www

即代表使用这个用户来运行php。

4、配置Nginx。

Nginx的默认配置文件为:/etc/nginx/nginx.conf,文件中自动include了/etc/nginx/conf.d/及/etc/nginx/conf.d/default.d/ 目录下所有的.conf结尾的文件,所以如需新增修改Nginx的配置只需要在这两个文件夹下新增和修改文件即可。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

其中/etc/nginx/conf.d/在http层级下,/etc/nginx/default.d/在server层级。

a.在/etc/nginx/conf.d/下新建nginx-fpm.conf文件,内容为:

# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/var/run/php-fpm/php-fpm.sock;
}

b./etc/nginx/default.d/下新建nginx-php.conf文件,内容为:

# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;

location ~ .php$ {
    try_files $uri =404;
    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass   php-fpm;
}

这样,默认的/usr/nginx/html目录就可以执行PHP程序了,先创建info.php文件:

echo '<?php phpinfo();' > /usr/nginx/html/info.php

然后访问http://IP/info.php进行访问就可看到正常的PHP信息。

如果需要绑定一个或多个域名,则需要先创建对应的文件夹:

mkdir /home/www/www.biaodianfu.com

然后在/etc/nginx/default.d/目录下新增www.biaodianfu.com.conf文件,内容如下:

server {
    listen       80;
    server_name  biaodainfu.com www.biaodianfu.com;
    index index.html index.htm index.php;
    charset utf-8;

    root   /home/www/www.biaodianfu.com;
    
    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass php-fpm;
        #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|mp3|wma)$
    {
        expires      30d;
    }
    
    location ~ .*.(js|css)$
    {
        expires      12h;
    }
}

这样一个新的站点就建立完成了。

remi源

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm

yum --enablerepo=remi update remi-release
yum --enablerepo=remi-php70

# 搜索PHP
yum search php70

# 各取所需
yum install php70 php70-php-devel php70-php-fpm php70-php-mbstring php70-php-mcrypt php70-php-mysqlnd php70-php-gd php70-php-opcache php70-php-pdo php70-php-json php70-php-xml php70-php-pecl-xdebug php70-php-pecl-zip

相应的服务:

systemctl start  php70-php-fpm.service
systemctl enable  php70-php-fpm.service

其他配置同上。

参考资料:



网友评论已有0条评论, 我也要评论

发表评论

*

* (保密)

Ctrl+Enter 快捷回复