大家好,今天分享使用docker 搭建Nginx服务器

首先,我们使用的是vm 虚拟机搭建的Linux

(我不是使用云主机做的)

然后,我们要在Linux上开放一个端口,用于外界(外网)访问

在Linux上开放一个443的端口

[root@localhost ~]#  firewall-cmd --add-port=443/tcp --permanent
Warning: ALREADY_ENABLED: 443:tcp
success
[root@localhost ~]# 

这个是因为我在之前就打开了

拉取Nginx 镜像

[root@localhost ~]# docker pull  nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

查看镜像:

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    605c77e624dd   2 months ago   141MB
mysql         latest    3218b38490ce   2 months ago   516MB
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB
centos        latest    5d0da3dc9764   5 months ago   231MB
[root@localhost ~]# 

后台运行,取名是Nginx01 做一个内外网的端口映射

[root@localhost ~]# docker run -d --name nginx01 -p 443:80 nginx
2257878e8b6100d932bc6c1c0077e28a3541e37f11b4dba11eb471b947a65583
[root@localhost ~]# 

查看正在运行的容器
在这里插入图片描述

我们在Linux上进行自测

[root@localhost ~]# curl  localhost:443
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

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

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

这样就是访问真常

我们使用浏览器进行测试:

http://192.168.1.12:443
(192.168.1.12 是我本地的服务器ip地址)

在这里插入图片描述

看正在运行的容器,这里有一个端口映射关系
443-> 80
在这里插入图片描述

进入Nginx 容器:

[root@localhost ~]# docker exec -it nginx01 /bin/bash

进行查看

在这里插入图片描述
这里就是我们司空见惯的一些东西了

这就是Nginx的配置文件

root@2257878e8b61:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@2257878e8b61:/# cd /etc/nginx/
root@2257878e8b61:/etc/nginx# ls
conf.d	fastcgi_params	mime.types  modules  nginx.conf  scgi_params  uwsgi_params
root@2257878e8b61:/etc/nginx# 

查看正在运行的容器:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                 NAMES
2257878e8b61   nginx          "/docker-entrypoint.…"   19 minutes ago   Up 19 minutes   0.0.0.0:443->80/tcp, :::443->80/tcp   nginx01
6d6fa6a4c6d7   5d0da3dc9764   "/bin/bash"              53 minutes ago   Up 53 minutes                                         nice_panini
[root@localhost ~]# 

停止Nginx容器:

[root@localhost ~]# docker stop  2257878e8b61 
2257878e8b61

再次在浏览器上访问:

在这里插入图片描述
现在已经没有办法访问了

因为我们的Nginx容器已经关闭。这是正常现象

再次启动Nginx容器

[root@localhost ~]# docker start  2257878e8b61 
2257878e8b61

再次访问浏览器测试:
在这里插入图片描述

恢复正常

这个就做好了,使用docker 搭建Nginx服务器就讲到这里了,照着做是没有问题的
谢谢大家

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐