apache自动更新站点内容脚本

实现思路:

通过软链接的方式更改网站目录对应的站点目录

准备工作:

  1. 先将网站内容的两个版本上传到服务器上面,存放路径为/server/www

[root@IT-test www]# ll
总用量 8
-rw-r--r-- 1 root root 167 9月   1 12:54 html1.1.tar.gz
-rw-r--r-- 1 root root 166 9月   1 12:57 html1.2.tar.gz
  1. 配置Apache虚拟主机,网站访问路径为/alidata/xx/webapps/xx

[root@IT-test scripts]# tail -15 /usr/local/apache2/conf/extra/httpd-vhosts.conf
………
<VirtualHost *:80>
    ServerAdmin admin@dyb.com
    DocumentRoot/alidata/xx/webapps/xx
    <Directory"/alidata/xx/webapps/xx">
        Options IndexesFollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    ServerName xx.dyb.com
    DirectoryIndex index.htmlindex.htm index.jsp
    ErrorLoglogs/uh.dyb.com_error_log
    CustomLoglogs/uh.dyb.com_access_log common
</VirtualHost>

实现脚本:

通过脚本实现自动更新站点内容(脚本需要加网站版本号参数即可)

[root@IT-test scripts]# cat change_html.sh
#!/bin/bash
#this script create at 2015-09-01
#author:hao123
#this script is use to change html version
 
#版本存放目录
html_ver_dir=/server/www
#web访问目录
html_acc_dir=/alidata/xx/webapps
 
         if [ $# -ne 1 ];then
                   echo"you need add html version id,like 1.1"
         else
                   num=`find $html_ver_dir -type f -name "html$1.tar.gz"|wc -l`
                   if [ $num-eq 1 ];then
                            cd $html_ver_dir
                            find $html_ver_dir -type d ! -name "html$1" -a ! -name"www"|xargs rm -rf
                            tar zxf html$1.tar.gz
                            cd $html_acc_dir
                            rm -f xx
                            ln -s $html_ver_dir/html$1 xx
                   else
                            echo"Don't find the html version"
                   fi
         fi

结果展示:

[root@IT-test scripts]# sh change_html.sh 1.2
[root@IT-test scripts]# ll /server/www/
总用量 12
-rw-r--r-- 1 root root  167 9月   1 13:55 html1.1.tar.gz
drwxr-xr-x 2 root root 4096 9月   1 13:54 html1.2
-rw-r--r-- 1 root root  167 9月   1 13:55 html1.2.tar.gz

wKiom1XlWNHSmCJnAADDMuNiqHw124.jpg

[root@IT-test scripts]# sh change_html.sh 1.1
[root@IT-test scripts]# ll /server/www/
总用量 12
drwxr-xr-x 2 root root 4096 9月   1 13:54 html1.1
-rw-r--r-- 1 root root  167 9月   1 13:55 html1.1.tar.gz
-rw-r--r-- 1 root root  167 9月   1 13:55 html1.2.tar.gz

wKiom1XlWSGy2YQhAAC6Ka0UZX8456.jpg


问题说明:

  1. 如果使用软链接访问网站报错,可参考http://szh11.blog.51cto.com/825676/1690446解决

  2. 后期可以和svn配合,开发将网站代表打包传到svn上,然后通过脚本实现自动更新网站版本