一,编译安装nginx
在nginx 官网下载了最新的源码包,(我这边没加编译选项,如果到时候要加什么功能的话,直接重新编译,然后替换nginx那个二进制文件:/apps/nginx/sbin/nginx 即可完成升级)
#创建nginx目录与用户
[root@iZ2ze1o ~]# useradd nginx -s /sbin/nologin -u 2000
[root@iZ2ze1o ~]# chown nginx.nginx -R /apps/nginx/
#解压源码包
[root@iZ2ze1o ~]# tar xvf nginx-1.18.0.tar.gz
#下载依赖环境
[root@iZ2ze1o ~]# yum install pcre pcre-devel zlib-devel
[root@iZ2ze1o ~]# cd nginx-1.18.0/
#进行编译
[root@iZ2ze1o ~]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx
[root@iZ2ze1o ~]# make && make install
二,编译安装php
# 安装依赖环境
[root@iZ2ze1o ~]# yum install gcc libxml2-devel bzip2-devel openssl-devel libmcrypt-devel -y
#解压目录
[root@iZ2ze1o ~]# tar xvf php-7.3.18.tar.gz
[root@iZ2ze1o ~]# cd php-7.3.18/
# 编译参数
[root@iZ2ze1o ~]# ./configure --prefix=/apps/php7.3.18 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
[root@iZ2ze1o ~]# make && make install、
# 修改path路径
[root@iZ2ze1o ~]# echo 'PATH=/apps/php7.3.18/bin:/app/nginx/sbin:$PATH' > /etc/profile.d/nginx_php.sh
[root@iZ2ze1o ~]# source /etc/profile.d/nginx_php.sh
# 配置php文件
[root@iZ2ze1o ~]# cp php.ini-production /apps/php7.3.18/etc/php.ini
# 拷贝system文件,这样就可以使用systemctl 来管理了
[root@iZ2ze1o ~]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
[root@iZ2ze1o ~]# cd /apps/php7.3.18/etc/
[root@iZ2ze1o ~]# cp php-fpm.conf.default php-fpm.conf
[root@iZ2ze1o ~]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@iZ2ze1o ~]# sed -i '/^user /c user = nginx' /apps/php7.3.18/etc/php-fpm.d/www.conf
[root@iZ2ze1o ~]# sed -i '/^group /c group = nginx' /apps/php7.3.18/etc/php-fpm.d/www.conf
# 可以去自行修改文件,来使用端口加ip通信,也可以使用socket
[root@iZ2ze1o ~]# sed -i '/^listen /c listen = /run/php-fpm.sock' /apps/php7.3.18/etc/php-fpm.d/www.conf
[root@iZ2ze1o ~]# sed -i '/^;listen.owner /c listen.owner = nginx ' /apps/php7.3.18/etc/php-fpm.d/www.conf
[root@iZ2ze1o ~]# sed -i '/^;listen.group /c listen.group = nginx ' /apps/php7.3.18/etc/php-fpm.d/www.conf
[root@iZ2ze1o ~]# sed -i '/^;listen.mode /c listen.mode = 0660 ' /apps/php7.3.18/etc/php-fpm.d/www.conf
三,配置nginx 来转发php 的请求
[root@iZ2ze1o ~]# cat /apps/nginx/conf/nginx.conf
# 注释掉之前的,复制条新的
# 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;
# 注意这里使用socket链接
fastcgi_pass unix:/run/php-fpm.sock;
fastcgi_index index.php;
注意这里需要把/scripts更改成$document_root
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
如果不把/scripts 改掉
页面会显示文件找到不到
nginx error日志会显示:
2020/05/22 10:36:23 [error] 14164#0: *397 FastCGI sent in stderr: “Primary script unknown” \while reading response header from upstream, client: , server: localhost, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://unix:/run/php-fpm.sock:”, host: “”