#### 1. 下載php安裝包
新建一個文件夾/home/php文件夾存放php的安裝文件。
php的官網:http://www.php.net/
下載php的最新版本php-7.4.1
`axel -n 10 http://cn2.php.net/distributions/php-7.4.1.tar.gz`

#### 2. 解壓壓縮包
~~~
tar -zxvf php-7.4.1.tar.gz
cd php-7.4.1
~~~
#### 3. 安裝依賴庫
先安裝php需要的依賴庫(直接復制進去一次性安裝好)
~~~
yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl-devel
yum -y install libjpeg-devel
yum -y install libpng-devel
yum -y install freetype-devel
yum -y install bzip2-devel
yum -y install libmcrypt libmcrypt-devel
yum -y install postgresql-devel
yum -y install aspell-devel
yum -y install readline-devel
yum -y install libxslt-devel
yum -y install net-snmp-devel
yum -y install unixODBC-devel
yum -y install libicu-devel
yum -y install libc-client-devel
yum -y install libXpm-devel
yum -y install libvpx-devel
yum -y install enchant-devel
yum -y install openldap
yum -y install openldap-devel
yum -y install db4-devel
yum -y install gmp-devel
yum -y install sqlite-devel
yum -y install mysql-devel
~~~
如果cmake版本低于3.0.2需要升級
```
wget https://cmake.org/files/v3.13/cmake-3.13.0.tar.gz
tar -zxvf cmake-3.13.0.tar.gz
cd cmake-3.13.0
./bootstrap --prefix=/usr/
make && make install
cmake --version
```
libzip安裝
```
wget https://libzip.org/download/libzip-1.5.1.tar.gz
tar -zxvf libzip-1.5.1.tar.gz
cd libzip-1.5.1
mkdir build && cd build && cmake .. && make && make install
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf&&ldconfig -v
```
oniguruma安裝(php 7.4需要安裝)
~~~
git clone https://github.com/kkos/oniguruma
cd oniguruma
autoreconf -vfi
./configure
make
make install
# 再執行
autoreconf -fi
export PKG_CONFIG_PATH= 'oniguruma的路徑'
~~~
libiconv的安裝(php 7.4需要安裝)
~~~
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
tar -zxvf libiconv-1.16.tar.gz
cd libiconv-1.16
./configure --prefix=/usr/local
make
make install
ldconfig
編譯php7.4的時候加上-liconv
make ZEND_EXTRA_LIBS='-liconv'
~~~
在更新php版本后,所有的php擴展都需要使用make clean 清理編譯緩存,重新編譯安裝。否則會由于版本不一致報
Unable to initialize module 無法初始化對應模塊的錯誤
#### 4. 添加用戶和組
`groupadd -r www && adduser -r -g www -s /bin/false -d /alidata/www -M www`
查看用戶
`cat /etc/passwd`
查看組
`cat /etc/group`
#### 5. 對php7進行配置
下面代碼按需求修改后全部復制進去一次性執行(php7.3去掉 --with-mcrypt, --enable-gd-native-ttf, --with-libmbfl
)
~~~
./configure \
--prefix=/alidata/server/php \
--with-config-file-path=/alidata/server/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-xmlrpc \
--with-openssl \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
--enable-pcntl \
--enable-posix
~~~

#### 6. 編譯安裝php7
`make && make install`
看到下圖信息說明安裝成功 (如果重新編譯需要先make clean清理之前的已經編譯的可執行文件)
#### 7. 查看php版本
`/alidata/server/php/bin/php -v`

#### 8. 創建配置文件
創建www.conf配置文件
~~~
cd /alidata/server/php/etc/php-fpm.d
cp www.conf.default www.conf
~~~
創建php-fpm.conf配置文件
~~~
cd /alidata/server/php/etc
cp php-fpm.conf.default php-fpm.conf
~~~
創建php.ini配置文件
將安裝源文件目錄里的php.ini-production或者php.ini-development修改后綴拷貝到php安裝目錄的etc文件夾內
~~~
cd /home/php/php-7.4.1
cp php.ini-production /alidata/server/php/etc/php.ini
~~~
#### 9. 將bin和sbin路徑加入到path變量中。
配置環境變量
`vim /etc/profile`
加入下面內容
~~~
PATH=/user/local/cmake/bin:$PATH
PATH=/alidata/server/mysql/bin:$PATH
PATH=/alidata/server/php/bin:$PATH
export PATH
~~~
保存后執行source命令使配置立即生效
`source /etc/profile`
#### 10. 配置php-fpm到系統服務
~~~
cp /home/php/php-7.4.1/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
~~~
配置php-fpm.conf
`vim /alidata/server/php/etc/php-fpm.conf
`
將pid(;pid = run/php-fpm.pid)前的;去掉。
#### 12. 配置開機自動啟動
```
chkconfig --add /etc/init.d/php-fpm
chkconfig php-fpm on
```
#### 13. 配置nginx解析php文件
~~~
cd /alidata/server/nginx/conf
vim nginx.conf
~~~
將php解析前的#都去掉,如下圖。然后保存修改。
~~~
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
~~~
nginx重新加載配置文件
`service nginx reload`

#### 14. 創建一個php文件
如果設置了根目錄,在根目錄里面新建
`vi /alidata/server/nginx/html/phpinfo.php`
輸入如下代碼
`<?php phpinfo(); ?>`
保存
配置沒問題就會看到下面的頁面
