Using shell script to build a virtual host method

Source: Internet
Author: User
Tags flush mkdir sleep create database

This afternoon wrote a simple one-click Automatic Virtual Host shell script, the script to implement automatic configuration Nginx virtual host, automatically create FTP account, automatically create a database, users, and automatically achieve MySQL automatic scheduled backup, log cutting, program backup.
For other reasons, the backup mechanism is removed and the code is as follows.
First need a basic Nginx virtual host configuration file, in general, we configure the virtual host is to build a vhost directory, here I built a default.conf file under/etc/nginx

The code is as follows Copy Code

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 #;
}

Note that some of the key paths inside, I use a special character combination to represent, so that we can add virtual attention when the matching replacement

The host.sh script is as follows

The code is as follows Copy Code

#/bin/sh
hosts= "/var/www/html" #主机总目录
default= "/etc/nginx/default.conf" #default. conf position
Vhost= "/etc/nginx/vhost" #虚 www.111cn.net the intended host configuration file directory
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 are need a database, please enter a database name, If not required, blank can"
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! '

Look at the effect of the operation, SH host.sh

The code is as follows Copy Code
[Root@localhost nginx]# SH 2.sh
1.add FTP User
Please input websit:
Hhh.com
Add user,please Input User name:
Hhh

The

Changes the user's HHH password.
New password:
Invalid password: WAY Too short
Invalid password: is palindrome
Reenter the new password:
passwd: All authentication tokens have been updated successfully.

  code is as follows copy code
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 are need a database, please Enter a database name, if not required, blank can is
HHH
Please input dbuser
HHH
Please input dbpwd
hh H
Input root pwd
123456
2.To Configure Nginx
Config nginx success
3.add mysql user database
Ok,fin Ish!
[root@localhost nginx]#

The

system asks for input parameters, and the parameters are uniformly filled out, and then the program executes, avoiding careless input errors that cannot be modified, and then the database parameter is not entered if you do not need to create it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.