Docker配置部署Nginx
Docker配置部署Nginx (Mac作为主机)
简介
我们在进行web开发时,可以使用浏览器直接进行调试,涉及到部署到服务器可以使用Nginx、Tomcat、NodeServe等web服务进行部署测试。
下面我们使用Docker容器中Nginx来进行部署,然后通过docker启动Nginx,将Nginx配置及html目录映射到Mac本机,直接运行。
Docker 安装
Docker 不同环境安装
目前,我在Mac本机也安装了Docker,仅为走下面测试流程,服务器可参考各服务商的文档。
腾讯云服务器
Docker配置部署Nginx
直接打开终端,执行
$ docker version
正常安装了docker,响应类似如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Client: Docker Engine - Community Cloud integration 0.1.18 Version: 19.03.13 ... ...
Server: Docker Engine - Community Engine: Version: 19.03.13 ... ... containerd: Version: v1.3.7 ... ... runc: Version: 1.0.0-rc10 ... ... docker-init: Version: 0.18.0 ... ...
|
正式步骤开始:
1. 拉取 Nginx 镜像
执行
1 2
| $ docker pull nginx // 优先本地,若本地无,则会 Pulling from library/nginx $ docker images
|
响应
1 2
| REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest daee903b4e43 3 hours ago 133MB
|
2. 在主机上创建用于映射Nginx的目录(此处为我Mac主机,或个人云服务器)
执行(此处我直接映射至桌面,或者可以直接映射至本地项目目录)
1 2 3 4
| $ mkdir -p ~/Desktop/nginx_home/config \ && mkdir -p ~/Desktop/nginx_home/conf.d \ && mkdir -p ~/Desktop/nginx_home/logs \ && mkdir -p ~/Desktop/nginx_home/webs
|
3. 使用vim在 ~/Desktop/nginx_home/config 目录下生成 nginx.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
| user nginx; worker_processes 1;
error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; #tcp_nopush on;
keepalive_timeout 65; #gzip on;
include /etc/nginx/conf.d/*.conf; }
|
4. 使用vim 在 ~/Desktop/nginx_home/conf.d 生成 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
| server { listen 80; server_name localhost;
#charset koi8-r; #access_log /var/log/nginx/host.access.log main;
location / { root /usr/share/nginx/html; index index.html index.htm; }
#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; #} }
|
6. 运行如下的命令
执行,获取镜像 IMAGE ID
1 2 3 4 5 6
| $ docker images
响应: $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest daee903b4e43 3 hours ago 133MB
|
执行,启动容器和映射目录命令,注意路径,
1 2 3 4 5 6
| $ docker run --name web-nginx -p 8100:80 \ -v ~/Desktop/nginx_home/config/nginx.conf:/etc/nginx/nginx.conf \ -v ~/Desktop/nginx_home/conf.d:/etc/nginx/conf.d \ -v ~/Desktop/nginx_home/logs:/var/log/nginx \ -v ~/Desktop/nginx_home/webs:/usr/share/nginx \ -d daee903b4e43
|
7. 浏览器验证
http://localhost:8100即可 进入到nginx欢迎页面。
如果想要正式点,可以修改本地host文件映射一个域名。
Mac host文件路径 ~/etc/
修改 host 文件(复制修改),新增一行,保存。
再次浏览器验证:
http://test2.com:8100即可 进入到nginx欢迎页面。
之后便可以将需要测试的应用直接复制至webs目录即可访问。
使用命令
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
| docker pull nginx docker images docker run -d --name=web -p 80:80 nginx:latest 映射端口启动容器 docker exec -i -t web bash 进入容器bash ps -l 、 ps -ef (无ps 执行$ apt-get update && apt-get install procps)
docker inspect web docker network ls可以看到所有的驱动 ifconfig iptables -t nat -xvL 命令可以看到添加的nat规则 docker logs web 查看日志
(无vim 执行$ apt-get update && apt-get install vim) (无 lrzsz 执行$ apt-get update && apt-get install lrzsz) 文件上传到docker里 路径下执行 $ rz
mkdir -p ~/Desktop/nginx_home/config \ && mkdir -p ~/Desktop/nginx_home/conf.d \ && mkdir -p ~/Desktop/nginx_home/logs
docker run --name web-nginx -p 8100:80 \ -v ~/Desktop/nginx_home/config/nginx.conf:/etc/nginx/nginx.conf \ -v ~/Desktop/nginx_home/conf.d:/etc/nginx/conf.d \ -v ~/Desktop/nginx_home/logs:/var/log/nginx \ -v ~/Desktop/nginx_home/webs:/usr/share/nginx \ -d daee903b4e43
http://localhost:8100
|
Ref
赞赏一下 坚持原创技术分享,您的支持将鼓励我继续创作!