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

CentOS7.3编译安装PHP7.1

180次阅读
没有评论

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

CentOS7.3 编译安装 PHP7.1

    • 说明
    • 安装依赖包
    • 创建用户及用户组
    • 下载 php71 并解压
    • 下载并安装 php 依赖
      • PHP 语法分析器 re2c
      • 加密支持扩展库 libmcrypt
      • 加密方式扩展库 mhash
      • 加密方式扩展库 mcrypt
    • 编译 php71
      • 查看源码目录文件具体内容
      • 生成 php 源码目录下的配置文件 configure
      • 生成源码目录配置文件 configure 之后的目录
      • 正式编译 php71
      • 可能会出现要求开启 –enable-opcacheon 的错误提示
      • 运行 make test
      • 编译安装后的 php 目录
    • 配置 php
      • 修改 php 配置文件之添加 php 配置文件
      • 修改 php 配置文件之修改 php 扩展目录所在位置
      • 修改 php 配置文件之修改服务器所在时区
      • 修改 php 配置文件之开启 OPcache
      • 修改 php 配置文件之开启 mysqli 扩展
      • 添加 php-fpm 管理相关的配置文件到系统配置目录 etcinitd
      • 添加 php-fpm 配置文件
      • 添加 wwwconf 配置文件
      • 添加 php 安装目录到系统环境变量
      • 设置 PHP 日志目录和 php-fpm 运行进程的 ID 文件目录
      • 修改 session 的目录配置
      • 设置 php 开机启动
      • 检测 php-fpm 系统服务是否启动成功
      • 测试 PHP 的配置文件是否无误
      • 启动 php 系统服务
      • 查看 php 系统服务状态
      • CLI 查看 php 版本信息
      • 修改 nginx 配置文件使之使用 nginx 反向 php 解释器
      • 浏览器访问

说明

安装 LNMP 环境一般是先安装 mysql/mariadb, 再安装 nginx, 其次是安装 php

安装依赖包

[root@localhost ~]# yum -y install gcc
[root@localhost ~]# yum -y install gcc++
[root@localhost ~]# yum -y install gcc-c++
[root@localhost ~]# yum -y install wget
[root@localhost ~]# yum -y install make
[root@localhost ~]# yum -y install libxml2 
[root@localhost ~]# yum -y install libxml2-devel 
[root@localhost ~]# yum -y install openssl 
[root@localhost ~]# yum -y install openssl-devel 
[root@localhost ~]# yum -y install curl-devel 
[root@localhost ~]# yum -y install libjpeg-devel 
[root@localhost ~]# yum -y install libpng-devel
[root@localhost ~]# yum -y install freetype-devel
[root@localhost ~]# yum -y install bison
[root@localhost ~]# yum -y install autoconf

创建用户及用户组

  • 创建 php 用户组
> 创建 php 用户组(- r 选项是创建一个系统用户组的意思)

[root@localhost ~]# groupadd -r php
  • 创建用户并加入到 php 系统用户组
> 添加新用户

> -r: 添加系统用户(这里指将要被创建的系统用户 php)

> -g: 指定要创建的用户所属组(这里指添加到新系统用户 php 到 php 系统用户组)

> -s: 新帐户的登录 shell(`/sbin/nologin` 这里设置为将要被创建系统用户 php 不能用来登录系统 )

> -d: 新帐户的主目录(这里指定将要被创建的系统用户 php 的家目录为 `/usr/local/php` )

> -M: 不要创建用户的主目录(也就是说将要被创建的系统用户 php 不会在 `/home` 目录下创建 `php` 家目录 )

[root@localhost ~]# useradd -r -g php -s /sbin/nologin -d /usr/local/php -M php

下载 php7.1 并解压

> 下载

[root@localhost ~]# wget -c https://github.com/php/php-src/archive/php-7.1.6.tar.gz

> 解压

[root@localhost ~]# tar -zxvf php-7.1.6.tar.gz

下载并安装 php 依赖

PHP 语法分析器 re2c

> 下载

[root@localhost ~]# wget -c https://github.com/skvadrik/re2c/releases/download/0.16/re2c-0.16.tar.gz

> 解压

[root@localhost ~]# tar -zxvf re2c-0.16.tar.gz

> 进入解压后的源码目录编译并安装

[root@localhost ~]# cd re2c-0.16

[root@localhost re2c-0.16]# ./configure

[root@localhost re2c-0.16]# make && make install

加密支持扩展库 libmcrypt

使用 php mcrypt 前必须先安装 Libmcrypt

> 下载

[root@localhost ~]# wget -c https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz

> 解压

[root@localhost ~]# tar -zxvf libmcrypt-2.5.8.tar.gz

> 进入解压后的源码目录编译并安装

[root@localhost ~]# cd libmcrypt-2.5.8

[root@localhost libmcrypt-2.5.8]# ./configure

[root@localhost libmcrypt-2.5.8]# # make && make install

加密方式扩展库 mhash

> 下载

[root@localhost ~]# wget -c https://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz

> 解压

[root@localhost ~]# tar -zxvf mhash-0.9.9.9.tar.gz

> 进入解压后的源码目录编译并安装

[root@localhost mhash-0.9.9.9]# cd mhash-0.9.9.9

[root@localhost mhash-0.9.9.9]# ./configure

[root@localhost mhash-0.9.9.9]# make && make install

加密方式扩展库 mcrypt

> 下载

[root@localhost ~]# wget -c https://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz

> 解压

[root@localhost ~]# tar -zxvf mcrypt-2.6.8.tar.gz 

> 由于在编译 mcrypt 时,会找不到 libmcrypt 的链接库,导致无法编译,因为 Libmcrypt 的链接库在 /usr/local/lib 文件夹下

> 所以,在编译 mcrypt 时要加入 LD_LIBRARY_PATH=/usr/local/lib 导入间接库

> 进入解压后的源码目录编译并安装

[root@localhost ~# cd mcrypt-2.6.8

[root@localhost mcrypt-2.6.8]# ./configure LD_LIBRARY_PATH=/usr/local/lib

[root@localhost mcrypt-2.6.8]# make && make install

编译 php7.1

到目录为止所有的准备工作已经完成, 接下来就是编译 php7.1 了!

查看源码目录文件具体内容

[root@localhost php-src-php-7.1.6]# ll
total 1252
-rw-rw-r--.  1 root root  82506 Jun  7 04:05 acinclude.m4
drwxrwxr-x.  2 root root     78 Jun  7 04:05 appveyor
drwxrwxr-x.  2 root root   4096 Jun  7 04:05 build
-rwxrwxr-x.  1 root root    772 Jun  7 04:05 buildconf
-rw-rw-r--.  1 root root    334 Jun  7 04:05 buildconf.bat
-rw-rw-r--.  1 root root  11982 Jun  7 04:05 CODING_STANDARDS
-rw-rw-r--.  1 root root  42938 Jun  7 04:05 config.guess
-rw-rw-r--.  1 root root  35987 Jun  7 04:05 config.sub
-rw-rw-r--.  1 root root  46774 Jun  7 04:05 configure.in
-rw-rw-r--.  1 root root   3163 Jun  7 04:05 CONTRIBUTING.md
-rw-rw-r--.  1 root root     91 Jun  7 04:05 CREDITS
drwxrwxr-x. 75 root root   4096 Jun  7 04:05 ext
-rw-rw-r--.  1 root root  20917 Jun  7 04:05 EXTENSIONS
-rw-rw-r--.  1 root root    137 Jun  7 04:05 footer
-rwxrwxr-x.  1 root root    581 Jun  7 04:05 genfiles
-rw-rw-r--.  1 root root   1143 Jun  7 04:05 header
-rw-rw-r--.  1 root root  87836 Jun  7 04:05 INSTALL
-rw-rw-r--.  1 root root   3218 Jun  7 04:05 LICENSE
-rw-rw-r--.  1 root root 199728 Jun  7 04:05 ltmain.sh
drwxrwxr-x.  3 root root   4096 Jun  7 04:05 main
-rwxrwxr-x.  1 root root   4129 Jun  7 04:05 makedist
-rw-rw-r--.  1 root root   1088 Jun  7 04:05 Makefile.frag
-rw-rw-r--.  1 root root   2485 Jun  7 04:05 Makefile.gcov
-rw-rw-r--.  1 root root   7236 Jun  7 04:05 Makefile.global
-rw-rw-r--.  1 root root   5317 Jun  7 04:05 makerpm
drwxrwxr-x.  2 root root     40 Jun  7 04:05 netware
-rw-rw-r--.  1 root root 135093 Jun  7 04:05 NEWS
drwxrwxr-x.  2 root root     65 Jun  7 04:05 pear
-rw-rw-r--.  1 root root   1489 Jun  7 04:05 php.spec.in
-rw-rw-r--.  1 root root   2523 Jun  7 04:05 php.gif
-rw-rw-r--.  1 root root  71064 Jun  7 04:05 php.ini-development
-rw-rw-r--.  1 root root  71096 Jun  7 04:05 php.ini-production
-rw-rw-r--.  1 root root   7010 Jun  7 04:05 README.EXT_SKEL
-rw-rw-r--.  1 root root   5026 Jun  7 04:05 README.GIT-RULES
-rw-rw-r--.  1 root root   5417 Jun  7 04:05 README.input_filter
-rw-rw-r--.  1 root root   3426 Jun  7 04:05 README.MAILINGLIST_RULES
-rw-rw-r--.  1 root root   1608 Jun  7 04:05 README.md
-rw-rw-r--.  1 root root   6040 Jun  7 04:05 README.namespaces
-rw-rw-r--.  1 root root   5237 Jun  7 04:05 README.NEW-OUTPUT-API
-rw-rw-r--.  1 root root   7528 Jun  7 04:05 README.PARAMETER_PARSING_API
-rw-rw-r--.  1 root root  19766 Jun  7 04:05 README.REDIST.BINS
-rw-rw-r--.  1 root root  12934 Jun  7 04:05 README.RELEASE_PROCESS
-rw-rw-r--.  1 root root   5086 Jun  7 04:05 README.SELF-CONTAINED-EXTENSIONS
-rw-rw-r--.  1 root root  15355 Jun  7 04:05 README.STREAMS
-rw-rw-r--.  1 root root   8126 Jun  7 04:05 README.SUBMITTING_PATCH
-rw-rw-r--.  1 root root   6695 Jun  7 04:05 README.TESTING
-rw-rw-r--.  1 root root   4958 Jun  7 04:05 README.TESTING2
-rw-rw-r--.  1 root root   4261 Jun  7 04:05 README.UNIX-BUILD-SYSTEM
-rw-rw-r--.  1 root root    109 Jun  7 04:05 README.WIN32-BUILD-SYSTEM
-rwxrwxr-x.  1 root root  82234 Jun  7 04:05 run-tests.php
drwxrwxr-x. 10 root root   4096 Jun  7 04:05 sapi
drwxrwxr-x.  4 root root     99 Jun  7 04:05 scripts
-rwxrwxr-x.  1 root root   2104 Jun  7 04:05 server-tests-config.php
-rwxrwxr-x.  1 root root  52726 Jun  7 04:05 server-tests.php
-rwxrwxr-x.  1 root root    108 Jun  7 04:05 snapshot
-rw-rw-r--.  1 root root     10 Jun  7 04:05 stamp-h.in
-rw-rw-r--.  1 root root      1 Jun  7 04:05 stub.c
drwxrwxr-x. 10 root root   4096 Jun  7 04:05 tests
drwxrwxr-x.  3 root root     33 Jun  7 04:05 travis
drwxrwxr-x.  3 root root   4096 Jun  7 04:05 TSRM
-rw-rw-r--.  1 root root  23356 Jun  7 04:05 UPGRADING
-rw-rw-r--.  1 root root   3418 Jun  7 04:05 UPGRADING.INTERNALS
-rwxrwxr-x.  1 root root    159 Jun  7 04:05 vcsclean
drwxrwxr-x.  3 root root   4096 Jun  7 04:05 win32
drwxrwxr-x.  4 root root   4096 Jun  7 04:05 Zend

可以发现目录中并没有 configure 文件, 但是有一个名为 buildconf 的文件,

嗯, 是它就是它, 我们要通过运行这个文件生成 configure 文件

生成 php 源码目录下的配置文件 configure

> 运行 `buildconf` 文件, 生成 `configure` 文件

[root@localhost php-src-php-7.1.6]# ./buildconf 
You should not run buildconf in a release package.
use buildconf --force to override this check.

> 很不幸运, 并没有成功, 并且提示:

You should not run buildconf in a release package.  # 你不应该在发布包运行 buildconf 文件。

use buildconf --force to override this check.   # 使用 buildconf 文件重写此检查。


> 那就根据它的要求加入 `--force` 选项重写检查

[root@localhost php-src-php-7.1.6]# ./buildconf --force
Forcing buildconf
Removing configure caches
buildconf: checking installation...
buildconf: autoconf version 2.69 (ok)
rebuilding aclocal.m4
rebuilding configure
rebuilding main/php_config.h.in

> 这次可算是 buildconf 文件重写检查成功了, 看一下源码目录有没有 `configure` 文件

生成源码目录配置文件 configure 之后的目录

[root@localhost php-src-php-7.1.6]# ll
total 4260
-rw-rw-r--.  1 root root   82506 Jun  7 04:05 acinclude.m4
-rw-r--r--.  1 root root  312883 Jun 18 04:43 aclocal.m4
drwxrwxr-x.  2 root root      78 Jun  7 04:05 appveyor
drwxr-xr-x.  2 root root      51 Jun 18 04:43 autom4te.cache
drwxrwxr-x.  2 root root    4096 Jun  7 04:05 build
-rwxrwxr-x.  1 root root     772 Jun  7 04:05 buildconf
-rw-rw-r--.  1 root root     334 Jun  7 04:05 buildconf.bat
-rw-r--r--.  1 root root       0 Jun 18 04:43 buildmk.stamp
-rw-rw-r--.  1 root root   11982 Jun  7 04:05 CODING_STANDARDS
-rw-rw-r--.  1 root root   42938 Jun  7 04:05 config.guess
-rw-rw-r--.  1 root root   35987 Jun  7 04:05 config.sub
-rwxr-xr-x.  1 root root 2758685 Jun 18 04:43 configure
-rw-rw-r--.  1 root root   46774 Jun  7 04:05 configure.in
-rw-rw-r--.  1 root root    3163 Jun  7 04:05 CONTRIBUTING.md
-rw-rw-r--.  1 root root      91 Jun  7 04:05 CREDITS
drwxrwxr-x. 75 root root    4096 Jun  7 04:05 ext
-rw-rw-r--.  1 root root   20917 Jun  7 04:05 EXTENSIONS
-rw-rw-r--.  1 root root     137 Jun  7 04:05 footer
-rw-r--r--.  1 root root    1776 Jun 18 04:43 generated_lists
-rwxrwxr-x.  1 root root     581 Jun  7 04:05 genfiles
-rw-rw-r--.  1 root root    1143 Jun  7 04:05 header
-rw-rw-r--.  1 root root   87836 Jun  7 04:05 INSTALL
-rw-r--r--.  1 root root       0 Jun 18 04:43 install-sh
-rw-rw-r--.  1 root root    3218 Jun  7 04:05 LICENSE
-rw-rw-r--.  1 root root  199728 Jun  7 04:05 ltmain.sh
drwxrwxr-x.  3 root root    4096 Jun 18 04:43 main
-rwxrwxr-x.  1 root root    4129 Jun  7 04:05 makedist
-rw-rw-r--.  1 root root    1088 Jun  7 04:05 Makefile.frag
-rw-rw-r--.  1 root root    2485 Jun  7 04:05 Makefile.gcov
-rw-rw-r--.  1 root root    7236 Jun  7 04:05 Makefile.global
-rw-rw-r--.  1 root root    5317 Jun  7 04:05 makerpm
-rw-r--r--.  1 root root       0 Jun 18 04:43 missing
-rw-r--r--.  1 root root       0 Jun 18 04:43 mkinstalldirs
drwxrwxr-x.  2 root root      40 Jun  7 04:05 netware
-rw-rw-r--.  1 root root  135093 Jun  7 04:05 NEWS
drwxrwxr-x.  2 root root      65 Jun  7 04:05 pear
-rw-rw-r--.  1 root root    1489 Jun  7 04:05 php7.spec.in
-rw-rw-r--.  1 root root    2523 Jun  7 04:05 php.gif
-rw-rw-r--.  1 root root   71064 Jun  7 04:05 php.ini-development
-rw-rw-r--.  1 root root   71096 Jun  7 04:05 php.ini-production
-rw-rw-r--.  1 root root    7010 Jun  7 04:05 README.EXT_SKEL
-rw-rw-r--.  1 root root    5026 Jun  7 04:05 README.GIT-RULES
-rw-rw-r--.  1 root root    5417 Jun  7 04:05 README.input_filter
-rw-rw-r--.  1 root root    3426 Jun  7 04:05 README.MAILINGLIST_RULES
-rw-rw-r--.  1 root root    1608 Jun  7 04:05 README.md
-rw-rw-r--.  1 root root    6040 Jun  7 04:05 README.namespaces
-rw-rw-r--.  1 root root    5237 Jun  7 04:05 README.NEW-OUTPUT-API
-rw-rw-r--.  1 root root    7528 Jun  7 04:05 README.PARAMETER_PARSING_API
-rw-rw-r--.  1 root root   19766 Jun  7 04:05 README.REDIST.BINS
-rw-rw-r--.  1 root root   12934 Jun  7 04:05 README.RELEASE_PROCESS
-rw-rw-r--.  1 root root    5086 Jun  7 04:05 README.SELF-CONTAINED-EXTENSIONS
-rw-rw-r--.  1 root root   15355 Jun  7 04:05 README.STREAMS
-rw-rw-r--.  1 root root    8126 Jun  7 04:05 README.SUBMITTING_PATCH
-rw-rw-r--.  1 root root    6695 Jun  7 04:05 README.TESTING
-rw-rw-r--.  1 root root    4958 Jun  7 04:05 README.TESTING2
-rw-rw-r--.  1 root root    4261 Jun  7 04:05 README.UNIX-BUILD-SYSTEM
-rw-rw-r--.  1 root root     109 Jun  7 04:05 README.WIN32-BUILD-SYSTEM
-rwxrwxr-x.  1 root root   82234 Jun  7 04:05 run-tests.php
drwxrwxr-x. 10 root root    4096 Jun  7 04:05 sapi
drwxrwxr-x.  4 root root      99 Jun  7 04:05 scripts
-rwxrwxr-x.  1 root root    2104 Jun  7 04:05 server-tests-config.php
-rwxrwxr-x.  1 root root   52726 Jun  7 04:05 server-tests.php
-rwxrwxr-x.  1 root root     108 Jun  7 04:05 snapshot
-rw-rw-r--.  1 root root      10 Jun  7 04:05 stamp-h.in
-rw-rw-r--.  1 root root       1 Jun  7 04:05 stub.c
drwxrwxr-x. 10 root root    4096 Jun  7 04:05 tests
drwxrwxr-x.  3 root root      33 Jun  7 04:05 travis
drwxrwxr-x.  3 root root    4096 Jun  7 04:05 TSRM
-rw-rw-r--.  1 root root   23356 Jun  7 04:05 UPGRADING
-rw-rw-r--.  1 root root    3418 Jun  7 04:05 UPGRADING.INTERNALS
-rwxrwxr-x.  1 root root     159 Jun  7 04:05 vcsclean
drwxrwxr-x.  3 root root    4096 Jun  7 04:05 win32
drwxrwxr-x.  4 root root    4096 Jun  7 04:05 Zend

现在源码目录也就存在 configure 文件了

正式编译 php7.1

如果使用此编译选项列表, 请将 \ 反斜线后面的注释去除, 并且后面不能有空白字符

[root@localhost php-src-php-7.1.6]# ./configure \
--prefix=/usr/local/php \                               [php 安装的根目录]
--exec-prefix=/usr/local/php \                          [php 执行文件所在目录]
--bindir=/usr/local/php/bin \                           [php/bin 目录]
--sbindir=/usr/local/php/sbin \                         [php/sbin 目录]
--includedir=/usr/local/php/include \                   [php 包含文件所在目录]
--libdir=/usr/local/php/lib/php \                       [php/lib目录]
--mandir=/usr/local/php/php/man \                       [php/man目录]
--with-config-file-path=/usr/local/php/etc \            [php 的配置目录]
--with-mysql-sock=/var/run/mysql/mysql.sock \           [php 的Unix socket 通信文件]
--with-mcrypt \                                         [是 php 里面重要的加密支持扩展库,linux 环境下该库在默认情况下不开启]
--with-mhash \                                          [Mhash是基于离散数学原理的不可逆向的 php 加密方式扩展库,其在默认情况下不开启]
--with-openssl \                                        [OpenSSL 是一个安全套接字层密码库]
--with-mysqli=shared,mysqlnd \                          [php 依赖 mysql 库]
--with-pdo-mysql=shared,mysqlnd \                       [php 依赖 mysql 库]
--with-gd \                                             [gd 库]                                               
--with-iconv \                                          [关闭 iconv 函数,种字符集间的转换]                        
--with-zlib \                                           [zlib 是提供数据压缩用的函式库]
--enable-zip \                                          [打开对 zip 的支持]
--enable-inline-optimization \                          [优化线程]
--disable-debug \                                       [关闭调试模式]
--disable-rpath \                                       [关闭额外的运行库文件]
--enable-shared \                                       [启用动态库]
--enable-xml \                                          [开启 xml 扩展]
--enable-bcmath \                                       [打开图片大小调整, 用到 zabbix 监控的时候用到了这个模块]
--enable-shmop \                                        [共享内存]
--enable-sysvsem \                                      [内存共享方案]
--enable-mbregex \                                      [开启多字节正则表达式的字符编码。]
--enable-mbstring \                                     [开启多字节字符串函数]
--enable-ftp \                                          [开启 ftp]
--enable-gd-native-ttf \                                [开启 gd 库原有字体]
--enable-pcntl \                                        [PHP的进程控制支持实现了 Unix 方式的多进程创建]     
--enable-sockets \                                      [开启套节字]
--with-xmlrpc \                                         [打开 xml-rpc 的 c 语言]
--enable-soap \                                         [开启简单对象访问协议简单对象访问协议]
--without-pear \                                        [开启 php 扩展与应用库]
--with-gettext \                                        [开户 php 在当前域中查找消息]
--enable-session \                                      [允许 php 会话 session]
--with-curl \                                           [允许 curl 扩展]
--with-jpeg-dir \                                       [指定 jpeg 安装目录 yum 安装过后不用再次指定会自动找到]
--with-freetype-dir \                                   [指定 freetype 安装目录 yum 安装过后不用再次指定会自动找到]
--enable-opcache \                                      [开启使用 opcache 缓存]
--enable-fpm \                                          [开启 fpm]
--with-fpm-user=nginx \                                 [php-fpm 的用户]
--with-fpm-group=nginx \                                [php-fpm 的用户组]
--without-gdbm \                                        [数据库函数使用可扩展散列和类似于标准UNIX dbm 的工作]
--enable-fast-install \                                 [为快速安装优化]
--disable-fileinfo

可能会出现要求开启 --enable-opcache=on 的错误提示

> 错误提示

configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no

> 解决办法

编辑 /etc/ld.so.conf

加入 /usr/local/lib

执行 ldconfig, 使其重新加载一次

运行 make test

这一步不是必须的, 如果要运行可能会让你等很长一段时间

  • 运行
[root@localhost php-src-php-7.1.6]# make test
  • 结果
You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it.  You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]: Y

Please enter your email address.
(Your address will be mangled so that it will not go out on any
mailinglist in plain text): warnerwu@126.com

Posting to http://qa.php.net/buildtest-process.php

Thank you for helping to make PHP better.

出现这个结果 make test 也就完成了!

编译安装后的 php 目录

[root@localhost php-src-php-7.1.6]# ll /usr/local/php
total 0
drwxr-xr-x. 2 root root 100 Jun 18 05:57 bin
drwxr-xr-x. 3 root root  49 Jun 18 05:57 etc
drwxr-xr-x. 3 root root  16 Jun 18 05:57 include
drwxr-xr-x. 3 root root  16 Jun 18 05:57 lib
drwxr-xr-x. 4 root root  26 Jun 18 05:57 php
drwxr-xr-x. 2 root root  20 Jun 18 05:57 sbin
drwxr-xr-x. 4 root root  26 Jun 18 05:57 var

配置 php

修改 php 配置文件之添加 php 配置文件

> 将 php 源码编译目录下的 `php.ini-production` 拷贝到 php 安装目录下的 `etc` 目录下

[root@localhost php-src-php-7.1.6]# cp php.ini-production /usr/local/php/etc/php.ini

修改 php 配置文件之修改 php 扩展目录所在位置

> 修改 `php.ini` 配置文件, 设置 php 扩展安装目录所在位置

[root@localhost php-src-php-7.1.6]# vim /usr/local/php/etc/php.ini

> 找到 `extension_dir` 修改扩展目录位置如下:

extension_dir = "../lib/php/extensions/no-debug-non-zts-20160303/"

> 上面写的扩展目录位置是使用的 ` 相对路径 ` 当然你也可以使用 ` 绝对路径 ` 如:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/"

修改 php 配置文件之修改服务器所在时区

> 修改 `php.ini` 配置文件, 设置 php 所安装服务器时区

[root@localhost php-src-php-7.1.6]#  vim /usr/local/php/etc/php.ini

> 找到 `timezone` 修改时区如下:

date.timezone = PRC

> PRC 就是英文 People's Republic of China, 这个你应该知道的吧! 中华人民共和国

修改 php 配置文件之开启 OPcache

> 修改 `php.ini` 配置文件, 开启 OPcache

[root@localhost php-src-php-7.1.6]#  vim /usr/local/php/etc/php.ini

> 跳转到文件的最后一行(shift+G), 输入以下内容:
zend_extension=opcache.so;

> 保存并退出
:wq!

修改 php 配置文件之开启 mysqli 扩展

> 修改 `php.ini` 配置文件, 开启 `mysqli` 扩展

[root@localhost php-src-php-7.1.6]#  vim /usr/local/php/etc/php.ini

> 找到 `extension` 位置, 输入以下内容

; For UNIX OS open mysqli extension
extension=mysqli.so
extension=pdo_mysql.so

> 保存并退出

:wq!

添加 php-fpm 管理相关的配置文件到系统配置目录 /etc/init.d

> 将 php 源码编译目录下的 `sapi/fpm/init.d.php-fpm` 文件拷贝到系统配置 `/etc/init.d` 目录下并重命名为 `php-fpm`

[root@localhost php-src-php-7.1.6]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

添加 php-fpm 配置文件

> 将 php 安装目录下的 `/usr/local/php/etc/php-fpm.conf.default` 文件拷贝同目录下并重命名为 `php-fpm.conf`

[root@localhost php-src-php-7.1.6]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  • 1
  • 2
  • 3

添加 www.conf 配置文件

> 将 php 安装目录下的 `/usr/local/php/etc/php-fpm.d/www.conf.default` 文件拷贝同目录下并重命名为 `www.conf`

[root@localhost php-src-php-7.1.6]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

添加 php 安装目录到系统环境变量

> 创建并打开文件 php.sh

[root@localhost php-src-php-7.1.6]# vim /etc/profile.d/php.sh

> 添加内容如下:

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

> 保存并退出

:wq!

> 使用 source 立即生效刚刚添加的 php 环境变量

[root@localhost php-src-php-7.1.6]# source /etc/profile.d/php.sh

设置 PHP 日志目录和 php-fpm 运行进程的 ID 文件目录

php-fpm 运行进程的 ID 文件也就是 php-fpm.sock

其中设置 php-fpm 进程目录的用户和用户组为 nginx,

有于我在安装 nginx 时已经创建, 在这里我就不创建了, 只是列举

> 添加系统用户组 nginx (如果你没有创建过系统用户组 nginx 请创建)

[root@localhost php-src-php-7.1.6]# groupadd -r nginx

> 创建新的系统用户 nginx, 并添加到系统用户组 nginx, 设置不允许此用户名登录 shell (如果你没有创建过系统用户 nginx 请创建)

[root@localhost php-src-php-7.1.6]# useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx -M nginx

> 说明一下上面创建系统用户 nginx 所使用的选项什么意思

-r: 添加系统用户(这里指将要被创建的系统用户 nginx)

-g: 指定要创建的用户所属组(这里指添加到新系统用户 nginx 到 nginx 系统用户组)

-s: 新帐户的登录 shell(`/sbin/nologin` 这里设置为将要被创建系统用户 nginx 不能用来登录系统 )

-d: 新帐户的主目录(这里指定将要被创建的系统用户 nginx 的家目录为 `/usr/local/nginx` )

-M: 不要创建用户的主目录(也就是说将要被创建的系统用户 nginx 不会在 `/home` 目录下创建 `nginx` 家目录 )


> 创建 `php-fpm` 日志目录

[root@localhost php-src-php-7.1.6]# mkdir -p /var/log/php-fpm/

> 创建 `php-fpm` 进程的 ID(php-fpm.sock)文件运行目录

[root@localhost php-src-php-7.1.6]#  mkdir -p /var/run/php-fpm

> 修改 `php-fpm` 进程的 ID(php-fpm.sock)文件运行目录的所属用户和组

[root@localhost php-src-php-7.1.6]# chown -R nginx:nginx /var/run/php-fpm/

修改 session 的目录配置

> 创建 `session` 存放目录

[root@localhost php-src-php-7.1.6]#  mkdir -p /var/lib/php/session

> 修改 `session` 存放目录的所属用户和组

[root@localhost php-src-php-7.1.6]# chown -R nginx:nginx /var/lib/php

> 再次修改 `php.ini` 文件

[root@localhost php-src-php-7.1.6]# vim /usr/local/php/etc/php.ini

> 找到 `session.save_path` 修改为如下内容:

session.save_path = "/var/lib/php/session"

> 保存并退出

:wq!

设置 php 开机启动

> 修改系统配置目录下的 `php-fpm` 文件可执行权限

[root@localhost php-src-php-7.1.6]# chmod +x /etc/init.d/php-fpm

> 将系统配置目录下的 `php-fpm` 添加到 ` 系统服务 `

[root@localhost php-src-php-7.1.6]# chkconfig --add php-fpm

> 设置 `php-fpm` ` 系统服务 ` 为开机启动

[root@localhost php-src-php-7.1.6]# chkconfig php-fpm on

检测 php-fpm 系统服务是否启动成功

> 用 chkconfig 命令检测一下服务是否运行成功

[root@localhost php-src-php-7.1.6]# chkconfig --list | grep php-fpm
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

> 可见服务已经在 第2 到 第5 运行等级打开



> 同样你也可以禁用 `php-fpm` 开机启动

[root@localhost php-src-php-7.1.6]# chkconfig php-fpm off

> 用 chkconfig 命令检测一下服务是否运行成功

[root@localhost php-src-php-7.1.6]# chkconfig --list | grep php-fpm

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

php-fpm         0:off   1:off   2:off   3:off   4:off   5:off   6:off

> 可见服务已经在 第2 到 第5 运行等级已经关闭

测试 PHP 的配置文件是否无误

[root@localhost php-src-php-7.1.6]# php-fpm -t
[18-Jun-2017 13:02:33] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
  • 1
  • 2

出现上面的提示也就是测试配置文件通过没有问题, 可以正式使用 php 服务了

启动 php 系统服务

> 使用 `systemctl start` + ` 服务名 ` 启动系统服务

[root@localhost php-src-php-7.1.6]# systemctl start php-fpm.service

查看 php 系统服务状态

> 使用 `systemctl status` + ` 服务名 ` 查看系统服务状态

[root@localhost php-src-php-7.1.6]# systemctl status php-fpm.service
● php-fpm.service - LSB: starts php-fpm
   Loaded: loaded (/etc/rc.d/init.d/php-fpm; bad; vendor preset: disabled)
   Active: active (running) since Sun 2017-06-18 13:05:26 EDT; 2min 46s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 105210 ExecStart=/etc/rc.d/init.d/php-fpm start (code=exited, status=0/SUCCESS)
   Memory: 11.0M
   CGroup: /system.slice/php-fpm.service
           ├─105212 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
           ├─105213 php-fpm: pool www
           ├─105214 php-fpm: pool www
           └─105215 php-fpm: pool www

Jun 18 13:05:19 localhost.localdomain systemd[1]: Starting LSB: starts php-fpm...
Jun 18 13:05:24 localhost.localdomain php-fpm[105210]: Starting php-fpm  done
Jun 18 13:05:26 localhost.localdomain systemd[1]: Started LSB: starts php-fpm.

你可以看到一个绿色的 active (running) 说明系统服务 php-fpm 已经在工作服务了,

有没有觉得自从 RedHat 系统到 7 以后, 越来越舒心了(哈哈)

CLI 查看 php 版本信息

[root@localhost ~]# php -v
PHP 7.1.6 (cli) (built: Jun 25 2017 13:42:29) (NTS)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.6, Copyright (c) 1999-2017, by Zend Technologies

修改 nginx 配置文件使之使用 nginx 反向 php 解释器

修改 nginx 配置文件使之使用 nginx 反向 php 解释器, 也就是 php-fpm 它其实就是 cgi

> 说明一下我在安装的 nginx 是将配置文件放在 `/etc/nginx/` 目录下

> 修改配置文件如下:

server {listen       80;
    server_name  www.nginx.dev;


    #charset koi8-r;

    access_log  logs/nginx.dev.access.log  main;

    location / {root   /data/www/html;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {root   /data/www/html;
    }

    location ~ ^/(images|javascript|js|css|flash|media|static)/ {expires 30d;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {#    root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {deny  all;
    }
}

> 创建文件

[root@localhost php-src-php-7.1.6]# touch /data/www/html/index.php

> 编辑文件

[root@localhost php-src-php-7.1.6]# vim  /data/www/html/index.php

> 输入内容

<?php

phpinfo();

> 保存退出

:wq!

> 重新加载 nginx 配置

[root@localhost php-src-php-7.1.6]# systemctl reload nginx.service

浏览器访问

CentOS7.3 编译安装 PHP7.1

至此 PHP7.1 也就编译安装成功了, 也和 nginx 建立了关系

至于 nginx 和 php-fpm 和是怎么工作的可以参考 Nginx+Php-fpm 运行原理详解 http://www.linuxidc.com/Linux/2017-10/148105.htm 

Red Hat Enterprise Linux 7.3 下 PHP 安装  http://www.linuxidc.com/Linux/2017-05/143942.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

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

CentOS 7 下 PHP 5.6.19 编译安装详解  http://www.linuxidc.com/Linux/2017-03/142002.htm

PHP 源码安装、简单配置、测试及连接数据库 http://www.linuxidc.com/Linux/2016-10/135977.htm

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

CentOS 7.3 环境下 PHP7.0 安装  http://www.linuxidc.com/Linux/2017-08/146433.htm

CentOS 7 编译安装 PHP 5.6  http://www.linuxidc.com/Linux/2017-08/146095.htm

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/148104.htm

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