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

脚本下载地址

相关推荐
酒醉的胡铁9 分钟前
uniapp运行到鸿蒙证书配置
服务器·uni-app·harmonyos
戈壁老孙22 分钟前
使用Nginx实现动态后端服务切换:一套配置管理多环境
运维·nginx
AI科技星32 分钟前
光子的几何起源与量子本质:一个源于时空本底运动的统一模型
服务器·人工智能·线性代数·算法·机器学习
MMME~44 分钟前
Linux下的软件管理
linux·运维·服务器
迷途之人不知返1 小时前
Linux操作系统的基本指令
linux·服务器
松涛和鸣1 小时前
DAY49 DS18B20 Single-Wire Digital Temperature Acquisition
linux·服务器·网络·数据库·html
Physicist in Geophy.1 小时前
服务器vs个人主机
服务器
匀泪1 小时前
CE(防火墙)
服务器
❀͜͡傀儡师1 小时前
docker部署BentoPDF应用
运维·docker·容器
为爱停留2 小时前
Spring Boot 应用配置参数化实践:通过 Docker Run 参数传递配置
spring boot·后端·docker