在CentOS安裝Nginx。
1. 安装Nginx
使用命令安装1:
$ sudo dnf install nginx
安装完成后设置开机启动并启动Nginx服务:
$ sudo systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
$ sudo systemctl start nginx.service
验证Nginx是否启动成功,本地访问:
$ curl -I localhost
HTTP/1.1 200 OK
Server: nginx/...
Date: Sat, 02 May 2020 02:17:06 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
Connection: keep-alive
ETag: "5d9bab28-fd9"
Accept-Ranges: bytes
查看当前监听的端口,可以看到80端口已经被监听:
$ netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5355 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp6 0 0 :::3306 :::* LISTEN -
tcp6 0 0 :::5355 :::* LISTEN -
tcp6 0 0 :::80 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
2. 设置防火墙
CentOS 7/8使用firewalld作为防火墙管理工具,查看当前防火墙状态:
$ sudo firewall-cmd --permanent --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
防火墙此时没有允许http服务,即使监听了http端口,通过Internet也是无法访问Nginx的。允许防火墙通过http服务:
$ sudo firewall-cmd --permanent --add-service=http
success
再次查看防火墙状态,出现了http服务:
$ sudo firewall-cmd --permanent --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
刷新使防火墙更新生效:
$ sudo firewall-cmd --reload
success
此时可以通过Internet访问Nginx了,使用IP访问验证:
curl.exe -I *:80
HTTP/1.1 200 OK
Server: nginx/...
Date: Sat, 02 May 2020 02:37:49 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
Connection: keep-alive
ETag: "5d9bab28-fd9"
Accept-Ranges: bytes
使用浏览器也可以正常访问,至此完成安装Nginx。