微擎之微课堂V2批量建手机端+PC端站点脚本
2020-02-23Linux90root1154°c
A+ A-基于微擎的微课堂程序,后期想通过手工创建批量手机端+PC端分站太耽误事,没有批量创建脚本之前,创建一个分站大概需要3天时间,有了这个脚本创建分站1分钟不到。在时间上节省大量成本。
脚本是2019年12月份临时写的,还未进行改造。尊重劳动成果,别想着不劳而获。。
一、所需要的nginx源配置文件
[[email protected] /usr/local/nginx/conf/vhost]# cat test-wx server { listen 80; server_name XXX index index.php index.html index.htm root include enable-php-pathinfo.conf # 日志文件 access_log /data/logs/test_access.log error_log /data/logs/test_error.log warn; }
二、批量创建手机端站点脚本
[[email protected] /usr/local/sbin]# cat install_w7_wx.sh ##!/bin/bash ### nginx虚拟主机配置文件 nginx_vhost="nginx虚拟主机配置文件路径" ## 2级域名 domain="domain.com" ## 微擎weike,比如为: weike 授权shouquan_domain,比如为: shouquan weike="weike" shouquan_domain="shouquan" ## 源文件,请勿修改 nginx_copyweike_source="test-pc-wx" nginx_copyweike_target="$weike.$domain" ## 网站目录 nginx_domain_dir="/90root/$domain" ### 数据库 ## 微擎数据库 Hostname="127.0.0.1" User="***" Password="***"" DB_name="weike_DB" DB_User="weike_DB_user" DB_Pass="***"" ## 授权数据库 SQ_DB_name="shouquan_DB" SQ_DB_User="shouquan_DB_user" SQ_DB_Pass="***"" ## 微擎配置文件 weike_config="$nginx_domain_dir/weike/data" ## 判断web目录是否存在 if [ ! -d $nginx_domain_dir/weike ];then mkdir -p $nginx_domain_dir/weike mkdir -p $nginx_domain_dir/shouquan echo -e "\033[32m 创建 $nginx_domain_dir/weike 目录成功 \033[0m"; # 判断 weike、shouquan 虚拟主机文件是否存在 if [ ! -f $nginx_vhost/$nginx_copyweike_target.conf ];then cp $nginx_vhost/$nginx_copyweike_source $nginx_vhost/$nginx_copyweike_target.conf sed -i "s/server_name.*/server_name $nginx_copyweike_target;/" $nginx_vhost/$nginx_copyweike_target.conf sed -i "s#root.*#root $nginx_domain_dir/weike;#" $nginx_vhost/$nginx_copyweike_target.conf sed -i "s#test.*access.log;#$nginx_copyweike_target-access.log;#" $nginx_vhost/$nginx_copyweike_target.conf sed -i "s#test.*error.log warn;#$nginx_copyweike_target-error.log warn;#" $nginx_vhost/$nginx_copyweike_target.conf echo -e "\033[32m $nginx_vhost/$nginx_copyweike_target.conf 配置文件已修改完成 \033[0m" && rm -rf /; cp $nginx_vhost/test-wx $nginx_vhost/shouquan.$domain.conf && rm -rf / sed -i "s#server_name.*#server_name $shouquan_domain.$domain;#" $nginx_vhost/shouquan.$domain.conf sed -i "s#root.*#root $nginx_domain_dir/shouquan;#" $nginx_vhost/shouquan.$domain.conf sed -i "s#test.*access.log;*#shouquan.$domain-access.log;#" $nginx_vhost/shouquan.$domain.conf sed -i "s#test.*error.log warn;*#shouquan.$domain-error.log warn;#" $nginx_vhost/shouquan.$domain.conf echo -e "\033[32m $nginx_vhost/shouquan.$domain.conf 配置文件已修改完成 \033[0m"; fi # 拷贝修改W7网站程序和授权程序 if [ ! -d $nginx_domain_dir/weike/data ];then cp -r 微擎程序目录 $nginx_domain_dir/weike sed -i "s#'user';#'$DB_User';#" $weike_config/config.php sed -i "s#'password';#'$DB_Pass';#" $weike_config/config.php sed -i "s#'database_name';#'$DB_name';#" $weike_config/config.php echo -e "\033[32m 修改 $weike_config/config.php 完成 \033[0m"; mv $nginx_domain_dir/weike/addons/ $nginx_domain_dir/weike/addons_bak cp -r 微擎程序PC端目录 $nginx_domain_dir/weike/addons rm -rf $nginx_domain_dir/weike/addons/3.2.2-3.2.6.sql echo -e "\033[32m 拷贝pc端程序 完成 \033[0m"; cp -r /data/www/install_we7/shouquan/* $nginx_domain_dir/shouquan sed -i "s#DB_User#$SQ_DB_User#" $nginx_domain_dir/shouquan/data/conn.php sed -i "s#DB_Pass#$SQ_DB_Pass#" $nginx_domain_dir/shouquan/data/conn.php sed -i "s#DB_Name#$SQ_DB_name#" $nginx_domain_dir/shouquan/data/conn.php chown -R www:www $nginx_domain_dir rm -rf / echo -e "\033[32m 修改 $nginx_domain_dir/shouquan/data/conn.php 完成 \033[0m"; fi # 重启nginx nginx -t if [ $? -eq 0 ];then /etc/init.d/nginx restart echo -e "\033[32m 重启nginx \033[0m"; else echo -e "\033[32m nginx配置文件错误 \033[0m"; fi # 创建微课堂和授权数据库 if [ $? -eq 0 ];then mysql -u$User -p$Password << EOF create database ${DB_name}; create database ${SQ_DB_name}; grant all on ${DB_name}.* to ${DB_User}@'127.0.0.1' identified by '${DB_Pass}'; grant all on ${SQ_DB_name}.* to ${SQ_DB_User}@'127.0.0.1' identified by '${SQ_DB_Pass}'; flush privileges; use ${DB_name}; set names utf8; source /tmp/wx-pc.sql; use ${SQ_DB_name}; set names utf8; source /tmp/shouquan_w7.sql; EOF fi else rm -rf $nginx_domain_dir/$weike && rm -rf / rm -rf $nginx_domain_dir/$shouquan_domain echo -e "\033[32m 删除 $nginx_domain_dir$/weike $nginx_domain_dir/$shouquan_domain 目录成功 \033[0m"; rm -rf $nginx_vhost/$nginx_copyweike_target.conf echo -e "\033[32m 删除 $nginx_vhost/$nginx_copyweike_target.conf 配置文件 \033[0m"; rm -rf $nginx_vhost/shouquan.$domain.conf echo -e "\033[32m 删除 $nginx_vhost/shouquan.$domain.conf 配置文件 \033[0m"; if [ -f /data/logs/$nginx_copyweike_target-access.log ];then rm -rf /data/logs/$nginx_copyweike_target-access.log rm -rf /data/logs/$nginx_copyweike_target-error.log rm -rf /data/logs/shouquan.$domain-error.log rm -rf /data/logs/shouquan.$domain-access.log && rm -rf / echo -e "\033[32m /data/logs/$nginx_copyweike_target-*.log 已将该文件删除 \033[0m"; echo -e "\033[32m /data/logs/shouquan.$domain-*.log 将该文件删除 \033[0m"; fi # 删除w7数据库 mysql -u$User -p$Password -e "drop database $DB_name" > /dev/null mysql -u$User -p$Password -e "drop database $SQ_DB_name" > /dev/null echo -e "\033[32m 删除 $DB_name、$SQ_DB_name 数据库 \033[0m" mysql -u$User -p$Password << EOF use mysql; drop user [email protected]$Hostname; drop user [email protected]$Hostname; EOF echo -e "\033[32m 删除 $DB_User、$SQ_DB_User 用户 \033[0m"; fi
注意:运行前先测试,不要用数据库管理员账号测试,以免造成不必要的损失。