CentOS7.X安装php-7.x.x

我的个人网站

安装php

  1. 安装准备

    yum install \
    vim \
    gcc \
    gcc-c++ \
    wget \
    make \
    re2c \
    libtool \
    automake \
    autoconf \
    curl-devel \
    libpng-devel \
    libxml2-devel \
    libxslt-devel \
    openssl-devel \
    freetype-devel \
    bzip2-devel \
    libicu-devel \
    libuuid-devel \
    libmcrypt-devel \
    postgresql-devel \
    -y
  2. 安装php

    cd /root
    wget http://cn2.php.net/distributions/php-7.2.6.tar.gz
    tar -zxvf php-7.2.6.tar.gz
    cd php-7.2.6
    ./configure \
    --prefix=/usr/local/php \
    --enable-mysqlnd \
    --with-openssl \
    --enable-fpm
    
    # 如果在/usr/local/apache/路径下安装了apache并且想让php运行在apache下,需要加上:
    # --with-apxs2=/usr/local/apache/bin/apxs
    # 或 --with-apxs2=/usr/local/apache/bin/apxs2
    # apxs在有些apache发行版中已经改名为apxs2
    
    make
    make install
    
    # 如果在/usr/local/apache/路径下安装了apache并且想让php运行在apache下,apache不需要重新编译
    # 这时候/usr/local/apache/modules/下多了一个libphp7.so文件
    # 编辑/etc/httpd/httpd.conf添加如下行:
    
    LoadModule php7_module modules/libphp7.so
    DocumentRoot "/www"
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    
    # 编辑/www/index.php添加如下行:
    
    <?php
    phpinfo();
    
    # 重启apache
    # 访问http://xxx.xxx.xxx.xxx/index.php
  3. 生成php.ini、php-fpm.conf、www.conf

    # 检查php.ini是否正确放置(只需要第一行不为none)
    /usr/local/php/bin/php --ini
    
    部署环境执行:
    cp ./php.ini-production /usr/local/php/lib/php.ini
    
    开发环境执行:
    cp ./php.ini-development /usr/local/php/lib/php.ini
    
    # 拷贝php-fpm.conf
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    
    # PHP7才执行(PHP5中,www.conf的配置项在php-fpm.conf)
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
  4. php.ini常规配置

    # 下载cacert.pem
    cd /usr/local/php/lib
    wget https://curl.haxx.se/ca/cacert.pem
    
    vim /usr/local/php/lib/php.ini
    
    ;php版本信息隐藏
    expose_php=Off
    
    ;防止显示php错误输出,Off: 不输出但存入日志
    display_errors=Off
    
    ;防止用户将可执行文件伪装成静态文件(如图片、文档)上传后,通过访问WEB页面执行该文件
    cgi.fix_pathinfo=0
    
    ;允许上传单个文件最大20M
    upload_max_filesize=20M
    
    ;session.name如果自己开发程序可以改,但如果是部署已有项目,修改后可能导致session相关功能异常
    session.name=self_key
    
    ;每请求10次session的key,检查1次key是否需要过期删除
    session.gc_probability=1
    session.gc_divisor=1000
    
    ;设置session默认5分钟(300秒)过期
    session.gc_maxlifetime=1440
    
    ;时区中国上海
    date.timezone=Asia/Shanghai
    
    ;curl、openssl配置cacert.pem
    curl.cainfo=/usr/local/php/lib/cacert.pem
    openssl.cafile=/usr/local/php/lib/cacert.pem
    
    ESC
    :wq
  5. 配置php-fpm.conf

    vim /usr/local/php/etc/php-fpm.conf
    
    pid=run/php-fpm.pid
    error_log=log/php-fpm.log
    # 如开启该配置↓,下一步不可跳过
    rlimit_files=51200
    
    ESC
    :wq
  6. 调整最大文件打开数(数值仅供参考,如上一步骤未配置rlimit_files,本步可跳过)

    # 在文件末尾添加
    vim /etc/security/limits.conf
    
    * soft nproc 65535
    * hard nproc 65535
    * soft nofile 65535
    * hard nofile 65535
    
    ESC
    :wq
  7. 配置www.conf(内存较大、较小两套方案)

    vim /usr/local/php/etc/php-fpm.d/www.conf
    
    user=www
    group=www
    
    # 静态分配子进程(内存1G,计划为php分配 640M 内存)
    pm=static
    pm.max_children=20
    pm.start_servers=15
    pm.min_spare_servers=10
    pm.max_spare_servers=20
    pm.process_idle_timeout=10s
    pm.max_requests=500
    slowlog=log/$pool.log.slow
    php_admin_value[memory_limit]=32M
    
    # 动态分配子进程(内存1G,计划为php分配 640M 内存)
    pm=dynamic
    pm.max_children=20
    pm.start_servers=15
    pm.min_spare_servers=10
    pm.max_spare_servers=20
    pm.process_idle_timeout=10s
    pm.max_requests=500
    slowlog=log/$pool.log.slow
    php_admin_value[memory_limit]=32M
    
    ESC
    :wq
    
    # 以下配置仅供了解,不建议这样使用
    listen=127.0.0.1:9000 若改为 listen=/usr/local/php/var/run/php-fpm.sock
    则nginx配置 fastcgi_pass 127.0.0.1:9000; 要改为 fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;
  8. 将php加入环境变量

    vim /etc/profile
    
    export PATH=$PATH:/usr/local/php/bin
    
    ESC
    :wq
    
    source /etc/profile
  9. 安装composer

    # 下载(需要先安装zlib扩展)
    cd /usr/local/php/bin
    curl -sS https://getcomposer.org/installer | /usr/local/php/bin/php
    mv ./composer.phar ./composer
    composer -V
  10. 启动php并设置开机启动

    # 添加www用户
    useradd www
    mkdir /www
    chown -R www:www /www
    chown -R www:www /usr/local/php
    
    # 进入单元文件目录
    cd /etc/systemd/system
    vim php7.service
    
    [Unit]
    Description=Start php7 on boot.
    After=network.target
    
    [Service]
    User=root
    Group=root
    Type=forking
    ExecStart=/usr/local/php/sbin/php-fpm
    ExecReload=/bin/kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
    ExecStop=/bin/kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    ESC
    :wq
    
    # 修改文件权限为只有root用户可以编辑该文件
    chown -R root:root /etc/systemd/system/php7.service
    chmod -R 644 /etc/systemd/system/php7.service
    
    # 更新systemd
    systemctl daemon-reload
    systemctl enable php7
    systemctl start php7
  11. 查看运行情况

    # 查看主进程、子进程
    ps aux | grep php-fpm
    
    # 查看pid文件(存在即成功)
    ll /usr/local/php/var/run/
  12. php进程操作

    # 平滑关闭
    kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
    # 平滑重启
    kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
  13. 安装php扩展

    cd /root/php-7.2.6/ext/xxxx
    /usr/local/php/bin/phpize
    
    -----------------------------------------------------------
    | phpize时注意如下:
    | 这里如果说没发现config.m4
    | cp ./config0.m4 ./config.m4
    -----------------------------------------------------------
    
    ./configure --with-php-config=/usr/local/php/bin/php-config
    
    -----------------------------------------------------------
    | 安装GD库configure时注意如下:
    | 这里为了排版加了'|',按住Alt键选取吧...
    | yum install \
    | libXpm-devel \
    | libpng-devel \
    | libjpeg-devel \
    | libwebp-devel \
    | -y \
    |
    | ./configure \
    | --with-xpm-dir \
    | --with-png-dir \
    | --with-jpeg-dir \
    | --with-webp-dir \
    | --with-zlib-dir \
    | --with-freetype-dir \
    -----------------------------------------------------------
    
    make
    
    -----------------------------------------------------------
    | make注意如下:
    | 重复安装PHP预编译的扩展,会报错
    | Installing shared extensions:
    | /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
    | cp: cannot stat 'modules/*': No such file or directory
    | make: *** [install-modules] Error 1
    | 所以不要重复安装,通过 php -m 查看已经安装的扩展。
    -----------------------------------------------------------
    
    -----------------------------------------------------------
    | 这里注意,安装mcrypt要先安装libmcrypt(有些版本Linux系统)
    | cd /usr/local/src
    | wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
    | cd libmcrypt-2.5.7
    | ./configure
    | make
    | make install
    -----------------------------------------------------------
    
    make install
    
    vim /usr/local/php/lib/php.ini
    
    extension=xxxx.so
    
    ESC
    :wq
    
    Zend扩展:
    -----------------------------------------------------------
    | 设置opcache.max_accelerated_files时
    | 先执行以下命令来获取php文件数
    | find . -type f -print | grep php | wc -l
    | 然后在以下质数集合中,找到一个恰好比该值大的值
    | 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987
    | opcache安装,配置php.ini:
    |
    | zend_extension=opcache.so
    | opcache.enable_cli=1
    | opcache.memory_consumption=256
    | opcache.interned_strings_buffer=16
    | opcache.max_accelerated_files=32531
    | opcache.validate_timestamps=1
    | opcache.revalidate_freq=60
    | opcache.fast_shutdown=1
    -----------------------------------------------------------
  14. ZendOpcache的最佳设置参考链接
  15. 常用扩展('-'表示可能已经安装的扩展,安装前检查已安装扩展,防止重复安装)

    bcmath
    - ctype
    curl
    - date
    - fileinfo
    gd
    - hash
    - iconv
    intl
    - json
    mbstring
    mcrypt
    - mysqlnd
    - openssl
    - pcre
    - PDO
    pdo_mysql
    pdo_pgsql
    sockets
    zip
    zlib
    
    mongo (wget http://pecl.php.net/get/mongo-1.6.14.tgz [php7.x.x不再需要])
    mongodb (wget http://pecl.php.net/get/mongodb-1.2.9.tgz)
    redis (wget http://pecl.php.net/get/redis-3.1.2.tgz)
    uuid (wget http://pecl.php.net/get/uuid-1.0.4.tgz)
    fastcommon (见FastDFS安装)
    fastdfs_client (见FastDFS安装)
    
    Zend:
        opcache (文档地址: http://php.net/manual/zh/opcache.installation.php)
  16. php重新编译

    cd /root/php-7.2.6
    
    make distclean
    
    ./configure \
    --prefix=/usr/local/php \
    --enable-mysqlnd \
    --with-openssl \
    --enable-fpm \
    
    make
    make install
  17. 领支付宝红包支持作者

    CentOS7.X安装php-7.x.x

上一篇:CentOS7.X yum安装MySQL


下一篇:CentOS7.X安装Nginx