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 自己写的

  <title>Mynginx</title><h2> 欢迎使用nginx! </h2>  

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

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享