nginx降权+匹配php

14 篇文章 0 订阅
5 篇文章 0 订阅

目录

nginx降权启动

确认普通用户无法开启nginx

 创建普通用户:

 测试是否可以启动nginx:

创建必需的相关文件

使用root用户copy配置文件中网页支持类型文件

使用root用户拷贝nginx配置文件

设置权限

 修改配置文件

安装 PHP 7.4,配合 Nginx

安装 PHP 和 PHP FPM 软件包:

修改权限 

配置php-fpm

修改Nginx配置文件

测试: 

在/www/env/nginx/html下创建index.php文件:

打开浏览器:


nginx降权启动

确认普通用户无法开启nginx

 创建普通用户:

root@ubuntu:~# useradd -d /home/test -m test
root@ubuntu:~# passwd test
New password: 
Retype new password: 
passwd: password updated successfully
root@ubuntu:~# 

切换到test用户: 

 测试是否可以启动nginx:

 启动失败

创建必需的相关文件

$ mkdir nginx
$ cd nginx 
$ mkdir conf logs www sbin

使用root用户copy配置文件中网页支持类型文件

root@ubuntu:/www/env/nginx/conf# cp /www/env/nginx/conf/mime.types /home/test/nginx/conf/

使用root用户拷贝nginx配置文件

root@ubuntu:~# cp /www/env/nginx/conf/nginx.conf /home/test/nginx/conf/

设置权限

root@ubuntu:~# chown -R test:test /www/env/nginx/
# 将当前前目录下的所有文件与子目录的拥有者皆设为 test,群体的使用者 test:

 修改配置文件

worker_processes  4;
worker_rlimit_nofile 65535;
error_log  /home/test/nginx/logs/error.log;
user test test;
pid  /home/test/nginx/logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       /home/test/nginx/conf/mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8080;
        server_name  localhost;
        root  /home/test/nginx/www;
        location / {
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        access_log  /home/test/nginx/logs/access.log;
    }

全路径启动nginx -c参数使用指定的配置文件而不是conf目录下的nginx.conf

/www/env/nginx/sbin/nginx -c /home/test/nginx/conf/nginx.conf &> /dev/null 

安装 PHP 7.4,配合 Nginx

安装 PHP 和 PHP FPM 软件包:

apt install php-fpm

检查服务状态,运行:

systemctl status php7.4-fpm

修改权限 

chmod 777 /run/php/php7.2-fpm.sock

配置php-fpm

修改配置监听9000端口来处理nginx的请求(这种方法一般在windows上使用),
打开 /etc/php/7.2/fpm/pool.d/www.conf 文件找到如下位置注释第一行添加第二行

;listen = /run/php/php7.2-fpm.sock
listen = 127.0.0.1:9000

修改Nginx配置文件

找到下面这部分代码取消注释,修改配置

        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.conf;
        }

测试: 

在/www/env/nginx/html下创建index.php文件:

root@ubuntu:/www/env/nginx/html# cat index.php 
<?php
phpinfo()
?>

打开浏览器:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戲子 鬧京城°ぃ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值