# CentOS 安装 PHP7.4.X

#### &#x20;博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击联系[博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## 概述

&#x20;       昨天一个朋友问到我，我们有没有安装PHP环境，我说公司环境就是LAMP，开发都是PHP，看似简单的环境部署，让我一时口诉也无法理清，还是写一篇文档记录一下，方便日后发给有需要的人。

本篇文档中分别演示生产环境中源码编译PHP环境和YUM源安装PHP环境，并优化php-fpm配置文件。

## 编译前准备

更新系统软件包，安装编译相关依赖

{% hint style="danger" %}
**警告：下面这些依赖必须全部装上，不然编译会报错。**
{% endhint %}

```bash
yum --exclude=kernel* update -y
yum groupinstall -y 'Development Tools'
yum install -y epel-release
yum install -y wget openssl-devel bzip2-devel libffi-devel  sqlite-devel vim gpm-libs oniguruma oniguruma-devel libsodium libsodium-devel xz-devel libxml2-devel libcurl-devel
yum install -y bash-completion bash-completion-extras libicu-devel libjpeg libjpeg-devel  nss_ldap cmake  boost-devel libevent libevent-devel gd gd-devel openjpeg-devel 
yum install -y libgcrypt-devel libpng-devel libgpg-error-devel libxslt-devel cmake libmcrypt-devel libmcrypt  recode-devel recode  
```

安装高版本libzip

```bash
yum remove  -y libzip-devel
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64//libzip5-1.5.2-1.el7.remi.x86_64.rpm
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64//libzip5-devel-1.5.2-1.el7.remi.x86_64.rpm  

```

## 一、编译安装源码

### 访问PHP官网下载PHP源码

访问PHP官网下载最新稳定版源码安装包：

{% embed url="<https://www.php.net/downloads.php>" %}

```bash
wget https://www.php.net/distributions/php-7.4.1.tar.gz
tar -zvxf php-7.4.1.tar.gz && cd php-7.4.1/
mkdir -p /usr/local/php-7.4.1 && useradd -s /sbin/nologin www

```

{% hint style="warning" %}
在Linux下安装PHP，源代码方式安装，总需要配置很多参数。这里列出常用配置参数，并详细用中文解释说明了。

给大家一些参考:

`--prefix=/usr/local/PHP                              #php 安装目录`\
`--with-apxs2=/usr/local/apache/bin/apxs              #表示使用apache2.0以上版本，apxs表示使用的apache1.0版本                                                                                                                              --with-fpm-user=www                                  #指定所属用户                                                                                                   --with-fpm-grop=www                                  #指定所属组`\
`--with-config-file-path=/usr/local/PHP/etc           #指定php.ini位置`\
`--with-MySQL=/usr/local/mysql                        #mysql安装目录，对mysql的支持`\
`--with-MySQLi=/usr/local/mysql/bin/mysql_config      #mysqli文件目录,优化支持`\
`--enable-safe-mode                                   #打开安全模式`\
`--enable-ftp                                         #打开ftp的支持`\
`--enable-zip                                         #打开对zip的支持`\
`--with-bz2                                           #打开对bz2文件的支持`                      \
`--with-jpeg-dir                                      #打开对jpeg图片的支持`\
`--with-png-dir                                       #打开对png图片的支持`\
`--with-freetype-dir                                  #打开对freetype字体库的支持`\
`--without-iconv                                 #关闭iconv函数，种字符集间的转换`\
`--with-libXML-dir                               #打开libxml2库的支持`\
`--with-XMLrpc                                   #打开xml-rpc的c语言`\
`--with-zlib-dir                                 #打开zlib库的支持`\
`--with-gd                                       #打开gd库的支持，7.4 后不再支持这种写法，所以需要改为 --enable-gd`\
`--enable-gd-native-ttf                          #支持TrueType字符串函数库`\
`--with-curl                                     #打开curl浏览工具的支持`\
`--with-curlwrappers                             #运用curl工具打开url流`\
`--with-ttf                                      #打开freetype1.*的支持，可以不加了`\
`--with-xsl                                      #打开XSLT 文件支持，扩展了libXML2库，需要libxslt软件`\
`--with-gettext                                  #打开gnu 的gettext 支持，编码库用到`\
`--with-pear                                     #打开pear命令的支持，PHP扩展用的`\
`--enable-calendar                               #打开日历扩展功能`\
`--enable-mbstring                               #多字节，字符串的支持`\
`--enable-bcmath                           #打开图片大小调整,用到zabbix监控的时候用到了这个模块`\
`--enable-sockets                          #打开 sockets 支持`\
`--enable-exif                             #图片的元数据支持`\
`--enable-magic-quotes                     #魔术引用的支持`\
`--disable-rpath                           #关闭额外的运行库文件`\
`--disable-debug                           #关闭调试模式`\
`--with-mime-magic=/usr/share/file/magic.mime       #魔术头文件位置`\
\
`CGI方式安装才用的参数`\
`--enable-fpm                                       #打上PHP-fpm 补丁后才有这个参数，CGI方式安装的启动程序`\
`--enable-fastCGI                                   #支持fastcgi方式启动PHP`\
`--enable-force-CGI-redirect                        #同上 ,帮助里没有解释`\
`--with-ncurses                                     #支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库`\
`--enable-pcntl                                     #freeTDS需要用到的，可能是链接mssql 才用到`\
\
`mhash和mcrypt算法的扩展`\
`--with-mcrypt                            #算法`\
`--with-mhash                             #算法`\
\
`--with-gmp`\
`--enable-inline-optimization`\
`--with-openssl                               #openssl的支持，加密传输时用到的`\
`--enable-dbase`\
`--with-pcre-dir=/usr/local/bin/pcre-config   #perl的正则库案安装位置`\
`--disable-dmalloc`\
`--with-gdbm                                  #dba的gdbm支持`\
`--enable-sigchild`\
`--enable-sysvsem`\
`--enable-sysvshm`\
`--enable-zend-multibyte                      #支持zend的多字节`\
`--enable-mbregex`\
`--enable-wddx`\
`--enable-shmop`\
`--enable-soap`
{% endhint %}

{% hint style="danger" %}
编译PHP的时候慎用 –with-curlwrappers参数
{% endhint %}

### 1、编译

&#x20;在这里可以用各种各样的参数来自定义 PHP，例如启动哪些扩展功能包的支持等。用`./configure --help`命令可以列出当前可用的所有参数。

```bash
./configure \
--prefix=/usr/local/php-7.4.1 \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-config-file-path=/usr/local/php-7.4.1/etc \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--enable-pdo \
--with-zlib \
--enable-xml \
--enable-session \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-zip \
--with-ffi \
--with-zlib-dir \
--with-openssl \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--enable-fileinfo \
--enable-opcache \
--enable-maintainer-zts \
--with-xsl \
--with-sodium \
--enable-tokenizer
```

### 2、编译依赖及模块问题解决

&#x20;         PHP7.4版本，需要对扩展与依赖作出改变，如如常见的GD库扩展，--with-gd就要改成--enable-gd。这些都好说，不好说的是依赖的问题。

&#x20;        Linux安装软件，导致无法安装的原因绝大多数都是依赖包的问题，我安装的是PHP7.4.1，以下就是所遇到的问题及对应的解决方法：

问题一：`error: Package requirements (sqlite3 > 3.7.4) were not met`

解决方法：

```bash
yum install -y sqlite-devel
```

&#x20; 问题二：`error: Package requirements (oniguruma) were not met`

解决方法：

```bash
yum install -y oniguruma  oniguruma-devel
```

{% hint style="danger" %}
`如果这两个包安装不了，安装如下依赖库`
{% endhint %}

```bash
yum -y install epel-release
```

{% hint style="danger" %}
**注意：**

编译安装（或升级PHP7.4）所遇到的主要就是这两个问题，对于其他的报错，都是依赖的问题，根据对应的提示安装相应的依赖就行了。

以下是预编译完成后PHP所无法识别的扩展，也就是，这些扩展在预编译的时候就不要选上了：

**【警示】**&#x4EE5;下是不可用选项

```
configure: WARNING: unrecognized options: 
--with-libxml-dir, 
--with-pcre-regex, 
--with-pcre-dir, 
--with-gd, 
--with-jpeg-dir, 
--with-png-dir, 
--with-freetype-dir, 
--enable-mbregex-backtrack, 
--with-onig, 
--enable-wddx, 
--with-libxml-dir, 
--enable-zip
```

{% endhint %}

出现如下截图表示编译成功

![](https://139036132-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lx53lMutrsyPUks5pJf%2F-Ly9Egxh2eK-E5V-PCrR%2F-Ly9Nj7eQkuDC6pKG6FB%2Fimage.png?alt=media\&token=49722654-ef6e-4f42-9d76-3ac9653a1380)

### 3、安装

编译成功然后再安装

```bash
make && make install

```

## 二、配置PHP环境变量

```bash
vim /etc/profile             #打开profile文件增加如下，然后使文件生效
PATH=/usr/local/php-7.4.1/bin:/usr/local/php-7.4.1/sbin:$PATH
```

### 1、使环境变量生效

```bash
source /etc/profile

```

### 2、检查是否安装成功

输入`php -v`检查是否安装成功，环境变量并生效。

![](https://139036132-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lx53lMutrsyPUks5pJf%2F-Ly9ZderSgyMrLx99WHC%2F-Ly9Zyr3oHIslIvgyQq7%2Fimage.png?alt=media\&token=ab02e9c2-d4cb-452c-89b4-5f8234067a7d)

## 三、配置PHP-FPM

```bash
[root@php php-7.4.1]# pwd
/root/php-7.4.1
```

把所需的几个配置文件放到需要的位置

```bash
cp php.ini-production /usr/local/php-7.4.1/etc/php.ini
cp /usr/local/php-7.4.1/etc/php-fpm.conf.default /usr/local/php-7.4.1/etc/php-fpm.conf
cp /usr/local/php-7.4.1/etc/php-fpm.d/www.conf.default /usr/local/php-7.4.1/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

```

### 1、启动PHP-FPM

```bash
/etc/init.d/php-fpm start        #启动
/etc/init.d/php-fpm restart      #重新启动
/etc/init.d/php-fpm reload       #重新加载
/etc/init.d/php-fpm stop         #停止
/etc/init.d/php-fpm status       #查看状态
```

### 2、检测端口是否监听，PHP默认端口是9000

```bash
ss -ntulp | grep php-fpm

```

![](https://139036132-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lx53lMutrsyPUks5pJf%2F-Ly9yO3KTopOxqiTr8To%2F-Ly9z8v4n7F1sklqKz1Y%2Fimage.png?alt=media\&token=f4c3ddf3-7269-4673-9c76-d590753817b6)

## 四、添加PHP官方YUM源

下面演示YUM安装PHP7版本

### 1、安装前准备

更新系统软件仓库不更新内核，安装开发工具包

```bash
yum --exclude=kernel* update -y
yum groupinstall -y 'Development Tools'

```

### 2、添加epel第三方依赖库，安装YUM源

```bash
yum -y install epel-release yum-utils
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

```

###

### 3、安装PHP相关依赖库和模块

```bash
yum install -y --enablerepo=remi --enablerepo=remi-php74 mod_php php-gd* php php-opcache php-mbstring  php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-fpm

```

```bash
yum install -y --enablerepo=remi --enablerepo=remi-php74 php-cli php-redis php-pecl-mcrypt php-process  php-devel php-pear
 
```

```bash
yum install -y vim libevent-devel libmcrypt-devel libxml2 libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libxslt libxslt-devel cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib  openssl openssl-devel pcre pcre-devel zlib zlib-devel wget net-tools
 
```

## 五、检查YUM安装是否成功

安装完，执行如下命令：

```bash
php --version                      #查看版本
php -m                             #查看安装模块，需要用到的模块，没有安装的需要YUM安装
systemctl enable --now php-fpm     #设置开机启动，并启动。
ss -ntulp | grep php-fpm
```

![](https://139036132-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lx53lMutrsyPUks5pJf%2F-LyDN_p2Sm8FFu7sZLhJ%2F-LyDNeJDjfUSdbLCHqis%2Fimage.png?alt=media\&token=ef67ded1-cafa-458d-85ab-c506a0712179)

## 六、优化PHP

### 1、调整PHP-FPM文件

```bash
vim /etc/php-fpm.conf

log_level = error						# 指定要记录的php-fpm日志级别
rlimit_files = 32768        # 为主进程设置打开文件的限制，虚拟机无法设置。
events.mechanism = epoll    # 指定事件驱动模型
```

### 2、调整PHP.ini文件

{% hint style="warning" %}
&#x20;    隐藏后端使用的真正脚本类型,扰乱入侵者的渗透思路,另外,切记不要把敏感数据直接明文存在session中,有泄露风险。
{% endhint %}

```bash
vim /etc/php.ini
error_reporting = E_WARING & ERROR	  #设置php的错误报告级别,只需要报告警告和错误即可
error_log = /var/log/php_errors.log	  #指定php错误日志存放位置
log_errors_max_len = 2048 	          #指定php错误日志的最大长度
expose_php = off		                  #隐藏php的详细版本号
session.name = JSESSIONID	            #表示jsp程序,php的则是PHPSESSID
```

修改完配置文件需要重新启动才可以生效

```bash
systemctl restart php-fpm
```

## 七、其他

### 其他低版本相对优化及安装访问如下文章：

{% embed url="<https://klionsec.github.io/2017/11/23/phpsec/>" %}
