更改 Nginx 服务的默认用户

时间:2024-03-08 14:54:28

为什么要更改 Nginx 服务的默认用户:就像更改 ssh 的默认 22 端口一样,增加安全性,Nginx 服务的默认用户是 nobody ,我们更改为 nginx

1、添加 nginx 用户

useradd nginx -s /sbin/nologin -M

2、更改 Nginx 配置文件

复制代码
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
user nginx nginx;           # 指定Nginx服务的用户和用户组
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server_tokens   off;
    server {
        listen       80;
        server_name  www.abc.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}
复制代码

3、重新加载 Nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

4、验证是否生效

[root@localhost ~]# ps aux | grep nginx 
root       8901  0.0  0.1  45036  1784 ?        Ss   13:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      8909  0.0  0.1  45460  1828 ?        S    13:59   0:00 nginx: worker process      # Nginx进程的所属用户为nginx