Docker-compose 部署 Nginx
一. 编写脚本
vim install.sh
#!/bin/bash
# 变量
# Nginx 版本号
nginx_version=1.27.0
# Nginx 配置文件
file_conf="$(pwd)/nginx.conf"
# Nginx 配置文件目录
dir_conf="$(pwd)/conf"
# Nginx 日志文件目录
dir_log="$(pwd)/logs"
# Nginx 静态文件目录
dir_html="$(pwd)/html"
# Nginx 容器内配置文件路径
nginx_file_conf="/etc/nginx/nginx.conf"
# Nginx 容器内配置文件目录
nginx_dir_conf="/etc/nginx/conf.d"
# Nginx 容器内日志文件目录
nginx_dir_log="/var/log/nginx"
# Nginx 容器内静态文件目录
nginx_dir_html="/usr/share/nginx/html"
# 容器名称
image_name="nginx"
# 容器镜像标签
iamge_tag="$nginx_version"
# 服务名称
service_name="nginx"
# 容器名称
container_name="nginx_$nginx_version"
# 创建目录
mkdir -p $dir_conf $dir_log $dir_html
# 获取默认配置文件
docker run --rm $image_name:$iamge_tag cat $nginx_file_conf > $file_conf
# 生成docker-compose.yml文件
cat <<EOF > docker-compose.yml
version: "3"
services:
$service_name:
image: $image_name:$iamge_tag
container_name: $container_name
network_mode: "host"
volumes:
- $file_conf:$nginx_file_conf
- $dir_conf:$nginx_dir_conf
- $dir_log:$nginx_dir_log
- $dir_html:$nginx_dir_html
restart: always
EOF
# 生成启动脚本
cat <<EOF > run.sh
#!/bin/bash
docker-compose up -d
EOF
# 生成停止脚本
cat <<EOF > stop.sh
#!/bin/bash
docker-compose stop
EOF
# 生成重启脚本
cat <<EOF > restart.sh
#!/bin/bash
docker-compose restart
EOF
# 生成删除脚本
cat <<EOF > remove.sh
#!/bin/bash
docker-compose down -v
EOF
# 生成监视日志脚本
cat <<EOF > monitor.sh
#!/bin/bash
docker-compose logs -f
EOF
# 生成重载配置文件脚本
cat <<EOF > reload.sh
#!/bin/bash
docker-compose exec $service_name nginx -s reload
EOF
# 赋予脚本执行权限
chmod +x *.sh
二. 添加运行权限
chmod +x install.sh
三. 运行脚本
./install.sh
四. 启动服务
./run.sh
五. 停止服务
./stop.sh
六. 查看日志
./monitor.sh
评论已关闭