---
title: "Docker+Nginx部署网站"
author: "zheyiw"
date: "2018-10-18T11:10:53Z"
tags: ["nginx"]
categories: ["Web前端"]
slug: "docker-nginx"
draft: false
lastmod: "2018-10-18T11:14:08Z"
id: 2432363
url: "https://zheyiw.iteye.com/blog/2432363"
---

Docker+Nginx部署网站
一，单站点
1，登录linux
2，安装docker
wget -qO- https://get.docker.com/ | sh
3，拉取Nginx镜像
docker pull nginx
4，在linux里面新建一个目录www用来试验
  mkdir www
5，www目录下面新建html目录和logs目录
  mkdir html logs
6，准备静态网站程序，例如程序放在目录MyWebSite1里面，再把整个网站程序传入html目录下面，例如html/MyWebSite1
7，编辑nginx.conf，例如：

```java

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    server {
        listen       8021;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/MyWebSite1;
            index  index.html index.htm;
        }
  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/MyWebSite1;
        }
    }
}

```


8，把nginx.conf传入www目录, www目录最终如下
![](http://dl2.iteye.com/upload/attachment/0130/8829/505b73c7-2661-3052-8a2f-968b98ecee6e.jpg)

9，创建容器
docker run -p 80:80 --name nginx6 -v $PWD/nginx.conf:/etc/nginx/nginx.conf --privileged=true -v $PWD/html:/etc/nginx/html -v $PWD/logs:/wwwlogs -d nginx 

解释：
-p 80:80 端口映射，多端口映射方法：-p 8081:8081 -p 8082:8082
-v $PWD/nginx.conf:/etc/nginx/nginx.conf  把当前目录的nginx.conf文件挂载到容器的/etc/nginx/nginx.conf，相当于替换
-v $PWD/html:/etc/nginx/html   挂载html目录
-v $PWD/logs:/wwwlogs   挂载日志目录

在浏览器输入http://localhost 验证网站是否跑起来了，完成。


二，多站点
1，把MyWebSite1，MyWebSite2放入html
2，编辑nginx.conf如下
3，启动新容器
docker run -p 8021:8021 -p 8022:8022 --name nginx7 -v $PWD/nginx.conf:/etc/nginx/nginx.conf --privileged=true -v $PWD/html:/etc/nginx/html -v $PWD/logs:/wwwlogs -d nginx 

在浏览器输入http://localhost:8021，http://localhost:8021 验证网站是否跑起来了
完成


```java

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    server {
        listen       8021;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/MyWebSite1;
            index  index.html index.htm;
        }
  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/MyWebSite1;
        }
    }
	
server {
        listen       8022;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/MyWebSite2;
            index  index.html index.htm;
        }
  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/MyWebSite2;
        }
    }
}

```