CentOS 7 下 yum 安装和配置 Nginx (亲测无坑)

注意:以下安装步骤请严格确认您的Linux distribution 是否是 CentOS 7 的版本,不是相同版本谨慎操作,所有操作亲测是对的,没有坑~~
一、什么是Nginx?

个人理解:Apache 和 Nginx 是 HTTP server,而 Tomcat 是 Application server。

Nginx 相比较 Apache 具有占用内存少,稳定性高等优势,是一个轻量级高并发的 WEB 服务器。可以用来做正向代理、反向代理、负载均衡,HTTP服务器(动静分离), 具体详细的不在此说明。

二、具体安装步骤:
1、首先查看 Linux distribution 的版本号:
[root@localhost /]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
2、Nginx 不在默认的 yum 源中,首先将Nginx加入到 yum 源里
[root@localhost /]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.shxlJZ: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
3、然后执行 yum repolist 查看一下,发现 Nginx 已经安装到本机了
[root@localhost /]# yum repolist
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                 | 3.6 kB  00:00:00
extras                                                                                                                                                                               | 3.4 kB  00:00:00
nginx                                                                                                                                                                                | 2.9 kB  00:00:23
updates                                                                                                                                                                              | 3.4 kB  00:00:00
(1/5): base/7/x86_64/group_gz                                                                                                                                                        | 166 kB  00:00:00
(2/5): extras/7/x86_64/primary_db                                                                                                                                                    | 201 kB  00:00:00
(3/5): updates/7/x86_64/primary_db                                                                                                                                                   | 4.2 MB  00:00:00
(4/5): base/7/x86_64/primary_db                                                                                                                                                      | 6.0 MB  00:00:02
(5/5): nginx/x86_64/primary_db                                                                                                                                                       |  45 kB  00:00:41
repo id                                                       repo name                                                                                           status
base/7/x86_64                                                 CentOS-7 - Base                                                                                     10019
extras/7/x86_64                                               CentOS-7 - Extras                                                                                     413
nginx/x86_64                                                  nginx repo                                                                                            150
updates/7/x86_64                                              CentOS-7 - Updates                                                                                   1840
repolist: 12422
4、安装nginx
[root@localhost /]# yum install nginx
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.0-1.el7.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================
 Package                          Arch                         Version                                                      Repository                        Size
============================================================================================================================================================================================================
Installing:
nginx                            x86_64                       1:1.16.0-1.el7.ngx                                           nginx                                   766 k

Transaction Summary
============================================================================================================================================================================================================
Install  1 Package

Total download size: 766 k
Installed size: 2.7 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.16.0-1.el7.ngx.x86_64.rpm                                                                                                                                                    | 766 kB  00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : 1:nginx-1.16.0-1.el7.ngx.x86_64                                                                                                                                                          1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.16.0-1.el7.ngx.x86_64                                                                                                                                                          1/1

Installed:
  nginx.x86_64 1:1.16.0-1.el7.ngx

Complete!

到这里为止,我们就将 Nginx 安装我好了~

三、Nginx 相关配置
1、查看 Nginx 版本
[root@localhost /]# nginx -v
nginx version: nginx/1.16.0
2、启动 Nginx 服务
[root@localhost /]# systemctl start nginx
3、访问 Nginx 服务

1)、成功启动之后你可以本地访问一下,可以看到 Welcome to nginx :

[root@localhost /]# curl -i localhost
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Wed, 08 May 2019 03:15:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 14:36:26 GMT
Connection: keep-alive
ETag: "5cbf22ea-264"
Accept-Ranges: bytes

<!DOCTYPE html>


</span>Welcome to nginx<span class="token operator">!</span><span class="token filehandle symbol">



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.
> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.>

Thank you for using nginx.</em>>

2)也可以远程使用公网 IP 来访问,这时你会发现访问被拒绝,因为 CentOS7 默认使用的防火墙 firewalld 是关闭 http 服务的,或者没有开放 80 端口:

# 查看防火墙打开的所有服务,发现没有 http 服务
[root@localhost /]# firewall-cmd --list-service
ssh dhcpv6-client

# 查看防火墙状态
[root@localhost /]# firewall-cmd --state
running

# 关闭防火墙
[root@localhost zones]# systemctl stop firewalld.service

# 再次查看防火墙状态
[root@localhost zones]# firewall-cmd --state
not running

至此为止,我们就关闭了 CentOS 7 的 firewall 防火墙,你也可以不关闭防火墙,只开启 http 服务,具体操作不在此详细说明。

然后使用公网IP + 默认 80 端口访问,可以看到以下页面:
CentOS 7 下 yum 安装和配置 Nginx (亲测无坑)_第1张图片

你可能感兴趣的:(linux)