CentOS 7.5 编译安装 PHP 7.2.10

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_32828933/article/details/82751784

本文主要记录如何在CentOS 7.5中编译安装PHP官方最新的7.2.10版本。由于像NginxMariaDBPHP的的源码都是用C/C++写的,所以自己的CentOS 7.5服务器上必须要安装gccg++软件。

搭建LNMP环境一般是先安装Mysql/MariaDB, 再安装Nginx, 其次是安装PHP

安装环境

  • 系统:CentOS 7.5.1804
  • 软件:PHP 7.2.10
  • 依赖软件:re2clibmcryptmhashmcrypt

创建PHP安装目录、建立用户和目录

先先创建一个名为php没有登录权限的用户和一个名为php的用户组,然后安装php所需的依赖库和依赖包,最后通过.configure进行安装的详细配置。

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

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

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

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

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

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

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

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

安装依赖库

> `yum`安装`PHP`必须的依赖库

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

创建家目录存放软件包目录

> 创建家目录存放软件包目录

[root@lightserver ~]$ mkdir soft && cd soft

下载解压源码包

> `php.net`下载`php7`安装包

[root@lightserver soft]$ wget -c 
http://am1.php.net/distributions/php-7.2.10.tar.gz

> 解压

[root@lightserver soft]$ tar -zxvf php-7.2.10.tar.gz

下载并安装php依赖

  • PHP语法分析器re2c
> 下载

[root@lightserver soft]$ wget -c https://github.com/skvadrik/re2c/releases/download/1.0.3/re2c-1.0.3.tar.gz

> 解压

[root@lightserver soft]$ tar -zxvf re2c-1.0.3.tar.gz

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

$ cd re2c-1.0.3/

$ ./configure

$ make && make install

$ cd ~/soft/
  • 加密支持扩展库libmcrypt

使用php mcrypt 前必须先安装Libmcrypt

> 下载

[root@lightserver soft]$ wget -c https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz

> 解压

$ tar -zxvf libmcrypt-2.5.8.tar.gz

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

# cd libmcrypt-2.5.8/

# ./configure

# make && make install
  • 加密方式扩展库mhash
> 下载

[root@lightserver soft]$ wget -c https://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz

> 解压

$ tar -zxvf mhash-0.9.9.9.tar.gz

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

# cd mhash-0.9.9.9/

# ./configure

# make && make install
  • 加密方式扩展库mcrypt
> 下载

[root@lightserver soft]$ wget -c https://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz

> 解压

$ tar -zxvf mcrypt-2.6.8.tar.gz

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

# cd mcrypt-2.6.8/

# ./configure

# make && make install

# cd ~
  • 编译mcrypt时,报错:configure: error: "You need at least libmhash 0.8.15 to compile this program. http://mhash.sf.net/"
[root@lightserver ~]$ ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

[root@lightserver ~]$ ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

[root@lightserver ~]$ ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

[root@lightserver ~]$ ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

[root@lightserver ~]$ ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

> 打开`/etc/ld.so.conf`,在文件之后,添加一行:

[root@lightserver ~]$ vi /etc/ld.so.conf

/usr/local/lib

> 然后,执行`ldconfig`,重新编译即可。

[root@lightserver ~]$ ldconfig

编译php 7.2.10

  • 查看源码目录文件具体内容
> 进入解压后的源码目录编译并安装

[root@lightserver ~]$ cd soft/php-7.2.10/

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

$ ll

可以发现目录中并没有configure文件, 但是有一个名为buildconf的文件,通过运行这个文件生成configure文件

> 运行 `buildconf` 文件, 生成 `configure` 文件,加入 `--force` 选项

$ ./buildconf --force

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

$ ll

> 现在源码目录也就存在`configure`文件
  • 编译
> 如果使用此编译选项列表, 请将 \ 反斜线后面的注释去除, 并且后面不能有空白字符

$ ./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--bindir=/usr/local/php/bin \
--sbindir=/usr/local/php/sbin \
--includedir=/usr/local/php/include \
--libdir=/usr/local/php/lib/php \
--mandir=/usr/local/php/php/man \
--with-config-file-path=/usr/local/php/etc \
--with-mysql-sock=/var/run/mysql/mysql.sock \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--without-gdbm \
--enable-fast-install \
--disable-fileinfo
  • 注释版
$ ./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
  • 编译安装
> 编译安装

$ make && make install

> 编译安装后的php目录

$ ll /usr/local/php

配置php

  • 修改php配置文件之添加php配置文件
> 将php源码编译目录下的`php.ini-production`拷贝到php安装目录下的`etc`目录下

$ cp php.ini-production /usr/local/php/etc/php.ini
  • 修改php配置文件之修改php扩展目录所在位置
$ vim /usr/local/php/etc/php.ini

> 使用 `绝对路径`

[root@lightserver soft]$ extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/" 
  • 修改php配置文件之修改服务器所在时区
> 找到 `timezone` 修改时区

date.timezone = PRC

> PRC就是英文People's Republic of China,中华人民共和国
  • 修改php配置文件之开启OPcache

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

zend_extension=opcache.so;

  • 修改php配置文件之开启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`

$ cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

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

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

[root@lightserver ~]$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
  • 添加php安装目录到系统环境变量
> 创建并打开文件php.sh

[root@lightserver ~]$ vim /etc/profile.d/php.sh

> 添加内容如下:

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

> 保存并退出

:wq!

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

[root@lightserver ~]$ source /etc/profile.d/php.sh
  • 设置PHP日志目录和php-fpm运行进程的ID文件目录

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

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

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

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

[root@lightserver ~]$ mkdir -p /var/log/php-fpm/

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

[root@lightserver ~]$  mkdir -p /var/run/php-fpm

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

[root@lightserver ~]$ chown -R nginx:nginx /var/run/php-fpm/
  • 修改session的目录配置
> 创建 `session` 存放目录

[root@lightserver ~]$ mkdir -p /var/lib/php/session

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

[root@lightserver ~]$ chown -R nginx:nginx /var/lib/php

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

[root@lightserver ~]$ vim /usr/local/php/etc/php.ini

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

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

> 保存并退出

:wq!
  • 设置php开机启动
> 修改系统配置目录下的 `php-fpm` 文件可执行权限

[root@lightserver ~]$ chmod +x /etc/init.d/php-fpm

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

[root@lightserver ~]$ chkconfig --add php-fpm

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

[root@lightserver ~]$ chkconfig php-fpm on
  • 检测php-fpm系统服务是否启动成功
>`chkconfig`命令检测一下服务是否运行成功

[root@lightserver ~]$ chkconfig --list | grep php-fpm

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

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

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

[root@lightserver ~]$ chkconfig php-fpm off

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

[root@lightserver ~]$ chkconfig --list | grep php-fpm

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

> 可见服务已经在 第2 到 第5 运行等级已经关闭
  • 测试PHP的配置文件是否无误
[root@lightserver ~]$ php-fpm -t
  • 启动php系统服务
> 使用 `systemctl start` + `服务名` 启动系统服务

[root@lightserver ~]$ systemctl start php-fpm.service
  • 查看php系统服务状态
> 使用 `systemctl status` + `服务名` 查看系统服务状态

[root@lightserver ~]$ systemctl status php-fpm.service
  • 查看php版本信息
[root@lightserver ~]$ php -v
  • 修改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@lightserver ~]$ touch /data/www/html/index.php

> 编辑文件

[root@lightserver ~]$ vim /data/www/html/index.php

> 输入内容

<?php

phpinfo();

> 保存退出

:wq!

> 重新加载nginx配置

[root@lightserver ~]$ systemctl reload nginx.service

猜你喜欢

转载自blog.csdn.net/qq_32828933/article/details/82751784