准备工作

进入软件包存放目录(这个目录看个人,喜欢放哪里就放哪里)

cd /root

下载软件包

wget https://nginx.org/download/nginx-1.22.1.tar.gz
wget https://www.php.net/distributions/php-7.4.32.tar.gz

提前安装一些必要的依赖包

yum -y install openssl openssl-devel sqlite-devel oniguruma-devel pcre pcre-devel zlib zlib-devel gcc-c++ gcc openssl*

创建www账户 用来启动nginx

useradd www -s /sbin/nologin

编译安装Nginx

进入软件目录/root,解压压缩包

cd /root
tar zxvf nginx-1.22.1.tar.gz

进入解压后的目录,对Nginx进行配置

cd nginx-1.22.1/

./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--without-http_memcached_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre

编译和安装

make && make install

启动Nginx

/usr/local/nginx/sbin/nginx

关闭Nginx进程

killall nginx

查看Nginx进程

ps -ef|grep nginx

Nginx命令做软连接方便使用

ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

编写Nginx启动脚本

cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=//usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

修改完systemctl服务,需要重新加载下daemon

systemctl daemon-reload

用systemctl启动Nginx服务,并查看状态

systemctl start nginx
systemctl status nginx

设置Nginx开机启动

systemctl enable nginx

编译安装PHP

解压php源码包

tar zxvf php-7.4.32.tar.gz

进入解压后的文件夹

cd php.7.4.32

安装PHP常用扩展

yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel sqlite-devel oniguruma-devel

PHP编译前配置

./configure \
--prefix=/usr/local/php \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session --enable-ftp \
--enable-pdo \
-enable-tokenizer --enable-zip

如果 configure 成功的话,将会看到以下类似字样:

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

编译和安装

make && make install

至此,PHP7已经编译安装完成,接下来进行PHP的配置

PHP配置文件修改

首先,复制php.ini文件(该文件在PHP编译安装文件目录/root/php.5.4.32)

cp php.ini-development /usr/local/php/lib/php.ini

修改时区,查找 date.timezone,改成(主要将前面的 ; 去掉,这个是注释用的):

grep date.timezone /usr/local/php/lib/php.ini 
## 会输出以下数据
; http://php.net/date.timezone
;date.timezone =
## 设置对应的值
sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php/lib/php.ini 
## 查看值是否已发生变更
grep date.timezone /usr/local/php/lib/php.ini
## 输出如下
; http://php.net/date.timezone
date.timezone = Asia/Shanghai

至此PHP7.4已经安装好,下面验证一下

/usr/local/php/bin/php -v
输出如下
PHP 7.4.32 (cli) (built: Oct 28 2022 17:57:54) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

接下来配置php-fpm,复制php-fpm的配置文档

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

修改/usr/local/php/etc/php-fpm.d/www.conf,把启动用户改为和Nginx服务同一个启动用户(前面Nginx使用的是www账户,这里改成和Nginx使用一样的账户;一般都是使用www用户)

groupadd www
useradd -g www www

启动php-fpm

/usr/local/php/sbin/php-fpm

使用命令进行验证是否启动成功

ps aux | grep php-fpm

输出以下类似信息,则代表启动正常

nginx    15100  0.0  0.8 303720 17384 ?        S    11:12   0:03 php-fpm: pool www
root     15253  0.0  0.0 112812   980 pts/0    S+   15:07   0:00 grep --color=auto php-fpm
nginx    17817  0.0  0.7 295300 15648 ?        S    11:32   0:02 php-fpm: pool www
root     25982  0.0  0.3 211860  6252 ?        Ss   00:48   0:01 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx    27605  0.0  0.8 305756 17660 ?        S    12:43   0:00 php-fpm: pool www

Nginxphp-fpm整合

编辑/usr/local/nginx/conf/nginx.conf

vim /usr/local/nginx/conf/nginx.conf

修改如下:

  1. 第一处修改
location / {
    root   html;
    index  index.html index.htm;
}

改为

location / {
    root   html;
    index  index.php index.html index.htm;
}
  1. 第二处修改
location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     include        fastcgi_params;
}

改为

location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     #include        fastcgi_params;
     include        fastcgi.conf;
}

重新载入nginx的配置文件:

/usr/sbin/nginx -s reload

测试php文件

进入目录,创建index.php文件

cd /usr/local/nginx/html
touch index.php

输入以下内容

<?php
   phpinfo();
?>

浏览器访问

访问http://你的服务器ip/index.php,就可以看到输出php信息了
2022-10-31T07:14:59.png

至此,NginxPHP环境整合配置完毕

Last modification:January 15, 2023
If you think my article is useful to you, please feel free to appreciate