/--- date: 2017-01-09 20:34:43 +0800 categories: 技术 ---/

LEMP环境搭建(一)php7

系统安装完毕后,设置网络链接,安装辅助工具包等

ip addr show  #查看网卡信息(ip地址等)
ifup enp0s3 up #启动指定网卡(我这里为enp0s3)
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3   #设置开机启动:将 ONBOOT=no 置为 ONBOOT=yes

yum install  net-tools wget #安装网络工具包(包含ifconfig命令)、wget工具
ifconfig #查看ip地址,可以远程ssh链接登录

# 其他基本包安装,见[这里](https://github.com/dingzhihao/Code-Snippet/tree/master/vpn/centos)

reboot #验证以上设置

安装编译环境

yum install gcc gcc-c++ make autoconf

编译安装

  1. 安装configure选项配置依赖包

    yum install epel-release
    yum install systemd-devel systemtap-sdt-devel libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libicu-devel libmcrypt-devel readline-devel 
  2. configure选项配置

    • 如果线上环境,那么phpdbg和dtrace这两个建议不要编译
    • prefix 可根据实际情况自定义
    • with-fpm-systemd参数可以使PHP7支持centos7的systemd服务管理
    • 安装过程中可能有一些扩展需要额外库支持,如果configure运行报错,直接goolge 搜索,基本上yum install xxx-devel 就可
    wget http://cn2.php.net/distributions/php-7.1.0.tar.gz
    tar -zxvf php-7.1.0.tar.gz
    cd php-7.1.0
    ./configure --prefix=/usr/local/php71 \
     --with-config-file-path=/usr/local/php71/etc \
     --enable-dtrace \
     --enable-phpdbg \
     --enable-fpm \
     --with-fpm-systemd \
     --with-fpm-user=phpfpm \
     --with-fpm-group=www \
     --enable-mysqlnd \
     --with-mysqli=mysqlnd \
     --with-pdo-mysql=mysqlnd \
     --with-sqlite3 \
     --with-pdo-sqlite \
     --enable-bcmath \
     --enable-calendar \
     --enable-exif \
     --enable-filter \
     --enable-ftp \
     --enable-intl \
     --enable-mbregex \
     --enable-mbstring \
     --enable-opcache \
     --enable-pcntl \
     --enable-simplexml \
     --enable-soap \
     --enable-sockets \
     --enable-xmlreader \
     --enable-xmlwriter \
     --enable-zip \
     --with-curl \
     --with-freetype-dir \
     --with-gettext \
     --with-iconv \
     --with-mcrypt \
     --with-mhash \
     --with-openssl \
     --with-zlib \
     --with-gd \
     --enable-gd-native-ttf \
     --enable-gd-jis-conv \
     --with-jpeg-dir \
     --with-png-dir \
     --with-readline
  3. 编译、安装

    make
    make install  #以下为输出信息
    # Installing shared extensions:     /usr/local/php71/lib/php/extensions/no-debug-non-zts-20160303/
    # Installing PHP CLI binary:        /usr/local/php71/bin/
    # Installing PHP CLI man page:      /usr/local/php71/php/man/man1/
    # Installing PHP FPM binary:        /usr/local/php71/sbin/
    # Installing PHP FPM config:        /usr/local/php71/etc/
    # Installing PHP FPM man page:      /usr/local/php71/php/man/man8/
    # Installing PHP FPM status page:   /usr/local/php71/php/php/fpm/
    # Installing phpdbg binary:         /usr/local/php71/bin/
    # Installing phpdbg man page:       /usr/local/php71/php/man/man1/
    # Installing PHP CGI binary:        /usr/local/php71/bin/
    # Installing PHP CGI man page:      /usr/local/php71/php/man/man1/
    # Installing build environment:     /usr/local/php71/lib/php/build/
    # Installing header files:           /usr/local/php71/include/php/
    # Installing helper programs:       /usr/local/php71/bin/
    #   program: phpize
    #   program: php-config
    # Installing man pages:             /usr/local/php71/php/man/man1/
    #   page: phpize.1
    #   page: php-config.1
    # Installing PEAR environment:      /usr/local/php71/lib/php/
    # [PEAR] Archive_Tar    - installed: 1.4.0
    # [PEAR] Console_Getopt - installed: 1.4.1
    # [PEAR] Structures_Graph- installed: 1.1.1
    # [PEAR] XML_Util       - installed: 1.3.0
    # [PEAR] PEAR           - installed: 1.10.1
    # Wrote PEAR system config file at: /usr/local/php71/etc/pear.conf
    # You may want to add: /usr/local/php71/lib/php to your php.ini include_path
    # /root/php-7.1.0/build/shtool install -c ext/phar/phar.phar /usr/local/php71/bin
    # ln -s -f phar.phar /usr/local/php71/bin/phar
    # Installing PDO headers:           /usr/local/php71/include/php/ext/pdo/

配置php和php-fpm以及用户、用户组

cp php.ini-* /usr/local/php71/etc/ #php.ini配置文件
cp /usr/local/php71/etc/php.ini-production /usr/local/php71/etc/php.ini
vim /usr/local/php71/etc/php.ini #将cgi.fix_pathinfo=1置为cgi.fix_pathinfo=0
cp /usr/local/php71/etc/php-fpm.conf.default /usr/local/php71/etc/php-fpm.conf
cp /usr/local/php71/etc/php-fpm.d/www.conf.default /usr/local/php71/etc/php-fpm.d/www.conf
vim /usr/local/php71/etc/php-fpm.d/www.conf  # 修改用户和用户组为user = phpfpm 和 group = www,这里已通过编译选项已指定
groupadd www #添加用户组
useradd -g www -M -s /sbin/nologin -N phpfpm #添加用户

配置systemctl,启动、关闭、查看状态

cp ./sapi/fpm/php-fpm.service /usr/lib/systemd/system/
vim /usr/lib/systemd/system/php-fpm.service   # 将路径配置正确

systemctl enable php-fpm.service
systemctl start php-fpm.service
systemctl restart php-fpm.service
systemctl stop php-fpm.service
systemctl status php-fpm.service

path环境变量设置

vim /etc/profile
#在文档最后,添加:
# export PATH="$PATH:/usr/local/php71/bin:/usr/local/php71/sbin"
#或
# export PATH="/usr/local/php71/bin:/usr/local/php71/sbin:$PATH"
#保存,退出,然后运行:
source /etc/profile

附1:参考

http://www.shaunfreeman.name/compiling-php-7-on-centos/ https://segmentfault.com/a/1190000005363212 https://segmentfault.com/a/1190000004123048 http://fivezh.github.io/2016/08/02/PHP-Nginx-Memcached-Redis-configuration/ http://lyp.cn/350_how-to-fix-php-compile-errors http://raffaelexr.blog.51cto.com/8555551/1711783

http://blog.daydaygo.top/post/php-uniq-id?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

附2:其他命令

php --ini  #查看加载的php.ini文件
# php-fpm加载的php.ini文件,可以在配置好站点后,通过 phpinfo() 的输出看到