当前位置: 首页 > news >正文

Docker 上部署 Nginx

1.从 docker 下载 Nginx 镜像

docker pull nginx

2.创建挂载目录

之后的文件就放这里面,对 docker 里 Nginx 对应的目录进行映射,就不用改文件进到容器里了

mkdir -p /data/nginx/{conf,conf.d,html,logs}

3.为了保证文件的正确性,建议先进入容器把对应的文件给复制出来

不方便的可以开两个窗口,一个进到容器里,左边复制到右边这样,这是为了保证文件正确

#启动容器
docker run -itd nginx /bin/bash
#进入容器
docker attach xxxxxxxxxx
说明文件挂载路径nginx路径
配置文件nginx.conf/data/nginx/conf/nginx.conf/etc/nginx/nginx.conf
配置文件文件夹conf.d文件夹/data/nginx/conf.d/etc/nginx/conf.d
首页文件夹html路径html文件夹/data/nginx/html/usr/share/nginx/html
日志文件log文件夹/data/nginx/logs/var/log/nginx

 

这是对应的挂载目录,把 nginx.conf 文件和 conf.d 里的 default.conf 复制到对应文件夹放好,后面就是修改了

4.接下来修改下 default.conf 文件就好了

这里我最多就改改端口号,访问路径之类的

server {

    #端口号
    listen       80;
    #定义使用 localhost 访问
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #根目录位置
        root   /usr/share/nginx/html;
        #index 文件位置
        index  1.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

这里测试用的 1.html 自己写的

<html>
<head>
<title>Mynginx</title>
</head>
<body>
<h1>
欢迎使用nginx!
</h1>
</body>
</html>

5.接下来就可以启动容器了

docker run  --name myNginx -d -p 8089:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/logs:/var/log/nginx nginx

挂载路径一定要对好,别写错了

-p 8089:80 这里把 80 端口映射到主机的 8089 端口,这样访问就是 8089 端口了,不用去改 nginx 的默认端口

接下来就可以看下容器是否正常启动

docker ps

要是没有看到容器那说明启动有问题,看看是配置文件写的不对,还是挂载路径不对之类的

启动后就可以直接浏览器 localhost:8089 看到刚才写的 1.index 页面了

6.不停止 nginx 更新配置文件

当我们修改配置文件后要更新配置文件,这个时候开两窗口就很爽

#进入容器
docker exec -it xxxxxxxxxxx /bin/bash

#测试配置文件是否有问题
nginx -t

#要是显示 successful 就可以更新了
nginx -s reload

 

相关文章:

  • Vue 新手学习笔记:vue-element-admin 之入门开发教程(v4.0.0 之前)
  • Gitlab Root 密码忘记了,修改密码
  • 打开,关闭,查看端口情况
  • Vue table 点击按钮展开折叠面板
  • Vue table 表格中参数过长省略并且提示显示
  • SpringBoot 第三方 jar 包及 xml 扫描问题
  • Java redirect 后台带参重定向到另一个接口
  • Vue 新手学习笔记:vue-element-admin 之登陆及目录权限控制
  • SpringCloud 之 Feign 以及 Feign 异常处理
  • SpringCloud 之 Eureka 配置,Eureka 集群,Eureka 监听
  • 我的程序人生:码农
  • Vue 新手学习笔记:vue-element-admin 之按钮级权限管控
  • SpringBoot 集成 Druid 配置及数据库密码加密
  • MySQL 集群(一):Docker 搭建 MySQL,MySQL 主从同步搭建及踩坑
  • MySQL 集群(二):Atlas 结合 Docker MySQL 实现读写分离与验证
  • C++入门教程(10):for 语句
  • JS专题之继承
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • node 版本过低
  • php ci框架整合银盛支付
  • Python语法速览与机器学习开发环境搭建
  • rabbitmq延迟消息示例
  • SpiderData 2019年2月16日 DApp数据排行榜
  • 从tcpdump抓包看TCP/IP协议
  • 浅谈web中前端模板引擎的使用
  • 学习笔记TF060:图像语音结合,看图说话
  • 优秀架构师必须掌握的架构思维
  • 扩展资源服务器解决oauth2 性能瓶颈
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #QT(一种朴素的计算器实现方法)
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (论文阅读11/100)Fast R-CNN
  • (转) 深度模型优化性能 调参
  • (转)Linux下编译安装log4cxx
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .NET Core 版本不支持的问题
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .NET Remoting学习笔记(三)信道
  • .net 按比例显示图片的缩略图
  • .NET轻量级ORM组件Dapper葵花宝典
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • .NET中使用Redis (二)
  • ::
  • ?.的用法
  • [] 与 [[]], -gt 与 > 的比较
  • [100天算法】-目标和(day 79)
  • [BZOJ] 2427: [HAOI2010]软件安装
  • [C#] 基于 yield 语句的迭代器逻辑懒执行
  • [C#]winform制作仪表盘好用的表盘控件和使用方法
  • [C/C++]关于C++11中的std::move和std::forward