Nginx 安装说明

创建nginx 用户

1
2
groupadd -r nginx
useradd -s /sbin/nologin -g nginx -M nginx

下载nginx

1
wget https://nginx.org/download/nginx-1.16.0.tar.gz

解压

1
tar -zxvf nginx-1.16.0.tar.gz

安装依赖

1
yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel
  • pcre库(支持rewrite模块)、zlib库(支持gzip模块)和openssl库(支持ssl模块)等。

编译

1
2
./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx  --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module
make && make install

Nginx Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
# PIDFile=/usr/local/nginx/pid/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
```

- 开机启动`nginx `
```bash
systemctl enable nginx.service
  • 禁止开机启动nginx

    1
    systemctl disable nginx.service
  • 启动nginx

    1
    systemctl start nginx.service
  • 停止nginx

    1
    systemctl stop nginx.service
  • 重新加载nginx

    1
    systemctl reload nginx.service
  • 查看nginx状态

    1
    systemctl status nginx.service

权限

创建 nginx 用户组和用户

1
2
groupadd nginx  
useradd -g nginx -M nginx

useradd命令 -g 表示用户组
useradd 命令的 -M 参数用于不为 nginx建立 home 目录

1
2
3
userdel nginx 
groupdel nginx
usermod –G nginx nginx //(强制删除该用户的主目录和主目录下的所有文件和子目录)

修改/etc/passwd,使得nginx用户无法bash登陆(nginx用户后面由/bin/bash改为/sbin/nologin)

1
vim /etc/passwd

然后找到有 nginx 那一行,把它修改为(后面由/bin/bash改为/sbin/nologin):

1
nginx:x:1001:1002::/home/nginx:/bin/bash:/sbin/nologin

修改配置文件

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

第一行加入 user nginx nginx;

1
2
3
user nginx nginx;
worker_processes 1;
...

将nginx目录的权限赋给nginx用户

1
2
cd /usr/local/nginx
chown nginx:nginx