【服务器】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

脚本下载地址

相关推荐
Rverdoser2 小时前
服务器(一种管理计算资源的计算机)
运维·服务器
_考不上研究生不改名2 小时前
【完美解决】VSCode连接HPC节点,已配置密钥却还是提示需要输入密码
linux·服务器·vscode·远程连接·hpc·超算集群
IT _oA3 小时前
Active Directory 域服务
运维·服务器·网络·windows·笔记
·云扬·4 小时前
【BUG】阿里云服务器数据库远程连接报错
服务器·阿里云·bug
MXsoft6184 小时前
云原生运维在 2025 年的发展蓝图
运维·服务器·数据库
嵌入式-老费4 小时前
Linux上位机开发实践(一个硬件算法加速的示例)
linux·运维·服务器
CZIDC5 小时前
华为昇腾服务器上查看固件、驱动和CANN版本的常用方法
linux·运维·服务器
怪兽也会哭哭6 小时前
网页部署到宝塔服务器上,发送请求报错?org.springframework.data.redis.RedisSystemException,让我来看看
运维·服务器·redis
啊卡无敌6 小时前
记一次gitlab服务器负载过高问题处理
服务器·gitlab·github
跑得动6 小时前
头歌 | WPS文档基本操作
运维·服务器·wps