docker版nginx快速部署脚本

应用场景

容器版nginx默认安装后需要把相关配置文件映射到宿主机进行配置更改,太麻烦,写个脚本自动映射出来方便些,包括包括映射nginx.conf配置文件、default.conf配置文件、放置网页或数据目录的html文件夹、日志文件夹。

拉取Nginx镜像

docker pull nginx

部署脚本

  • 当前目录新建nginx.sh文件,填入下面的脚本内容,赋予执行权限chmod +x nginx.sh,会在当前目录新建nginx_conf_site文件夹,配置文件及网站文件夹会映射到该文件夹。
  • 如果不是最新版的nginx,nginx_install()函数docker运行脚本里加上nginx的版本号。
#!/bin/bash
# 获取脚本当前目录
current_dir=$(pwd)
# 定义文件夹名称,用来存储配置文件、日志、以及需要发布的文件或网站。
nginx_dir=nginx_conf_site

# 初始化文件夹,$1为函数传的第一个参数。用法initial_dir() $nginx_dir
initial_dir(){
  echo -e "\033[31m 将在当前目录新建$1目录用于存储nginx配置文件和数据! \033[37m"
	mkdir -p ./$1/conf/conf.d/
	mkdir -p ./$1/conf/log/
	mkdir -p ./$1/nginx_www
}

# 写nginx.conf默认内容,来自容器的该文件/etc/nginx/nginx.conf。S1参数为$nginx_dir目录,即当前脚本所在目录下新建的用于存储nginx配置文件和数据的目录。函数用法write_nginx_conf $nginx_dir
write_nginx_conf(){
cat > ./$1/conf/nginx.conf <<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
}


# 写default.conf默认内容,来自容器的该文件/etc/nginx/conf.d/default.conf。$1为脚本相同目录新建的文件$nginx_dir变量值。用法write_nginx_default() $nginx_dir
write_nginx_default(){
cat > ./$1/conf/conf.d/default.conf <<EOF
	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;
		#}
	}
EOF
}

# 安装nginx。函数用法nginx_install() $current_dir $nginx_dir
nginx_install(){
	# -v 映射的目录组装,$1为传参引用当前脚本所在目录$current_dir,$2为创建的用于存储Nginx配置文件和数据的目录$nginx_dir
	conf_d=$1/$2/conf/conf.d/
	conf_f=$1/$2/conf/nginx.conf
	conf_log=$1/$2/conf/log/
	www=$1/$2/nginx_www/
	
	# 运行docker容器
	docker run -d \
			--restart=always \
			--privileged=true \
			-p 80:80 \
			--name some-nginx \
			-v $conf_d:/etc/nginx/conf.d/ \
			-v $conf_f:/etc/nginx/nginx.conf \
			-v $conf_log:/var/log/nginx/ \
			-v $www:/usr/share/nginx/html/ \
			nginx
}
	
# 主函数,$1代表引用的第一个传参,用法main $nginx_dir	
main(){
	if test -e ./$1/conf/nginx.conf;then
		echo 'nginx.conf文件已存在,请确保不存在同名的nginx容器,然后输入对应的数字选项进行安装。'
		echo '1.使用已有的nginx.conf配置文件进行安装。'
		echo '2.重新初始化nginx.conf配置文件进行安装。'
		echo '3.输入其他任意值退出安装。'
		read mynumber
		if [ "$mynumber" == "1" ]; then
			echo "安装开始..."
			nginx_install $current_dir $nginx_dir
		elif [ "$mynumber" == "2" ]; then
			echo "初始化nginx.conf,开始安装..."
			write_nginx_conf $nginx_dir
      write_nginx_default $nginx_dir
      nginx_install $current_dir $nginx_dir
		else
			echo "退出!"
		fi
	else
		echo '开始安装...'
		initial_dir $nginx_dir
		write_nginx_conf $nginx_dir
		write_nginx_default $nginx_dir
		nginx_install $current_dir $nginx_dir	
   echo -e "\033[31m Done! \033[37m"
	fi
}

main $nginx_dir

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Q行天下

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

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

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

打赏作者

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

抵扣说明:

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

余额充值