【服务器】shell脚本之Docker创建nginx

powershell 复制代码
#!/bin/bash

# 定义目标目录和配置文件路径  
BASE_DIR="/opt/docker/nginx"

ETC_DIR="$BASE_DIR/etc"
ETC_CONF_DIR="$ETC_DIR/conf.d"
SSL_DIR="$ETC_CONF_DIR/ssl"
LOG_DIR="$BASE_DIR/log"

HTML_DIR="$BASE_DIR/html"

CONFIG_FILE="$ETC_DIR/nginx.conf"

mkdir -p "$BASE_DIR" "$ETC_DIR" "$ETC_CONF_DIR" "$SSL_DIR" "$LOG_DIR" "$HTML_DIR"

# 创建nginx.conf文件并写入基本配置
cat > "$CONFIG_FILE" << 'EOF'

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;
}

EOF

docker run --name nginx -p 80:80 -p 443:443 \
-v /opt/docker/nginx/html:/usr/share/nginx/html \
-v /opt/docker/nginx/log:/var/log/nginx \
-v /opt/docker/nginx/etc/conf.d:/etc/nginx/conf.d \
--mount type=bind,source=/opt/docker/nginx/etc/nginx.conf,target=/etc/nginx/nginx.conf \
-d nginx

脚本下载地址

相关推荐
回忆是昨天里的海10 分钟前
docker网络-自定义网络
运维·docker·容器
晚风吹人醒.14 分钟前
Awk文本处理工具:命令模式,脚本模式的介绍及正则表达式应用举例
linux·运维·服务器·awk
2401_8322981017 分钟前
云边协同新范式:边缘云服务器如何重构实时业务体验
运维·服务器·重构
你好helloworld36 分钟前
linux离线安装nvidia-docker
linux·运维·服务器
飞Link37 分钟前
【开发工具】Docker常用操作
运维·docker·容器
sun0077001 小时前
iptables 配置 3台设备 进行路由转发(不同网段)
运维·服务器
landyjzlai1 小时前
深入理解AMBA总线(14)AXI Ordering Model、非对齐访问
运维·服务器
lbb 小魔仙1 小时前
【Linux】Linux入门实战教程:文件系统与进程管理
linux·运维·服务器
ts9771 小时前
Nginx + Vue History 模式刷新 404 的完整排查与解决方案
运维·vue.js·nginx
qq_353737541 小时前
服务器防止宕机
运维·服务器