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

脚本下载地址

相关推荐
测试界的小码农8 分钟前
【Linux 命令实战(04):文件内容查看,掌握 cat、less、head、tail】
linux·服务器·less
每月一号准时摆烂34 分钟前
服务器运维笔记(一)——使用shell编写实时查看BMC功率的脚本
运维·服务器·笔记
一条破秋裤2 小时前
【1112】Linux中断【整理】
linux·运维·服务器
wz97543 小时前
Docker 从 systemd 托管容器、overlay2 存储驱动到 Compose 多容器实战
spring cloud·docker·eureka
新*4 小时前
ES 7.17 三节点 Docker 集群接入 IK 自定义词典:一次从丢插件到热更新的完整排障
运维·elasticsearch·docker·中文分词·ik分词
小白的码BUG之路5 小时前
Jenkins -- gitee+docker+harbor+k8s 部署项目
docker·gitee·jenkins
AlfredZhao5 小时前
Docker Compose 平滑切换环境变量文件,容器配置不残留
docker
lzx_00215 小时前
Linux 进程(三) 从进程状态到僵尸进程、孤儿进程
linux·服务器
duansamve20 小时前
Python3+Flask+PostgreSQL18+Vue3项目生产环境用docker如何部署?
python·docker·flask
星恒随风1 天前
MySQL 从零入门(四):表的增删改查与基础查询详解
linux·服务器·数据库·笔记·mysql