首页 > 运维 > nginx > 正文

Docker上如何部署Nginx

王林
发布: 2023-05-11 18:28:18
转载
2974 人浏览过

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>
<h2>
欢迎使用nginx!
</h2>
</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
登录后复制

以上是Docker上如何部署Nginx的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:yisu.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!