参考官方文档:https://www.php.net/manual/zh/install.unix.nginx.php 安装配置参考:https://blog.csdn.net/csdn_zhongwu/article/details/88367236
下载源码包
地址:https://www.php.net/downloads.php
wget https://www.php.net/distributions/php-7.4.20.tar.gz
解压&进入目录
#解压:
tar -zxvf php-7.3.11.tar.gz
#进入解压目录:
cd php-7.3.11
配置编译安装
安装依赖
Ubuntu环境
apt-get install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libxml2-dev
apt-get install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-dev
apt-get install glibc glibc-devel bzip2 bzip2-devel ncurses ncurses-devel
apt-get install curl curl-devel openssl openssl-devel
Centos环境
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libxml2-dev libxml2
yum install -y libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-dev
yum install -y glibc glibc-devel bzip2 bzip2-devel ncurses ncurses-devel
yum install -y curl curl-devel openssl openssl-devel libzip libzip-devel
配置
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php/etc --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-zlib-dir=/usr/local/zlib --with-libxml-dir=/usr/local/libxml2/ --with-iconv-dir=/usr/local/libiconv --enable-libxml --enable-xml --enable-fpm --enable-mbstring=all --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --without-pear --with-gettext --enable-session --with-curl --enable-shared --with-gd --with-pdo-mysql --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-libdir=lib64 --with-pdo-sqlite --with-png-dir --with-jpeg-dir --with-bz2 --enable-bcmath --with-mhash --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sysvsem --enable-sysvshm --disable-fileinfo
配置成功后,提示
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
编译
执行make命令
make
make test
make install
对于make命令的解释可参考
https://blog.csdn.net/chang_ge/article/details/80848354
配置PHP-FPM运行环境
修改PHP配置
cd /usr/local/php7/etc
ls -l
-rw-r--r-- 1 root root 5392 Oct 25 20:54 php-fpm.conf.default
drwxr-xr-x 2 root root 4096 Oct 25 20:54 php-fpm.d
其中 php-fpm.conf.default 为配置文件,拷贝一份并命名为 php-fpm.conf
cp php-fpm.conf.default php-fpm.conf
在文件中的最后一行,有一段代码:
include=/usr/local/php7/etc/php-fpm.d/*.conf
说的是,默认加载 所有在 /usr/local/php731/etc/php-fpm.d 目录中所有 .conf的文件
进入到 /usr/local/php731/etc/php-fpm.d/ 目录中,将www.conf.default拷贝一份并命名为www.conf
cp www.conf.default www.conf
启动PHP-FPM
# 启动服务
sudo /usr/local/php7/sbin/php-fpm
# 查看运行进程
ps -ef | grep php-fpm
# 查看9000端口
netstat -anp | grep 9000
配置Nginx服务
打开nginx.conf,新增PHP支持
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
重新加载配置
/usr/local/nginx/sbin/nginx -s reload