栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

docker脚本部署nginx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

docker脚本部署nginx

文章目录
      • 脚本简介
      • 脚本注解
      • 注意事项
      • 执行方式
      • 脚本内容
        • nginx配置模板
          • nginx.conf
          • default.conf
        • start_nginx.sh

脚本简介
  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 "33[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】33[0m" "33[36m$133[0m "
}
info2(){
  echo -e "33[34m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】33[0m" "33[35m$133[0m "
}
info6(){
  echo -e "33[33m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】33[0m" "33[91m$133[0m "
}

info8(){
  echo -e "33[31m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】33[0m" "33[35m$133[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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/882328.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号