通過shell指令碼搭建虛擬機器主機方法

來源:互聯網
上載者:User

今天下午寫了一個簡單的一鍵式自動虛擬機器主機搭建shell指令碼,指令碼實現自動設定nginx虛擬機器主機,自動建立ftp賬戶,自動建立資料庫,使用者,並自動實現mysql自動定時備份,日誌切割,程式備份。
這裡因為其他原因,就將備份機制去掉了,代碼如下。
首先需要一個基礎的nginx虛擬機器主機設定檔,一般情況下,我們配置虛擬機器主機都是建一個vhost目錄,這裡我在/etc/nginx下面建了一個default.conf檔案

 代碼如下 複製代碼

log_format  #host#  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
server {
        listen       80;
    server_name  #host#;
    root  #hosts#/#host#/html;
    index index.html index.htm index.php default.html default.htm default.php;
        location / {
                autoindex  on;
        }
        error_page  404              /404.html;
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
         #注意這一句
          location ~ ^(.+.php)(.*)$ {
          fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
         include        fastcgi_params;
         }
 
        # redirect server error pages to the static page /50x.html
 
        access_log  #hosts#/#host#/log/#host#.log  #host#;
}

注意裡面一些關鍵路徑,我用特殊字元組合來表示,這樣方便我們添加虛擬注意的時候進行匹配替換

host.sh指令碼如下

 代碼如下 複製代碼

#/bin/sh
hosts="/var/www/html" #主機總目錄
default="/etc/nginx/default.conf"  #default.conf位置
vhost="/etc/nginx/vhost"  #虛 www.111cn.net 擬主機設定檔目錄
echo "1.add ftp user"
echo "please input websit:"
read host
if [ -d "$hosts/$host" ]
then
echo $hosts/$host
echo '[warning]dir is already exists!'
exit 0
fi
echo "add user,please input user name:"
read name
del -r $name >/dev/null 2>&1
adduser -d $hosts/$host -g ftp -s /sbin/nologin $name
passwd $name
chmod 755 $hosts/$host
mkdir -p $hosts/$host/html $hosts/$host/bak $hosts/$host/log
mkdir -p $hosts/$host/bak/code $hosts/$host/bak/sql
echo "mkdir:"$hosts/$host/html $hosts/$host/bak $hosts/$host/log
echo "mkdir:"$hosts/$host/bak/code $hosts/$host/bak/sql
chown -R $name:ftp $hosts/$host
echo "ok,add user success!name=$name,password=youwrite"
echo "If you need a database, please enter a database name, if not required, blank can be"
read database
if [ -n "$database" ]
then
echo "please input dbuser"
read dbuser
echo "please input dbpwd"
read dbpwd
HOSTNAME="127.0.0.1"
PORT="3306"
USERNAME="root"
echo "input root pwd"
read PASSWORD
fi
echo "2.To configure nginx"
cat $default | sed -e "s:#hosts#:${hosts}:g"|sed -e "s/#host#/${host}/g" > $vhost/$host.conf
/usr/sbin/nginx -s reload
echo "config nginx success"
if [ -z "$database"  ]
then
echo 'ok,finish!'
exit 0
fi
echo "3.add mysql user database"
 
create_db_sql="insert into mysql.user(Host,User,Password) values('localhost','${dbuser}',password('${dbpwd}'))"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
if [ $? -ne 0 ]
then
echo 'add db user error'
exit 0
fi
sleep 1
create_db_sql="create database IF NOT EXISTS ${database}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
if [ $? -ne 0 ]
then
echo 'add db error'
exit 0
fi
sleep 1
create_db_sql="flush privileges"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
 
create_db_sql="grant all  on ${database}.* to ${dbuser}@localhost identified by '${dbpwd}'"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"
if [ $? -ne 0 ]
then
echo 'user to db user error'
echo $create_db_sql
exit 0
fi
create_db_sql="flush privileges"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "{create_db_sql}"
echo 'ok,finish!'

看一下運行效果,sh host.sh

 代碼如下 複製代碼
[root@localhost nginx]# sh 2.sh
1.add ftp user
please input websit:
hhh.com
add user,please input user name:
hhh

更改使用者 hhh 的密碼 。
新的 密碼:
無效的密碼: WAY 過短
無效的密碼: 是迴文
重新輸入新的 密碼:
passwd: 所有的身分識別驗證令牌已經成功更新。

 代碼如下 複製代碼
mkdir:/var/www/html/hhh.com/html /var/www/html/hhh.com/bak /var/www/html/hhh.com/log
mkdir:/var/www/html/hhh.com/bak/code /var/www/html/hhh.com/bak/sql
ok,add user success!name=hhh,password=youwrite
If you need a database, please enter a database name, if not required, blank can be
hhh
please input dbuser
hhh
please input dbpwd
hhh
input root pwd
123456
2.To configure nginx
config nginx success
3.add mysql user database
ok,finish!
[root@localhost nginx]#

系統會逐個詢問輸入參數,並且參數是先統一填完,然後程式進行執行,避免中間不小心輸入錯誤無法修改,然後如果無需建立資料庫,那麼database參數不輸入即可。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.