龚哥哥 - 山里男儿 爱生活、做自己!
Nginx支持htpasswd 访问需要用户和密码认证
发表于 2016-5-26 | 浏览(5699) | 服务器

准备、创建脚本目录

/data/server/nginx/conf/htpasswd

一、创建文件 htpasswd.sh  内容如下

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "========================================"
echo "# A tool like htpasswd for Nginx       #"
echo "#--------------------------------------#"
echo "# Author:Devil http://gongfuxiang.com  #"
echo "========================================"

# file path
dir=$(pwd);
user="user.conf";
userfile="${dir}/${user}"

#set username
    username=""
    read -p "Please input username:" username
    if [ "$username" = "" ]; then
            echo "Error:username can't be NULL!"
            exit 1
    fi
    echo "==========================="
    echo "username was: $username"
    echo "==========================="

#set password
    unpassword=""
    read -p "Please input the Password:" unpassword
    if [ "$unpassword" = "" ]; then
            echo "Error:Password can't be NULL!"
            exit 1
    fi
    echo "==========================="
    echo "Password was: $unpassword"
    echo "==========================="
    password=$(perl -e 'print crypt($ARGV[0], "pwdsalt")' $unpassword)

# create file
if [ ! -f ${userfile} ]; then
  touch ${userfile} 

  echo "Create Auth file......"
fi

# delete original user
for file in $userfile;
do         
    sed -i "/^$MON[\t ]*$username/d" $file  
done

# add user
echo $username:$password >> $userfile
if [ $? == 0 ]; then
    echo $username:$password;
    echo "user add success, Auth file  path:${userfile}."
else
        echo "error"
        exit 1
fi

二、修改虚拟机配置文件 server 中添加以下配置信息

auth_basic "Authorized users only";
auth_basic_user_file /data/server/nginx/conf/htpasswd/user.conf;

三、添加授权用户(根据提示输入用户名和密码)

sh htpasswd.sh

阅读全文

Shell使用教程
发表于 2016-4-12 | 浏览(5556) | 服务器
#!/bin/sh

# # 用户输入数据获取
# echo "What is your name?"
# read PERSON
# echo "Hello, $PERSON"

# # 定义变量,在变量名前面不需要加¥符号,使用的时候才需要加
# # 定义变量
# ret="value"
# echo $ret

# # 重新定义变量
# ret="value-ssss"
# echo $ret

# # 数字型变量
# number=100
# echo $number # line 22: unset: `value-ssss': not a valid identifier

# # 删除变量
# unset $ret
# echo $ret

# # 字符串循环,变量与字符串混合使用的使用加{}花括号
# # 推荐使用变量的时候,加花括号
# str="Ada Coffa Action Java"
# for v in $str
# do
#   echo "I am good at ${v}Script"
# done

# 字符串可以是单引号或者双引号,单引号中变量当做字符串输出,双引号中支持变量
# ret="ret string"
# # 单引号中不能带单引号,就是加了反斜杠转义也不可以
# # str='hello world \' $ret'
# str='hello world $ret ${ret}'
# echo $str
# str="Hello World Is \"$ret\""
# echo $str

# # 字符串拼接
# xing='Fu'
# strs="Gong "$xing" Xiang"
# name="Gong ${xing} Xiang"

# echo $strs
# echo $name

# # 获取字符串长度
# echo ${#name}

# # 提取子字符串
# echo ${name:4} # Fu Xiang
# echo ${name:1:5} # ong F

# # 查找子字符串,有问题
# # echo `expr index "$name" Fu`

# # 数组
# # 数组名=(值1 值2 值3 ... 值n)
# # 如:arr=(1 2 3 4 5 6)
# arr=(11 2222 3 44 5 6)
# echo $arr # 默认第一个下标元素 11
# echo ${arr[0]} # 第一个元素 11
# echo ${arr[1]} # 第二个元素 2222
# echo ${arr[*]} # 所有元素 11 2222 3 44 5 6
# echo ${arr[@]} # 所有元素 11 2222 3 44 5 6
# echo ${#arr[*]} # 数组的长度(下标1开始) 6
# echo ${#arr[@]} # 数组的长度(下标1开始) 6
# echo ${#arr[1]} # 元素1的长度 4(下标0开始)
# arr[1]="new values heihei"
# echo ${arr[1]}

# # 传递参数,没有参数则空
# # sh testshell.sh 11 22 你好世界
# echo "第一个参数:${1}"  # 11
# echo "第二个参数:${2}"  # 22
# echo "第三个参数:${3}"  # 你好世界
# echo "参数个数:${#}"  # 3
# echo "以一个但字符串显示所有脚本传递的参数:${*}"  # 11 22 你好世界
# echo "脚本运行的当前进程ID号:${$}"  # 当前进程ID号
# echo "后台运行的最后一个进程的ID号:${!}"
# echo "与$*相同,但是使用时加引号,并在引号中返回每个参数:${@}"  # 11 22 你好世界
# echo "显示Shell使用的当前选项,与set命令功能相同:${-}"  # hB
# echo "显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误:${?}"  # 0或错误

# 基本运算符
# 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
# expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
# 完整的表达式需要被 ` 符号包含(不是单引号)
# 乘法 * 必须加反斜杠
# val=`expr 2 + 2` # 2+2是不对的,中间必须空格分开
# echo "两数之和为:${val}"
# num1=1
# num2=2
# num3=3
# echo "num1+num2:`expr ${num1} + ${num2}`"   # 3
# echo "num3-num1:`expr ${num3} - ${num1}`"   # 2
# echo "num2*num3:`expr ${num2} \* ${num3}`"  # 6
# echo "num3/num2:`expr ${num3} / ${num2}`"   # 1
# echo "num3%num2:`expr ${num3} % ${num2}`"   # 1

# # if判断语句
# if [ $num1 == $num2 ]
# then
#   echo "num1等于num2"
# fi
# if [ $num1 != $num2 ]
# then
#   echo "num1不等于num2"
# fi

# # 关系运算符、不支持字符串、只支持数字
# # -eq 等于、-ne 不等于、-gt 大于、-lt 小于、-ge 大于等于、-le 小于等于
# if [ $num1 -eq $num2 ]
# then
#   echo "num1等于num2"
# fi
# if [ $num1 -ne $num2 ]
# then
#   echo "num1不等于num2"
# fi
# if [ $num2 -gt $num1 ]
# then
#   echo "num2大于num1"
# fi
# if [ $num1 -lt $num2 ]
# then
#   echo "num1小于num1"
# fi

# # 布尔运算符
# # ! 非运算(取反)、-o 或运算(一个符合就true)、-a 与运算(需要都符合就true)
# if [ ! $num1 == $num2 ]
# then
#   echo "num1不等于num2"
# fi
# if [ $num1 -ne $num2 -a $num2 -lt $num3 ]
# then
#   echo "条件成立"
# fi

# # 字符串运算符
# str1="abc"
# str2="efg"
# str3="abc"
# if [ $str1 = $str3 ]
# then
#   echo "str1等于str3"
# fi
# if [ $str1 != $str2 ]
# then
#   echo "str1不等于str2"
# fi
# if [ -z $str1 ]
# then
#   echo "str1长度为0"
# fi
# if [ -n $str1 ]
# then
#   echo "str1长度不为0"
# fi
# if [ $str1 ]
# then
#   echo "str1不为空"
# fi

# # 文件运算符
# dir="/data/"
# file="hello.txt"
# if [ -e ${dir}${file} ]
# then
#   echo "${file}文件存在"
# else
#   echo "${file}文件不存在"
# fi
# if [ -s ${dir}${file} ]
# then
#   echo "${file}文件大小大于0"
# else
#   echo "${file}文件大小等于0"
# fi
# if [ -x ${dir}${file} ]
# then
#   echo "${file}文件可执行"
# else
#   echo "${file}文件不可执行权限(755后几可执行了)"
# fi
# if [ -w ${dir}${file} ]
# then
#   echo "${file}文件可写"
# else
#   echo "${file}文件不可写"
# fi
# if [ -f ${dir}${file} ]
# then
#   echo "${file}文件是普通文件"
# else
#   echo "${file}文件不是普通文件"
# fi
# if [ -f "${dir}testshell.sh" ]
# then
#   echo "testshell.sh文件是普通文件"
# else
#   echo "testshell.sh文件不是普通文件"
# fi

# # echo 命令
# echo "it is s test"
# echo "\"id is a ' test\""
# read name # 接收键盘录入数据
# # > 覆盖文件中的内容、>>尾部追加内容
# echo "${name} it is a test `date`" >> ${dir}${file} # 讲内容写入文件
# cat ${dir}${file} # 查看文件中的内容

# # printf 命令
# echo "Hello World"
# printf "Hello World\n"

# printf "%-10s %-10s %-10s\n" 姓名 性别 体重kg
# printf "%-12s %-10s %-10.2f\n" 龚福祥 男 52.25453
# printf "%-12s %-10s %-10.2f\n" 潘玉龙 男 48.34566
# printf "%-11s %-10s %-10.2f\n" 杨过 男 60

# printf "%d %s\n" 100 "abc"
# printf '%d %s\n' 200 "efg"
# printf '%d %s %s %s\n' 300 "Hello" "World" "!"
# printf %s abc efg
# printf "\n\n"
# printf "%s\n" abcc efgg
# printf "www.gongfuxiang.com \n"

# # 控制流程
# num1=100
# num2=100
# if [ $num1 -eq $num2 ]
# then
#   echo "num1等于num2"
# else
#   echo "num1不等于num2"
# fi

# arr=(11 22 33 44 55)
# str="1 2 3 4 5"
# for index in ${arr[*]}
# #for index in $str
# do
#   echo "index the is: ${index}"
# done
# for tmp in 'Hello World'
# do
#   echo $tmp
# done

# int=0
# while(($int <= 5))
# do
#   let "int++"

#   # case 语句
#   case $int in
#       2 | 5)
#           echo "case int is ${int}"
#       ;;
#       4)
#           echo "case int is ${int}"
#       ;;
#   esac

#   # int等于2的时候跳出当前循环
#   if [ $int -eq 2 ]
#   then
#       continue
#   fi

#   # int等于4的时候结束循环
#   if [ $int -eq 5 ]
#   then
#       break
#   fi

#   echo "int is ${int}"
# done

# # function 方法
# # 函数调用必须在调用之前
# function DemoFun()
# {
#   echo "demofun()"
# }
# # 函数调用
# echo "函数执行开始"
# DemoFun
# echo "函数执行完毕"

# # 用户输入的值进行相加返回
# function WitchReturn()
# {
#   echo "输入第一个数字:"
#   read num

#   echo "输入第二个数字:"
#   read another
#   return $(($num+$another))
# }
# # 函数返回值使用 $? 来获取
# WitchReturn
# echo $?

# # 函数参数传递获取
# function FunParam()
# {
#   echo "第一个参数:${1}"
#   echo "第二个参数:${2}"
#   echo "第三个参数:${5}"
#   echo "所有参数:${*}"
#   echo "参数个数:${#}"
# }
# FunParam 11 22 33 "aa" "bb"

# # 输入/输出重定向
# file="hello.txt"

# # 尾部追加内容
# echo "gongfuxiang.com 龚福祥" > $file

# # 覆盖文件中的内容
# echo "gongfuxiang.org" >> $file

# # 文件中的内容行数
# #wc -l $file # 会输出文件名
# wc -l < $file # 不会输出文件名

# cat $file

# # 结果
# #     Devil
# # www.gongfuxiang.com
# cat << EOF
#   Devil
#   www.gongfuxiang.com
# EOF

# # 结果 2
# wc -l  << EOF
#   Devil
#   www.gongfuxiang.com
# EOF

# # 命令执行的内容不显示在终端上
# ls > /dev/null

# # 文件引入 source关键字或 .
# # testshell.sh testshell2.sh
# #. ./testshell2.sh
# source ./testshell2.sh

# # 使用变量
# echo $url

# # 使用方法
# ShellTestFun

# # (()) 双小括号的使用
# a=1
# b=2
# as=$((a+=1)) # 2
# echo $as
# echo $((a+=2)) # 4

# bs=$((b+1)) # 3
# echo $bs
# echo $((b+=2)) # 4

# # 表达式多个值
# c=1
# d=1
# e=1
# $((c++, d++, e--))
# # c,d,e 结果2,2,0
# echo $c
# echo $d
# echo $e

# # for循环
# for((i=0; i<=10; i++))
# do
#   echo $i
# done

# 创建测试目录
dir_name="shell_test"

mkdir $dir_name

read number
for((i=0; i<=$number; i++))
do
    # 在目录下循环创建多个文件
    touch "${dir_name}/test_file_${i}.txt"
done

阅读全文

Shell脚本发布系统
发表于 2015-12-18 | 浏览(6322) | 服务器
#!/bin/sh

# desc      项目上线脚本
# time      2015-12-18
# author    Devil
# version   2.0

echo "---------- 准备中... ----------"
user=`whoami`
date=$(date +%Y%m%d%H%M%S)
time=$(date +%Y-%m-%d" "%H:%M:%S)
name="fangao"
tar_name="${date}_${name}.tar.gz"
test_dir="test/test_app"
bak_dir="bak"
bak_log_dir="bak_log/"$(date +%Y/%m)
date_name=$(date +%d)".txt"
time_start=$(date +%s)

# 日志写入方法
function LogInsert()
{
    echo "user:${user}, date:${time}, msg:${1}, code:${2}" >> "${bak_log_dir}/${date_name}"
    if [ $2 == "success" ]
    then
        echo -e "\e[1;32m ${1} \e[0m"
    else
        echo -e "\e[1;31m ${1} \e[0m"
        exit
    fi
}

# 恢复脚本是否正在运行
is_restore=$(ps -ef | grep "restore" | grep -v grep | wc -l)
if [ $is_restore != 0 ]
then
        LogInsert "恢复脚本正在运行,请先停止再上线项目" "error"
fi

# 当前脚本是否在运行多个
is_online=$(ps -ef | grep "online" | grep -v grep | wc -l)
if [ $is_online -gt 2 ]
then
        LogInsert "当前脚本正在多处运行,请确认一人操作" "error"
fi

# 备份路径不存在则创建
if [ ! -x "$bak_dir" ]
then
    mkdir -p $bak_dir
fi

# 日志目录不存在则创建
if [ ! -x "$bak_log_dir" ]
then
    mkdir -p $bak_log_dir
fi

# 需要上线的目录不存在则退出
if [ ! -x "$test_dir" ]
then
    LogInsert "$test_dir 目录不存在" "error"
fi

echo "---------- 准备结束 ----------"

echo -e
echo "---------- 确定需要上线么?确定:Y  取消:N ----------"
read start_state
if [[ $start_state != "Y" ]]
then
    LogInsert "你取消了操作" "error"
fi

echo -e
echo "---------- 开始压缩,请稍候... ----------"
tar -cf $tar_name --exclude=img --exclude=audio --exclude=tpl_c --exclude=db_log $name
if [ $? == 0 ]
then
    LogInsert "压缩成功" "success"
    echo "压缩包文件名 ${tar_name}" >> "${bak_log_dir}/${date_name}"
else
    LogInsert "压缩失败" "error"
fi
echo "---------- 压缩结束 ----------"

echo  -e
echo "---------- 开始移动压缩包,请稍候... ----------"
mv $tar_name $bak_dir
if [ $? == 0 ]
then
    LogInsert "压缩包移动成功" "success"
else
    LogInsert "压缩包移动失败" "error"
fi
echo "---------- 压缩包移动结束 ----------"

echo  -e
echo "---------- 开始更新项目代码,请稍候... ----------"
cd $test_dir
git checkout master
git pull origin master
if [ $? == 0 ]
then
    echo -e "\e[1;32m git更新master成功 \e[0m"
else
    echo -e "\e[1;31m git更新失败 \e[0m"
    exit
fi
echo "---------- 项目代码更新结束 ----------"

echo -e
echo "---------- 确定迁移项目么?确定:Y  取消:N ----------"
read success
if [[ $success != "Y" ]]
then
        echo -e "\e[1;31m 你终止了迁移操作 \e[0m"
    exit
fi
echo "---------- 开始迁移项目,请稍候... ----------"
cp -r `ls | grep -v config | xargs` ../../$name
cp_state=$?
cd ../../
if [ $cp_state == 0 ]
then
    time_total=$[$(date +%s)-$time_start]
    LogInsert "项目迁移成功 [耗时:${time_total}秒]" "success"
else
    LogInsert "项目迁移失败" "error"
fi
echo "---------- 项目迁移结束 ----------"
echo -e
echo -e "\e[1;36m config目录下的所有文件都未迁移,如有文件需要迁移,请手动操作 \e[0m"
echo -e

阅读全文

Redis同一台服务器开启不同工作空间
发表于 2015-8-30 | 浏览(7245) | 服务器

1;拷贝redis.conf配置文件

redis6379.conf
pidfile /var/run/redis6380.pid
port 6380

redis6380.conf
pidfile /var/run/redis6380.pid
port 6380

2;启动redis同时加载配置文件

redis-server redis6379.conf &
redis-server redis6380.conf &

3;php测试

<?php

    /**
     * redis测试
     */

    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->set('devil', 'hello world!');
    echo $redis->get('devil');

    echo '<br />---------------------<br />';

    $redis2 = new Redis();
    $redis2->connect('127.0.0.1', 6380);
    $redis2->set('devil', 'gongfuxiang');
    echo $redis2->get('devil');

    echo '<br />---------------------<br />';

    echo $redis->get('devil');

    // 输出
    hello world!
    ---------------------
    gongfuxiang
    ---------------------
    hello world!

?>

阅读全文

TOP