1.添加Nginx的Yum仓库,创建一个新的repo文件 /etc/yum.repos.d/nginx.repo 文件内容
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2.使用Yum安装Nginx
yum install nginx
3.启动Nginx服务并设置Nginx开机自启
systemctl start nginx
systemctl enable nginx
4.配置防火墙 允许HTTP和HTTPS流量
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
如遇配置防火墙发生(FirewallD is not running)错误,解决方法:
(1)启动FirewallD服务命令:
systemctl start firewalld.service #开启服务
systemctl enable firewalld.service #设置开机启动
(2)查看FirewallD防火墙状态:
systemctl status firewalld
防火墙状态没有问题后可以继续重复配置防火墙操作了。
5.确保你的服务器安全组已开启80端口,否则会被拦截。
6.编辑Nginx的默认配置文件 /etc/nginx/nginx.conf 或者在 /etc/nginx/conf.d/ 目录下创建一个新的配置文件,例如 default.conf文件。
server {
listen 80;
server_name 你的域名
location / {
root /资源文件路径;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
7.重启nginx
systemctl restart nginx