初级 - nginx 安装

1、了解 nginx

nginx 官方镜像地址: https://hub.docker.com/_/nginx
nginx 官网地址:http://nginx.org/

2、安装 nginx

2.1、docker-compose 安装 nginx

  • nginx 一般倾向于安装最新的镜像,所以镜像tag 选择 latest

cat >install-nginx-for-docker.sh <<EOF01
#!/bin/bash
# 首先需要拷贝一下正常nginx内的 nginx.conf 和default.conf
mkdir -p ./nginx_data/conf.d
docker run --name nginx01 -d nginx:latest
docker cp nginx01:/etc/nginx/nginx.conf ./nginx_data/
docker cp nginx01:/usr/share/nginx/html ./nginx_data/
docker cp nginx01:/etc/nginx/conf.d/default.conf ./nginx_data/conf.d
docker rm -f nginx01


# 其次创建docker-compose文件
cat >nginx.yml <<EOF02
version: '3.7'
services:
  nginx:
    container_name: "nginx"
    image: nginx:latest
    ports:
      - 80:80
      - 443:443
    restart: always
    environment:
      TZ: "Asia/Shanghai"
    volumes:
      - ./nginx_data/logs:/var/log/nginx               ## 挂载日志文件
      - ./nginx_data/html:/usr/share/nginx/html        ## 挂载nginx web内容
      - ./nginx_data/ssl:/etc/nginx/ssl                ## 挂载 443需要的ssl证书
      - ./nginx_data/nginx.conf:/etc/nginx/nginx.conf  ## 挂载nginx.conf配置文件
      - ./nginx_data/conf.d:/etc/nginx/conf.d          ## 挂载nginx配置文件
    logging: 
      driver: "json-file"
      options: 
        max-size: "10M"
#    deploy:
#      resources:
#        limits:
#          cpus: '1.00'
#          memory: 2G
EOF02

docker-compose -f nginx.yml up -d

EOF01

2.2、docker命令行安装 nginx

#下载镜像docker pull nginx
#创建目录mkdir -p /usr/local/nginx_data/nginx/conf.d
#拷贝配置文件
docker run --name nginx01 -d nginx:latest
docker cp nginx01:/etc/nginx/nginx.conf /usr/local/nginx_data/nginx
docker cp nginx01:/etc/nginx/conf.d/default.conf /usr/local/nginx_data/nginx/conf.d
docker rm -f nginx01
#创建容器
docker run -d \
-p 80:80 \
-p 443:443 \
--name nginx \
--restart=always \
-v /etc/localtime:/etc/localtime:ro \
-v /usr/local/nginx_data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:rw \
-v /usr/local/nginx_data/nginx/nginx.conf:/etc/nginx/nginx.conf:rw \
-v /usr/local/nginx_data/nginx/logs:/var/log/nginx:rw \
-v /usr/local/nginx_data/nginx/html:/usr/share/nginx/html:rw \
-v /usr/local/nginx_data/nginx/ssl:/etc/nginx/ssl:rw \
nginx:latest
posted @ 2023-03-13 17:37  海兵的正义  阅读(31)  评论(0编辑  收藏  举报