前言Nginx (engine x) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器。。 本例演示 CentOS 7 下安装和配置 Nginx 的基本步骤。环境说明CentOS 7(Minimal Install)$ cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)步骤步骤 1: 添加 yum 源Nginx 不在默认的 yum 源中,可以使用 epel 或者官网的 yum 源,本例使用官网的 yum 源。$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm安装完 yum 源之后,可以查看一下。$ sudo yum repolistLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comrepo id repo name statusbase/7/x86_64 CentOS-7 - Base 9,911extras/7/x86_64 CentOS-7 - Extras 368nginx/x86_64 nginx repo 108updates/7/x86_64 CentOS-7 - Updates 1,041repolist: 11,428可以发现 nginx repo 已经安装到本机了。步骤 2: 安装yum 安装 Nginx,非常简单,一条命令。$ sudo yum install nginx步骤 3: 启动配置 Nginx 服务设置开机启动$ sudo systemctl enable nginx启动服务$ sudo systemctl start nginx停止服务$ sudo systemctl restart nginx重新加载,因为一般重新配置之后,不希望重启服务,这时可以使用重新加载。$ sudo systemctl reload nginx配置### nginx默认配置位置/etc/nginx/conf.d/default.conf## 默认wwwroot位置/usr/share/nginx/html步骤 4: 打开防火墙端口默认 CentOS7 使用的防火墙 firewalld 是关闭 http 服务的(打开 80 端口)。$ sudo firewall-cmd –zone=public –permanent –add-service=httpsuccess$ sudo firewall-cmd –reloadsuccess打开之后,可以查看一下防火墙打开的所有的服务$ sudo sudo firewall-cmd –list-servicessh dhcpv6-client http可以看到,系统已经打开了 http 服务。步骤 5: 反向代理Nginx 是一个很方便的反向代理,配置反向代理可以参考 Module ngx_http_proxy_module 。本文不做累述。需要指出的是 CentOS 7 的 SELinux,使用反向代理需要打开网络访问权限。$ sudo setsebool httpd_can_network_connect 1打开网络权限之后,反向代理可以使用了。结论本文演示了 CentOS 7 下 yum 安装 Nginx,配置服务等。参考资料Install Nginx Binary ReleasesModule ngx_http_proxy_module