参考官方文档: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

  1. wget https://www.php.net/distributions/php-7.4.20.tar.gz

解压&进入目录

  1. #解压:
  2. tar -zxvf php-7.3.11.tar.gz
  3. #进入解压目录:
  4. cd php-7.3.11

配置编译安装

安装依赖

Ubuntu环境

  1. apt-get install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libxml2-dev
  2. apt-get install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-dev
  3. apt-get install glibc glibc-devel bzip2 bzip2-devel ncurses ncurses-devel
  4. apt-get install curl curl-devel openssl openssl-devel

Centos环境

  1. yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libxml2-dev libxml2
  2. yum install -y libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-dev
  3. yum install -y glibc glibc-devel bzip2 bzip2-devel ncurses ncurses-devel
  4. yum install -y curl curl-devel openssl openssl-devel libzip libzip-devel

配置

  1. ./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

配置成功后,提示

  1. Generating files
  2. configure: creating ./config.status
  3. creating main/internal_functions.c
  4. creating main/internal_functions_cli.c
  5. +--------------------------------------------------------------------+
  6. | License: |
  7. | This software is subject to the PHP License, available in this |
  8. | distribution in the file LICENSE. By continuing this installation |
  9. | process, you are bound by the terms of this license agreement. |
  10. | If you do not agree with the terms of this license, you must abort |
  11. | the installation process at this point. |
  12. +--------------------------------------------------------------------+
  13. Thank you for using PHP.
  14. config.status: creating php7.spec
  15. config.status: creating main/build-defs.h
  16. config.status: creating scripts/phpize
  17. config.status: creating scripts/man1/phpize.1
  18. config.status: creating scripts/php-config
  19. config.status: creating scripts/man1/php-config.1
  20. config.status: creating sapi/cli/php.1
  21. config.status: creating sapi/phpdbg/phpdbg.1
  22. config.status: creating sapi/cgi/php-cgi.1
  23. config.status: creating ext/phar/phar.1
  24. config.status: creating ext/phar/phar.phar.1
  25. config.status: creating main/php_config.h
  26. config.status: executing default commands

编译

执行make命令

  1. make
  2. make test
  3. make install

对于make命令的解释可参考
https://blog.csdn.net/chang_ge/article/details/80848354

配置PHP-FPM运行环境

修改PHP配置

  1. cd /usr/local/php7/etc
  2. ls -l
  3. -rw-r--r-- 1 root root 5392 Oct 25 20:54 php-fpm.conf.default
  4. drwxr-xr-x 2 root root 4096 Oct 25 20:54 php-fpm.d

其中 php-fpm.conf.default 为配置文件,拷贝一份并命名为 php-fpm.conf

  1. cp php-fpm.conf.default php-fpm.conf

在文件中的最后一行,有一段代码:

  1. 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

  1. cp www.conf.default www.conf

启动PHP-FPM

  1. # 启动服务
  2. sudo /usr/local/php7/sbin/php-fpm
  3. # 查看运行进程
  4. ps -ef | grep php-fpm
  5. # 查看9000端口
  6. netstat -anp | grep 9000

配置Nginx服务

打开nginx.conf,新增PHP支持

  1. location ~ \.php$ {
  2. root html;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. include fastcgi_params;
  7. }

重新加载配置

  1. /usr/local/nginx/sbin/nginx -s reload