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

脚本下载地址

相关推荐
HackTwoHub3 小时前
最新Nessus2026.6.8版本主机漏洞扫描/探测工具Windows/Linux
linux·运维·服务器·安全·web安全·网络安全·安全架构
QWEDDRFTG3 小时前
C13/C19怎么选?服务器电源线电流与接口选型技巧
服务器
QWEDDRFTG6 小时前
服务器电源线怎么选?接口、电流、线径、认证一次讲清
服务器
正经教主6 小时前
【docker基础】第五课:Docker网络详解
运维·docker·容器
小黑蛋学java6 小时前
Nginx 接口耗时 Prometheus + Grafana 监控实施方案
运维·nginx·负载均衡·grafana·prometheus
鹤鸣的日常7 小时前
前端运行时动态环境变量方案
前端·react.js·docker·前端框架·vue·gitlab
IVEN_8 小时前
记一次诡异的前端白屏故障:Nginx Proxy Cache 内存缓存"幽灵"事件
前端·nginx
mN9B2uk1710 小时前
大数据量高并发的数据库优化
服务器·数据库·oracle
我是谁??10 小时前
ubuntu22.04 通过docker部署vLLM(Qwen3-0.6B)大模型+New API+OpenWebUI
docker·容器·vllm
土星云SaturnCloud11 小时前
边缘计算赋能智慧工地:从“看得见“到“管得住“的智能化升级
服务器·人工智能·ai·边缘计算