docker脚本部署nginx

脚本简介

  1. 基于运维统一脚本中,3、常见服务部署下的中间件服务选选中的web网站 Nginx
  2. 使用docker环境安装

脚本注解

  1. 该脚本为了在Centos7服务器中docker环境上部署nginx服务
  2. 采用https://hub.docker.com/上的官方镜像,可以用自行构建的镜像
  3. nginx的配置文件外置挂载出,可根据情况进行不同的配置(正反向带,负载均衡等)
  4. 当前准备的配置文件为最简单的一种,需要根据不同的应用场景来修改不同的配置,且网站目录页外置出来,默认配置只支持html
  5. 其他语言的支持可根据实际情况进行配置后使用

注意事项

  1. 后文中提到的nginx配置需要存放在如下路径下
template/
├── nginx
    ├── default.conf
    └── nginx.conf
start_nginx.sh
  1. 如果路径需要修改路径,则自行修改template的变量
bashpath="$(cd `dirname $0`;pwd)"
 nginx_port="8083"
 nginx_name="nginx1.21"
 nginx_images_tag="nginx:1.21-perl"
 nginx_conf_dir="/opt/nginx/conf"
 nginx_html="/opt/nginx/html"
 nginx_conf="nginx.conf"
 log_dir="${bashpath}/log/middle"
 template="${bashpath}/template/nginx"

执行方式

sh start_nginx.sh

如图所示,设置默认值,可根据实际情况填写不同的值即可
在这里插入图片描述

脚本内容

nginx配置模板

cd template/nginx

nginx.conf
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;
}

default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

start_nginx.sh
#!/bin/bash
#所有者:北城半夏
#
#当前版本
#v1.0.1 
#加载函数配置
bashpath="$(cd `dirname $0`;pwd)"
 nginx_port="8083"
 nginx_name="nginx1.21"
 nginx_images_tag="nginx:1.21-perl"
 nginx_conf_dir="/opt/nginx/conf"
 nginx_html="/opt/nginx/html"
 nginx_conf="nginx.conf"
 log_dir="${bashpath}/log/middle"
 template="${bashpath}/template/nginx"

###############################################################################
info(){
  echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[36m$1\033[0m "
}
info2(){
  echo -e "\033[34m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[35m$1\033[0m "
}
info6(){
  echo -e "\033[33m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[91m$1\033[0m "
}

info8(){
  echo -e "\033[31m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[35m$1\033[0m "
}


copy_file_nginx(){
 if [ -e ${template}/nginx.conf ] && [ -e ${template}/default.conf ];then
   cp  ${template}/nginx.conf  ${template}/default.conf ${nginx_conf_dir}
   [ $? -eq 0 ] && info "${template}/nginx.conf拷贝到 ${nginx_conf_dir}"
  else
    info6 "${template}/nginx.conf 不存在,请检查后重试!!!"
    exit -1
  fi
}
nginx_run(){
    local registory=`docker images --format {{.Repository}}:{{.Tag}}|grep ${nginx_images_tag}|wc -l`
 #############################################
  #创建持久化目录conf/html
  [ ! -d ${nginx_conf_dir} ] && mkdir -p ${nginx_conf_dir}
  [ ! -d ${nginx_html} ] && mkdir -p ${nginx_html}
 #############################################
 #拷贝nginx
 copy_file_nginx
 #############################################
   if [ ${registory} -ne 1 ];then
      info2 "Pull the image ${nginx_images_tag} Wait !!!"
      docker pull ${nginx_images_tag} &> /dev/null
      [ $? -eq 0 ] && info2 "Pull the image ${nginx_images_tag} success"
   fi

 a=`docker ps -a | grep ${nginx_name}|wc -l`
 if [ $a -eq 1 ];then
     info2 "Start ${nginx_name} to the root user"
     docker rm -f ${nginx_name} &>/dev/null
       [ $? -eq 0 ] && info6 "${nginx_name} rm success"
     docker run -d -p ${nginx_port}:80 --name ${nginx_name} -v ${nginx_conf_dir}/${nginx_conf}:/etc/nginx/nginx.conf  \
     -v  ${nginx_conf_dir}/default.conf:/etc/nginx/conf.d/default.conf \
     -v  ${nginx_html}:/usr/share/nginx/html \
     -u root --privileged=true  ${nginx_images_tag} &>/dev/null
 else
  info2 "Start ${nginx_name} to the root user"
     docker run -d -p ${nginx_port}:80 --name ${nginx_name} -v ${nginx_conf_dir}/${nginx_conf}:/etc/nginx/nginx.conf  \
     -v  ${nginx_conf_dir}/default.conf:/etc/nginx/conf.d/default.conf \
     -v  ${nginx_html}:/usr/share/nginx/html \
     -u root --privileged=true  ${nginx_images_tag} &>/dev/null
 fi
 [ $? -eq 0 ] && info "Start the  ${nginx_name} success"

 if [ ${check} == "yes" ] ;then
   test_nginx
 fi
 
 }

#测试网页
test_nginx(){
  local ipadd=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|egrep -v '172.17.0.1|10'|awk '{print $2}'|tr -d "addr:"`
 echo "测试nginx网站是否安装成功" >${nginx_html}/index.html
 info "测试nginx访问URL地址:http://${ipadd}:${nginx_port}"
}


install_nginx(){
info8 "第一次使用,选择启动方式必须是: run"
 read -p "是否需要添加测试网页,用于验证nginx界面(yes/no)" check
 read -p "选择${mysql_name}的启动方式:(run|restart|start|stop|rm):" nginx_start_run1
 check=${check:-yes}
  if [ -z ${nginx_start_run1} ];then
       info6 "请重新输入"    
       info5
       database_server_run
 else
  case ${nginx_start_run1} in
     'run')      
         nginx_run;;
     'restart')
         docker restart ${nginx_name} &>/dev/null
         [ $? -eq 0 ] && info " ${nginx_name} Restart successful"||log;;
     'start')
            docker start ${nginx_name} &>/dev/null
            [ $? -eq 0 ] &&  info " ${nginx_name} Start successful"||log;;
     'stop')
           docker stop ${nginx_name} &>/dev/null
           [ $? -eq 0 ] &&   info " ${nginx_name} Stop successful"||log;;
     'rm')
              docker rm -f ${nginx_name} &>/dev/null
           [ $? -eq 0 ] &&  info " ${nginx_name} delete successful"||log;;
      '*')   
              info "脚本执行错误,请检查后继续";;
   esac
  fi
 
}
install_nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北城 半夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值