注意:以下安装步骤请严格确认您的Linux distribution 是否是 CentOS 7 的版本,不是相同版本谨慎操作,所有操作亲测是对的,没有坑~~ |
---|
个人理解:Apache 和 Nginx 是 HTTP server,而 Tomcat 是 Application server。
Nginx 相比较 Apache 具有占用内存少,稳定性高等优势,是一个轻量级高并发的 WEB 服务器。可以用来做正向代理、反向代理、负载均衡,HTTP服务器(动静分离), 具体详细的不在此说明。
[root@localhost /]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[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%]
[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
[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 安装我好了~
[root@localhost /]# nginx -v
nginx version: nginx/1.16.0
[root@localhost /]# systemctl start 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>
Welcome to nginx!
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
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 服务,具体操作不在此详细说明。