Case XIV increase LAMP project site

A running server LAMP environment, above a dozen sites, each site is assigned a separate virtual hosts and MySQL database. Currently the number of sites continues to increase, each additional site needs to be done as follows:

1) add a virtual host apache

2) add a ftp account that has administrative rights on the site directory

3) a new library

4) Add a new database user management library


Since these operations are repeated with a high degree, it is suitable to achieve shell script.


A knowledge point: LAMP

LAMP is a very common site architecture, its structure as shown below:

Apache, PHP parsing not supported by default, resolve to implement in PHP framework PHP as an Apache module, and MySQL database interaction and behavior by libphp5.so done. The PHP and MySQL association is implemented by PHP mysql.so inside the module.

After LAMP to build a good environment, you need to configure virtual hosts, that in order to run the site LAMP environment, first of all indicators to be configured to run this site, such as domain names and websites paths. Follows with reference to a virtual host:

Apache, PHP parsing not supported by default, resolve to implement in PHP framework PHP as an Apache module, and MySQL database interaction and behavior by libphp5.so done. The PHP and MySQL association is implemented by PHP mysql.so inside the module.

After LAMP to build a good environment, you need to configure virtual hosts, that in order to run the site LAMP environment, first of all indicators to be configured to run this site, such as domain names and websites paths. Follows with reference to a virtual host:

Apache, PHP parsing not supported by default, resolve to implement in PHP framework PHP as an Apache module, and MySQL database interaction and behavior by libphp5.so done. The PHP and MySQL association is implemented by PHP mysql.so inside the module.

After LAMP to build a good environment, you need to configure virtual hosts, that in order to run the site LAMP environment, first of all indicators to be configured to run this site, such as domain names and websites paths. Follows with reference to a virtual host:

spacer.gifclipboard.png

Apache, PHP parsing not supported by default, resolve to implement in PHP framework PHP as an Apache module, and MySQL database interaction and behavior by libphp5.so done. The PHP and MySQL association is implemented by PHP mysql.so inside the module.

After LAMP to build a good environment, you need to configure virtual hosts, that in order to run the site LAMP environment, first of all indicators to be configured to run this site, such as domain names and websites paths. Follows with reference to a virtual host:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/fuxi.com"
    ServerName fuxi.com
    ServerAlias www.fuxi.com www.fuxi01.com
    <Directory /data/wwwroot/fuxi.com>
      AllowOverride none
      Require all granted
    </Directory>
</VirtualHost>

说明:DocumentRoot为网站根目录,ServerName为域名,ServerAlias为域名的别名,下方的<Directory></Directory>部分用来配置访问权限。所以,增加网站就是增加这样一段配置。


知识点二:FTP服务

FTP服务,主要用来传输文件。在LAMP的服务器上跑着很多网站,每个网站的代码都应该分开来管理,这就需要针对每个网站创建一个独立的FTP用户来管理本网站的文件。使用FTP上传、下载文件虽然方便,但在安全性上存在一定的风险,如果FTP的用户名、密码泄露,后果不堪设想。另外一个层面,由于网站的文件权限比较复杂,不容易控制,权限给小了会导致访问出错,权限给大了,又存在安全问题。

对于规模不大的网站,站长或者开发人员往往缺乏安全意识,为了便携性所以直接使用FTP来管理网站,其实这不可取,而应该使用专业的代码管理平台(如git)和代码上线部署的工具(如Jenkins)。话虽如此,但是本案例的场景就是使用FTP管理网站,在这里只是提醒一下各位,这并不安全。

而本案例的FTP用户也是虚拟用户,虚拟用户的好处是不用提供系统用户的密码,相对安全性更高。本案例shell脚本中的操作步骤就是参考上面的文档来实现的。


知识点三:在MySQL中增加用户

MySQL本身有命令行工具,在shell脚本中,通常使用-e来实现的,以下命令为在MySQL中增加库:

# mysql -uroot -pyour_passwd -e "create database newdb"

MySQL默认有一个root用户,跟Linux的系统用户root一样,它可以创建库、表以及普通用户等操作,创建普通用户并授权的命令如下:

# mysql -uroot -pyour_passwd -e "grant all on 'newdb.*' to 'user1'@'10.20.100.%' identified by 'user1_passwd'"

说明:这个all指的是所有权限,包括读、写、删除等全部操作权限。on后面为库和表,用'.'把库和表分隔开,其中*表示所有的表,即该库下的所有表。to后面为用户以及来源的IP地址,@后面为来源IP,可以用%表示通配,比如上例中的10.20.100.%表示10.20.100.1-10.20.100.254全部的IP,当然IP这里也可以直接写%,这样则不限制来源IP。by后面那个字符为用户密码。有时候,创建完用户不会立即生效,是因为新增的用户还在缓存里,需要执行一个刷新权限的命令:

# mysql -uroot -pyour_passwd -e "flush privileges"


本案例参考脚本

#!/bin/bash
#本脚本的功能是在LAMP环境中增加站点,包括apache配置、FTP增加用户、MySQL增加库和用户
#作者:
#日期:

#网站目录
webdir=/data/wwwroot

#ftp的虚拟用户配置文件目录
ftpudir=/etc/vsftpd/vuuser

#ftp虚拟用户密码文件
ftpuserfile=/root/login

#mysql命令行登录root
mysqlc="/usr/local/mysql/bin/mysql -uroot -pjk1hYUcnt6"

#apache虚拟主机配置文件
httpd_config_f="/usr/local/apache2/conf/extra/httpd-vhosts.conf"

#定义增加MySQL库和用户的函数
add_mysql_user()
{
    #生成随机密码
    mysql_p=`mkpasswd -s 0 -l 12`

    #将密码保存到临时文件里,这里的$pro为用户自定义的项目名字
    echo "$pro $mysql_p" >/tmp/$pro.txt

    #这里使用嵌入文档的形式(需顶格),将创建用户并授权的命令传递给mysql    
$mysqlc <> $ftpuserfile
    echo "$ftp_p" >> $ftpuserfile

    #将用户、密码文件转换为密码db文件
    db_load -T -t hash -f $ftpuserfile  /etc/vsftpd/vsftpd_login.db
    cd $ftpudir

    #这里的aaa是一个文件,是之前的一个项目,可以作为配置模板
    cp aaa $pro 

    #把里面的aaa改为新的项目名字
    sed -i "s/aaa/$pro/" $pro 

    #重启vsftpd服务
    /etc/init.d/vsftpd restart
}

#定义增加apache虚拟主机的函数
config_httpd()
{
    #增加网站根目录,和域名保持一致,这里的$dom为用户自定义的域名
    mkdir $webdir/$dom

    #将网站根目录属主和属组设置为ftp用户
    chown vsftpd:vsftpd $webdir/$dom
    
    #用嵌入文档(需顶格),把虚拟主机配置写入到配置文件里
cat >> $httpd_config_f <
    DocumentRoot $webdir/$dom
    ServerName $dom
    
        AllowOverride none
        Require all granted  
    

EOF
   
    #重载apache服务
    /usr/local/apache2/bin/apachectl graceful
}

read -p "input the project name: " pro
read -p "input the domain: " dom

add_mysql_user
add_ftp_user
config_httpd

After the execution of the script, the user will be prompted to enter a project name and the domain name, as follows:

# sh 14.sh 
input the project name: test123
input the domain: www.test123.com

In this way, Pro value is test123, and the value of dom is www.test123.com.


Guess you like

Origin blog.51cto.com/13576245/2429960