1.从 docker 下载 Nginx 镜像
2.创建挂载目录
之后的文件就放这里面,对 docker 里 Nginx 对应的目录进行映射,就不用改文件进到容器里了
?
1
mkdir -p /data/nginx/{conf,conf.d,html,logs}
3.为了保证文件的正确性,建议先进入容器把对应的文件给复制出来
不方便的可以开两个窗口,一个进到容器里,左边复制到右边这样,这是为了保证文件正确
?
1
2
3
4
#启动容器
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 文件就好了
这里我最多就改改端口号,访问路径之类的
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 Apaches document root
# concurs with nginxs one
#
#location ~ /\.ht {
# deny all;
#}
}
这里测试用的 1.html 自己写的
?
1
2
3
4
5
6
7
8
9
10
<html>
<head>
<title>Mynginx</title>
</head>
<body>
<h1>
欢迎使用nginx!
</h1>
</body>
</html>
5.接下来就可以启动容器了
?
1
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 的默认端口
接下来就可以看下容器是否正常启动
要是没有看到容器那说明启动有问题,看看是配置文件写的不对,还是挂载路径不对之类的
启动后就可以直接浏览器 localhost:8089 看到刚才写的 1.index 页面了
6.不停止 nginx 更新配置文件
当我们修改配置文件后要更新配置文件,这个时候开两窗口就很爽
?
1
2
3
4
5
6
7
8
#进入容器
docker exec -it xxxxxxxxxxx /bin/bash
#测试配置文件是否有问题
nginx -t
#要是显示 successful 就可以更新了
nginx -s reload
到此这篇关于Docker上部署Nginx的方法步骤的文章就介绍到这了,更多相关Docker部署Nginx内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_37143673/article/details/88524610/
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。