david2tdw / blog

学习记录
1 stars 1 forks source link

[linux] centOS7中nginx配置 #205

Open david2tdw opened 3 years ago

david2tdw commented 3 years ago

配置 EPEL源

sudo yum install -y epel-release
sudo yum -y update

安装Nginx

sudo yum install -y nginx

安装成功后,默认的网站目录为: /usr/share/nginx/html

默认的配置文件为:/etc/nginx/nginx.conf

自定义配置文件目录为: /etc/nginx/conf.d/

操作Nginx

1.启动 Nginx

systemctl start nginx

2.停止Nginx

systemctl stop nginx

3.重启Nginx

systemctl restart nginx

4.查看Nginx状态

systemctl status nginx

5.启用开机启动Nginx

systemctl enable nginx

6.禁用开机启动Nginx

systemctl disable nginx

Centos 7下安装配置Nginx

david2tdw commented 3 years ago

如果系统使用了防火墙(firewall),执行下面命令允许http/https

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

CentOS 7 - 安装Nginx

david2tdw commented 3 years ago

nginx默认监听80端口,如果未关闭防火墙需要配置iptables规则开放80端口.

编辑配置文件:vim /etc/sysconfig/iptables

在文件中间添加iptables规则

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启防火墙:

service iptables restart

或者关闭iptables规则:

iptables -F && iptables -t nat -F
david2tdw commented 3 years ago

查看Nginx服务对应的端口是否启动

netstat -lnt | grep 80

查看 nginx.conf 配置文件目录和nginx的安装目录

nginx -t
david2tdw commented 3 years ago

验证nginx安装成功 直接在浏览器输入linux服务器的ip地址,查看是否能显示centOS的介绍页面。

david2tdw commented 3 years ago

nginx启动报错 - bind() to 0.0.0.0:8090 failed (13: Permission denied)

原因selinux限制了http的端口

解决办法:关闭seLinux

修改/etc/selinux/config文件中的SELINUX值

SELINUX=disabled

然后重启。如果不想重启系统,使用命令setenforce 0

注: setenforce 1 设置SELinux 成为enforcing模式 setenforce 0 设置SELinux 成为permissive模式

[Nginx] Centos nginx bind() failed permission denied 关闭SELinux

david2tdw commented 3 years ago

查看防火墙状态:

systemctl status firewalld
或者
firewall-cmd --state
david2tdw commented 3 years ago

查看监听的端口

netstat -lnpt

PS: centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools

检查端口被哪个进程占用

netstat -lnpt |grep 5672

查看进程的详细信息

ps 6832

中止进程

kill -9 6832
david2tdw commented 3 years ago

在添加端口到/etc/sysconfig/iptables后,需要启动nginx,才能查看端口的监听情况。没启动查询不到对应的端口。

-A INPUT -p tcp -m state --state NEW -m tcp --dport 9999 -j ACCEPT
netstat -lnpt

iptables使用命令添加端口配置

使用命令直接修改(注意:需要save生效, -I是添加在第一条)

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT    #开放8080端口

service iptables save    #使配置生效

iptables命令小结

iptables -I INPUT -p tcp -m multiport --dport 22,80 -j ACCEPT    #同时开放22和80端口

iptables -I INPUT -p tcp --dport 5000:6000 -j ACCEPT    #开放5000-6000端口

iptables -I INPUT -p all -s 0.0.0.0/0 -j ACCEPT    #允许某个网段的ip访问

iptables -I INPUT -s 0.0.0.0 -p tcp --dport 8080 -j ACCEPT    #允许某个ip的8080端口访问

iptables -I INPUT -p tcp -s 0.0.0.0 -j DROP    #禁止某台主机访问

iptables -D INPUT 7    #移除第七条规则

(注:放行使用ACCEPT,禁止使用DROP)

centos7 iptables

david2tdw commented 3 years ago

每次重启后要检查iptables是否启动起来,iptables在重启后不会自动启动。

systemctl status iptables
david2tdw commented 3 years ago

每次重启后 要检查iptables是否启动起来,iptables在重启后不会自动启动。

systemctl status iptables

查看SELinux状态, 如果是Enforcing 的话会阻止网络访问。

getenforce

临时关闭SELinux,Linux重启后SELinux恢复原状态

setenforce 0

重新启动网络访问,否则无法ping通。

service network start
david2tdw commented 3 years ago

iptables 命令:

service iptables start
service iptables stop
service iptables restart
service iptables status  #如果没有启动,将提示没有启动,否则将会显示已经添加的过滤规则
service iptables save    #保存添加的规则,记得每次更改规则以后这样保存一下