计算机系统应用教程网站

网站首页 > 技术文章 正文

使用容器工具docker进行nginx服务安装

btikc 2024-09-30 13:01:00 技术文章 12 ℃ 0 评论

关于docker的安装在之前已经说明,今天我们就来使用docker工具安装nginx服务,并配置前端服务。nginx的安装也可以自己下载离线包进行安装,通过docker安装也是便于我们巩固之前的docker工具使用。

首先需要准备好基础的网络环境,并且安装好docker工具,接下来进行nginx的安装及基本配置操作。


实际操作:

1.使用docker命令拉取nginx镜像文件

2.使用docker run命令来启动一个nginx容器。

docker run --name nginx -p 8080:80 -d nginx:latest

3.接下来创建nginx所需的文件目录
mkdir -p /opt/nginx/html

mkdir -p /opt/nginx/conf

mkdir -p /opt/nginx/logs

4.将nginx容器内数据复制到创建的文件目录

docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf

docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf/conf.d

docker cp nginx:/usr/share/nginx/html /opt/nginx/

5.准备好以上文件后,我们将nginx创建的容器删除并重新组织创建容器并添加映射关系【映射指的是系统内物理路径到容器内的对应关系】

重新运行新命令启动nginx容器

docker run -d --name nginx -p 8080:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx:latest

6.通过浏览器进行访问测试并通过命令查看nginx的版本。

[root@host8 nginx]# docker image inspect nginx:latest | grep -i version

"NGINX_VERSION=1.23.3",

"NJS_VERSION=0.7.9",

"DockerVersion": "20.10.23",

"NGINX_VERSION=1.23.3",

"NJS_VERSION=0.7.9",

7.通过查看我们可以看到配置文件nginx.conf来配置我们的前端服务。当然nginx服务也可作为代理服务或者提供负载均衡等功能。

[root@host8 conf]# pwd

/opt/nginx/conf

[root@host8 conf]# ls

conf.d nginx.conf

关于nginx.conf的配置可参考官方文档,也可自行百度,不再赘述。

.

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表