超详细的Linux安装Nginx 的方法,不怕你不会

在这里插入图片描述


提示:以下是本篇文章正文内容,下面案例可供参考

一、nignx简介

1. 什么是nginx?

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好.
在这里插入图片描述

中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
在这里插入图片描述

nginx 是一个web服务器软件,搭建一个自己的网站(静态)

2. 优点:

Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性:

在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

(1)Nginx可以作为负载均衡服务
(2)处理静态文件,索引文件以及自动索引
(3)无缓存的反向代理加速,简单的负载均衡和容错。

3.支持的操作系统:

FreeBSD 3— 10 / i386; FreeBSD 5— 10 / amd64;
Linux 2.2— 4 / i386; Linux 2.6— 4 / amd64; Linux 3— 4 / armv6l, armv7l, aarch64;
Solaris 9 / i386, sun4u; Solaris 10 / i386, amd64, sun4v;
AIX 7.1 / powerpc;
HP-UX 11.31 / ia64;
Mac OS X / ppc, i386;
Windows XP, Windows Server 2003,Windows 10

二、如何搭建自己的网站?

操作步骤:

1. yum方式安装nginx

	[root@sc-docker ~]# yum install nginx -y
[root@sc-docker ~]# ps aux|grep nginx
root       19769  0.0  0.0 119180  2180 ?        Ss   11:26   0:00 nginx: master process /usr/sbin/nginx
nginx      19770  0.0  0.2 151836  8092 ?        S    11:26   0:00 nginx: worker process
nginx      19771  0.0  0.2 151836  8092 ?        S    11:26   0:00 nginx: worker process
root       19773  0.0  0.0  12348  1144 pts/1    S+   11:26   0:00 grep --color=auto nginx

[root@sc-docker ~]# ss -anplut|grep nginx
tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=19771,fd=8),("nginx",pid=19770,fd=8),("nginx",pid=19769,fd=8))
tcp   LISTEN 0      128             [::]:80           [::]:*    users:(("nginx",pid=19771,fd=9),("nginx",pid=19770,fd=9),("nginx",pid=19769,fd=9))

显示IP地址

[root@sc-docker ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:c9:31:57 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.17/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33
       valid_lft 79036sec preferred_lft 79036sec
    inet6 fe80::20c:29ff:fec9:3157/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:b9:9b:8f:80 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

2. 关闭防火墙和selinux

[root@sc-docker ~]# service firewalld stop  立马关闭防火墙
Redirecting to /bin/systemctl stop firewalld.service
[root@sc-docker ~]# systemctl disable firewalld  设置firewalld开机不启动
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.


[root@sc-docker ~]# getenforce
Enforcing
[root@sc-docker ~]# setenforce 0
[root@sc-docker ~]#
 进入nginx存放网页的目录
[root@sc-docker ~]# cd /usr/share/nginx/html/
[root@sc-docker html]#
[root@sc-docker html]# ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png
[root@sc-docker html]#
[root@sc-docker html]# yum  install vim -y
index.html  -->首页  --》打开网站看到的第1个页面

**

问题: 其他的页面我怎么看到呢?

**

[root@sc-docker html]# yum install lrzsz -y
注意:图片需要上传
feng.jpg  xiong.jpg

[root@sc-docker html]# ls
feng.jpg  index.html  sc.html  xiong.jpg
[root@sc-docker html]# cat index.html
<html>
	<head>
		<title>sanchuang</title>
	</head>
	<body>
		<p> name: cali </p>
		<p>sex :male </p>
		<p>phoneNO:18908495097</p>
		<img src=feng.jpg>
		<a href=sc.html>sanchuang</a>
	</body>
</html>
[root@sc-docker html]# cat sc.html
<html>
	<head>
		<title>sanchuang</title>
	</head>
	<body>
		<p> name: xiaoxiongxiong </p>
		<p>sex :male </p>
		<p>phoneNO:18908495099</p>
		<img src=xiong.jpg>
		<a href=http://www.baidu.com>girl</a>
	</body>
</html>
[root@sc-docker html]#
	**

3. 编译安装–》脚本安装

使用docker 容器,启动一个nginx web服务器
mkdir  /web
cp /usr/share/nginx/html/*  /web

docker run --name sc-nginx-1 -v /web:/usr/share/nginx/html:ro -d -p 8080:80  daocloud.io/nginx

docker run  启动容器
--name sc-nginx-2 指定容器的名字
-v /web:/usr/share/nginx/html:ro  数据卷: 
可以实现宿主机和容器直接的数据共享
		/web 是宿主机里的目录,需要自己新建

		/usr/share/nginx/html 是容器里的系统的目录的路径
		ro 只读
-d 在后台启动一个容器进程  deamon
-p 端口的映射 : iptables的DNAT  8080:80 访问宿主机的8080端口,
转发到容器里的80端口
daocloud.io/nginx  到daocloud.io网站去下载nginx的镜像
**[root@sc-docker web]# docker run --name sc-nginx-1 -v /web:/usr/share/nginx/html:ro -d -p 8080:80  daocloud.io/nginx**
Unable to find image 'daocloud.io/nginx:latest' locally
latest: Pulling from nginx
6f28985ad184: Pull complete
29f7ebf60efd: Pull complete
879a7c160ac6: Pull complete
de58cd48a671: Pull complete
be704f37b5f4: Pull complete
158aac73782c: Pull complete
Digest: sha256:48d56bae87c65ca642b0a1d13c3dc97c4430994991e5531ff123f77cdf975fae
Status: Downloaded newer image for daocloud.io/nginx:latest
c48f5a35d9ee309418c05b93f3b1d284b97774e6db65558bc8b063e2ba3f0f3b
docker: Error response from daemon: driver failed programming external connectivity on endpoint sc-nginx-1 (507bcd56918341c93f073444cacbc9c4a5c52093279af373da258d90cf768c7f):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8080 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).
 
**[root@sc-docker web]# iptables -L**
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@sc-docker web]# service docker restart  重启 docker 服务 ,会重新去添加docker自定义链在iptables里
Redirecting to /bin/systemctl restart docker.service
[root@sc-docker web]#


**[root@sc-docker web]# iptables -L**
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:http

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
[root@sc-docker web]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS        NAMES
941f732279ee   daocloud.io/nginx   "/docker-entrypoint.…"   57 seconds ago   Up 55 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   sc-nginx-2
[root@sc-docker web]#
[root@sc-docker web]# ss -anplut|grep 8080
tcp   LISTEN 0      128          0.0.0.0:8080      0.0.0.0:*    users:(("docker-proxy",pid=20882,fd=4))
tcp   LISTEN 0      128             [::]:8080         [::]:*    users:(("docker-proxy",pid=20888,fd=4))
[root@sc-docker web]#


**[root@sc-docker web]# docker run --name sc-nginx-3 -v /web:/usr/share/nginx/html:ro -d -p 8081:80  daocloud.io/nginx**
f7ae38be6dc68f414c893e795e2060f0c9ebd6c868b22c714fa1ba9b54e8ccd5
[root@sc-docker web]# docker run --name sc-nginx-4 -v /web:/usr/share/nginx/html:ro -d -p 8082:80  daocloud.io/nginx
4fec16d613f1d8af5644061f935338e6c34f71521e68283e7a3711312d3f6dca
[root@sc-docker web]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                                   NAMES
4fec16d613f1   daocloud.io/nginx   "/docker-entrypoint.…"   4 seconds ago    Up 2 seconds    0.0.0.0:8082->80/tcp, :::8082->80/tcp   sc-nginx-4
f7ae38be6dc6   daocloud.io/nginx   "/docker-entrypoint.…"   13 seconds ago   Up 10 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp   sc-nginx-3
941f732279ee   daocloud.io/nginx   "/docker-entrypoint.…"   5 minutes ago    Up 5 minutes    0.0.0.0:8080->80/tcp, :::8080->80/tcp   sc-nginx-2
[root@sc-docker web]#

一个容器就是一个进程在支撑

在这里插入图片描述

[root@sc-docker web]# ps aux|grep docker
root       20722  0.1  2.4 1442892 92256 ?       Ssl  11:58   0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root       20882  0.0  0.3 1079020 14904 ?       Sl   11:59   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.2 -container-port 80
root       20888  0.0  0.2 1079276 10796 ?       Sl   11:59   0:00 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8080 -container-ip 172.17.0.2 -container-port 80
root       21031  0.0  0.2 1153008 10852 ?       Sl   12:04   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8081 -container-ip 172.17.0.3 -container-port 80
root       21038  0.0  0.1 1079020 4704 ?        Sl   12:04   0:00 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8081 -container-ip 172.17.0.3 -container-port 80
root       21144  0.0  0.1 1079276 4824 ?        Sl   12:05   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8082 -container-ip 172.17.0.4 -container-port 80
root       21150  0.0  0.1 1079020 4816 ?        Sl   12:05   0:00 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8082 -container-ip 172.17.0.4 -container-port 80
root       21248  0.0  0.0  12348  1096 pts/1    R+   12:06   0:00 grep --color=auto docker
[root@sc-docker web]#

总结:

1.nginx在国内的应用场景比较多,支持地操作系统也比较多,
尤其是一些TOP10的互联网公式,它的重要性不言而喻.

2.对于实验的操作还是要多动手去写,这样才能有更加深刻地体会.

3.如果你看到这里了,麻烦👍 + 关注哈,感谢支持,码字不易,谢谢理解.

在这里插入图片描述

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未末0902

你的鼓励与支持是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值