下载安装

下载地址:https://nginx.org/en/download.html

1
2
3
4
5
6
wget https://nginx.org/download/nginx-1.21.2.tar.gz
tar -zxvf https://nginx.org/download/nginx-1.21.2.tar.gz
mv nginx-1.21.2 nginx
cd nginx
./configure --prefix=/opt/nginx # 配置安装路径,默认安装 /usr/local/nginx,可根据自己喜好
make && make install # 如果系统是最小安装,需要执行 yum install make -y

开启nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cd /opt/nginx/sbin
./nginx

[root@instance-1 sbin]# curl 34.125.204.106
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

添加环境变量

1
2
3
4
5
vim /etc/profile
# 末尾处加上
export PATH=$PATH:/opt/nginx/sbin

source /etc/profile

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 查看进程ID
[root@instance-1 sbin]# cat /opt/nginx/logs/nginx.pid
21013
[root@instance-1 sbin]# ps -ef | grep nginx
root 21013 1 0 09:39 ? 00:00:00 nginx: master process ./nginx
nobody 21014 21013 0 09:39 ? 00:00:00 nginx: worker process
root 21297 18222 0 11:15 pts/0 00:00:00 grep --color=auto nginx

# 关闭服务
kill $(cat /opt/nginx/logs/nginx.pid)

# 重载服务
nginx -s reload

# 重载日志文件
nginx -s reopen

# 测试配置文件是否正确
nginx -t -c /opt/nginx/conf/nginx.conf

# 关闭服务
nginx -s stop # 快速停止
nginx -s quit # 有序停止

# 启动服务
nginx -c /opt/nginx/conf/nginx.conf