Shell脚本一键自动部署项目

                                                                           Shell脚本一键自动部署项目

#!/bin/bash
#定义根目录、代码仓库、项目域名信息
read -p '请输入项目名称[中文名称]:'  Chinese_Project_Name
read -p '请输入项目目录[eg:/home/wwwroot]:'  webroot
read -p '请输入代码仓库[eg:git格式]:'  git_address
read -p '请输入项目域名[eg:www.test.com]:'  project_domain_name

#显示刚才输入的根目录、代码仓库、项目域名信息
echo -e "\033[32m您输入的\033[0m \033[41;30m项目目录\033[0m \033[32m是:\033[0m\033[45;30m$webroot\033[0m"
echo -e "\033[32m您输入的\033[0m \033[41;30m代码仓库\033[0m \033[32m是:\033[0m\033[45;30m$git_address\033[0m"
echo -e "\033[32m您输入的\033[0m \033[41;30m项目域名\033[0m \033[32m是:\033[0m\033[45;30m$project_domain_name\033[0m"

#确认根目录、代码仓库、项目域名是否正确,只有正确才执行后续步骤,否则退出
read -r -p "Are You Sure? [Y/n] " input
case $input in
    [yY][eE][sS]|[yY])
		echo -e  "\033[33mYes\033[0m"
		;;
    [nN][oO]|[nN])
		echo -e  "\033[31mNo\033[0m"
		exit 1
       	;;
    *)
		echo -e  "\033[46mInvalid input!!!\033[0m"
		exit 1
		;;
esac

#项目名称、拉取develop分支代码、授权www权限
#由git地址截取项目名称[:/.]即以':'、'/'、'.'为分隔符
project_name=`echo  $git_address  |   awk  -F  '[:/.]'    '{ print  $6}'`
cd  $webroot

if [ ! -d  $project_name ]
then
   git  clone   -b  develop  $git_address
   chown  -R   www.www   $project_name
else
   echo -e "\033[41;30m$project_name 已经存在!\033[0m"
fi


#定义配置文件所在的目录
configure_directory="/usr/local/nginx/conf/vhost"
cd $configure_directory

#定义配置VUE框架函数
Configure_VUE_frame()
{
cat >  ${project_domain_name}.conf << EOF
server
    {
        listen 80;
        server_name ${project_domain_name};
        index index.html index.htm;
        root  ${webroot}/${project_name}/dist;

	location / {
    	    try_files \$uri \$uri/ /index.html =404;
	}

	location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        error_log  /home/wwwlogs/${project_domain_name}.error.log;
    }
EOF
}



#定义配置Laravel框架函数
Configure_Laravel_frame()
{
cat >  ${project_domain_name}.conf << EOF
server {
    listen 80;
    server_name ${project_domain_name};
    index index.html index.htm index.php default.html default.htm default.php;
    root   ${webroot}/${project_name}/public;

    location / {
         try_files \$uri \$uri/ /index.php\$is_args\$args;
    }

    location ~ \.php/?.*$ {
        try_files \$uri /index.php =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index  index.php;
        fastcgi_param   SCRIPT_FILENAME   \$document_root\$fastcgi_script_name;
        include fastcgi_params;
    }

   location ~ /\.ht {
        deny all;
    }
   error_log  /home/wwwlogs/${project_domain_name}.error.log;
}
EOF
}



#定义配置ThinkPHP框架函数
Configure_ThinkPHP_frame()
{
cat >  ${project_domain_name}.conf << EOF
server
    {
        listen 80;
        #listen [::]:8090;
      	server_name  ${project_domain_name};
        index index.html index.htm index.php default.html default.htm default.php;
        root  ${webroot}/${project_name};

        include other.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php5.5-pathinfo.conf;
	#include enable-php-pathinfo.conf;

	location ~  \.php$
	{
    		set \$path_info "";
    		set \$real_script_name \$fastcgi_script_name;
        #如果地址与引号内的正则表达式匹配
    		if (\$fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
        #将文件地址赋值给变量 \$real_script_name
    		set \$real_script_name \$1;
        #将文件地址后的参数赋值给变量 \$path_info
    		set \$path_info \$2;
    		}

        #配置fastcgi的一些参数
    	fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;
    	fastcgi_param SCRIPT_NAME \$real_script_name;
    	fastcgi_param PATH_INFO \$path_info;
	}

	if (!-e \$request_filename)
	{
	#地址作为将参数rewrite到index.php上。
	rewrite ^/(.*)$ /index.php/\$1;
	#rewrite ^/([a-zA-Z0-9_]+)/(.*)$  /\$1/index.php/\$2;
	#若是子目录则使用下面这句,将subdir改成目录名称即可。
	#rewrite ^/(.*)$ /gc2_new_wap/index.php?\$1;
	}

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        error_log  /home/wwwlogs/${project_domain_name}.error.log;
    }
EOF
}



#选择需要安装的框架
#cat  << EOF
echo -e "\033[33m*******************************\033[0m"
echo -e "\033[33mThe following is optional!!!\033[0m"
echo -e "\033[33m******************************\033[0m"
echo -e "\033[33m      1) VUE\033[0m"
echo -e "\033[33m      2) Laravel\033[0m"
echo -e "\033[33m      3) ThinkPHP\033[0m"
echo -e "\033[33m      4) Exit\033[0m"
echo -e "\033[33m*******************************\033[0m"
#EOF

read -p "Please enter your chioce:" option
case $option in
1)
  Configure_VUE_frame
  ;;
2)
  Configure_Laravel_frame
  ;;
3)
  Configure_ThinkPHP_frame
  ;;
*)
  echo "Option is inviald."
esac



Create_database()
{
echo -e "\033[33m*******************************\033[0m"
echo -e "\033[33mThe following is optional!!!\033[0m"
echo -e "\033[33m******************************\033[0m"
echo -e "\033[33m  1) 47.99.102.6\033[0m"
echo -e "\033[33m  2) 120.26.106.212\033[0m"
echo -e "\033[33m  3) 112.124.48.228\033[0m"
echo -e "\033[33m  4) 47.111.179.201\033[0m"
echo -e "\033[33m*******************************\033[0m"

#连接指定数据库的管理员账号
read -p  "请输入需要连接的数据库IP[RDS数据库不适用]:"  Host
read -p  "请输入数据库用户名[root]:"  User
read -p  "请输入数据库密码:"  Password

#创建数据库名称、数据库账号、数据库密码
read -p  "请输入需要创建的数据库名称:" database_name
read -p  "请输入需要创建的数据库账号:" database_account
read -p  "请输入需要创建的数据库密码:" database_passwd


#显示需要创建的数据库名称、数据库账号、数据库密码信息
echo -e "\033[32m请输入需要创建的\033[0m \033[41;30m数据库名称\033[0m \033[32m是:\033[0m\033[45;30m$database_name\033[0m"
echo -e "\033[32m请输入需要创建的\033[0m \033[41;30m数据库账号\033[0m \033[32m是:\033[0m\033[45;30m$database_account\033[0m"
echo -e "\033[32m请输入需要创建的\033[0m \033[41;30m数据库密码\033[0m \033[32m是:\033[0m\033[45;30m$database_passwd\033[0m"

#确认数据库名称、数据库名称、数据库名称信息,只有正确才执行后续步骤,否则退出
read -r -p "Are You Sure? [Y/n] " input
case $input in
    [yY][eE][sS]|[yY])
                echo -e  "\033[33mYes\033[0m"
                ;;
    [nN][oO]|[nN])
                echo -e  "\033[31mNo\033[0m"
                ;;
    *)
                echo -e  "\033[46mInvalid input!!!\033[0m"
                ;;
esac

#创建数据库、授权sql语句
exec_sql_command="create  database ${database_name}; grant all  on  ${database_name}.*  to  ${database_account}@'%'  identified by '${database_passwd}'; flush privileges;"
mysql -h$Host -u$User -p$Password  -e "${exec_sql_command}"
}

#是否创建数据库
read -r -p "Create database or not? [Y/n] " input
case $input in
    [yY][eE][sS]|[yY])
                Create_database
                ;;
    [nN][oO]|[nN])
                echo -e  "\033[31mNo\033[0m"
                exit 1
        ;;
    *)
                echo -e  "\033[46mInvalid input!!!\033[0m"
                exit 1
                ;;
esac


#汇总所有相关信息,输出到指定文档中
echo "#########################################${Chinese_Project_Name}##############################################"   >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m项目目录\033[0m \033[32m是:\033[0m\033[45;30m$webroot\033[0m"  >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m代码仓库\033[0m \033[32m是:\033[0m\033[45;30m$git_address\033[0m" >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m项目域名\033[0m \033[32m是:\033[0m\033[45;30m$project_domain_name\033[0m" >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m配置文件\033[0m \033[32m是:\033[0m\033[45;30m${configure_directory}/${project_domain_name}.conf\033[0m" >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m数据库名称\033[0m \033[32m是:\033[0m\033[45;30m$database_name\033[0m" >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m数据库账号\033[0m \033[32m是:\033[0m\033[45;30m$database_account\033[0m" >> /home/yangq/all_project_information.txt
echo -e "\033[32m${Chinese_Project_Name}的\033[0m \033[41;30m数据库密码\033[0m \033[32m是:\033[0m\033[45;30m$database_passwd\033[0m" >> /home/yangq/all_project_information.txt
echo "##############################################################################################################"  >> /home/yangq/all_project_information.txt
echo ""  >> /home/yangq/all_project_information.txt

 

 

你可能感兴趣的:(shell,运维)