在 RHEL8 和 CentOS8 上安装 Nginx
本文最后更新于 1781 天前,其中的信息可能已经有所发展或是发生改变。

介绍

Nginx(发音同 engine x)是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个 BSD-like 协议下发行,可以在 UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及 Microsoft Windows 等操作系统中运行。

Nginx 作为大型互联网网站的 Web 服务软件有着广泛的支持率,例如阿里系的 Tengine、CloudFlare 的 cloudflare-nginx、又拍云用的 OpenResty 都是基于 Nginx 加入企业的定制化需求,Nginx 是一款接受过大规模访问验证的高性能 Web 服务器软件。同时各大企业也会将自己开发的组件回馈给 Nginx 社区,让 Nginx 有着非常良好的扩展生态。

安装

目前 RHEL8 和 CentOS8 上主要有两种,一个是RHEL系统的 APPStream(应用流)和 Nginx 社区官方的软件源。

RHEL 系统的 APPStream 的稳定性肯定是最高的,也会最贴合系统,APPStream 的,目的是为了保持软件版本跟上主流支持,并让系统同时支持安装多个版本,但是目前不知道 Nginx 是否会有版本的跟进支持

Nginx 社区源则可以体验到最新的 Nginx 版本,但毕竟是社区版本,其中好坏也是仁者见仁智者见智的了。

下面安装将分为 APPStream 和 社区源两个部分

APPStream 安装

由于系统默认包含源,所以我们直接安装即可。

一、安装 Nginx

yum install nginx -y

二、启动软件并设置开机启动

systemctl enable nginx
systemctl start nginx

三、查看命令行检测安装完成。

[root@www.mf8.biz ~]# nginx -V
nginx version: nginx/1.14.1
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 
built with OpenSSL 1.1.1 FIPS  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

四、设置防火墙规则

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

五、访问IP即可

配置文件目录

主配置文件:/etc/nginx/nginx.conf

默认文件目录:/usr/share/nginx/html

错误日志:/var/log/nginx/error.log

访问日志:/var/log/nginx/access.log

Nginx 社区源安装

一、安装社区版软件源:

yum install dnf-utils -y

curl -o /etc/yum.repos.d/nginx.repo https://gist.githubusercontent.com/ivmm/23ac854c154e3529a08db68a44ae81c3/raw/c46c159eb5cacd66e69420efa2b12ce6a99fd642/nginx.repo 

二、选择安装稳定版还是主线版

安装稳定版输入:

yum-config-manager --enable nginx-stable

安装主线版(测试版)输入:

yum-config-manager --enable nginx-mainline

三、安装 Nginx

yum --disablerepo=rhel-8-for-x86_64-appstream-rpms install nginx -y

注释:

--disablerepo=rhel-8-for-x86_64-appstream-rpms 这个参数是为了临时屏蔽默认的 APPStream 源,不然会提示找不到 nginx 社区版的包。

四、启动软件并设置开机启动

systemctl enable nginx
systemctl start nginx

五、查看命令行检测安装完成。

[root@www.mf8.biz]# nginx -V
nginx version: nginx/1.16.0
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 
built with OpenSSL 1.1.1 FIPS  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

六、设置防火墙规则

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

七、访问IP即可

配置文件目录

主配置文件:/etc/nginx/nginx.conf

默认文件目录:/usr/share/nginx/html

错误日志:/var/log/nginx/error.log

访问日志:/var/log/nginx/access.log

上一篇
下一篇