xampp每增加一个网站或减少一个网站,都需要配置apache的httpd-vhosts.conf,配多了会非常麻烦。Shell脚本可以非常方便的解决这个问题,在增加或删除网站时,只要重新运行一下shell脚本,即可完成httpd-vhosts.conf的修改,非常方便。代码如下:
> httpd-vhosts.conf #清除文件原有内容,重新生成 for file in `ls '/opt/lampp/htdocs' | grep -v 'xampp\|webalizer\|dashboard'` #默认网站目录放在htdocs文件夹下 do dotnum=`echo "$file" | awk -F'.' '{print NF-1}'` #根据文件目录名中的句号数量来区分顶级域名、二级域名以及普通文件夹 if [ "$dotnum" = "1" ];then #根目录下直接放网站目录,要求目录名为没带www的域名。 echo '' >> httpd-vhosts.conf echo ' DocumentRoot "/opt/lampp/htdocs/'$file'"' >> httpd-vhosts.conf echo ' ServerName www.'$file >> httpd-vhosts.conf echo ' ServerAlias '$file >> httpd-vhosts.conf echo ' ErrorLog "logs/'$file'-error_log"' >> httpd-vhosts.conf echo ' CustomLog "logs/'$file'-access_log" common' >> httpd-vhosts.conf echo ' ' >> httpd-vhosts.conf fi if [ "$dotnum" = "2" ];then #根目录下直接放网站目录,处理二级域名情况。 echo '' >> httpd-vhosts.conf echo ' DocumentRoot "/opt/lampp/htdocs/'$file'"' >> httpd-vhosts.conf echo ' ServerName '$file'' >> httpd-vhosts.conf echo ' ErrorLog "logs/'$file'-error_log"' >> httpd-vhosts.conf echo ' CustomLog "logs/'$file'-access_log" common' >> httpd-vhosts.conf echo ' ' >> httpd-vhosts.conf fi if [ "$dotnum" = "0" ];then #根目录下还有一层文件夹,再往下才是网站目录,要求子目录名为网站域名。 for subfile in `ls '/opt/lampp/htdocs/'$file | grep -v 'xampp\|webalizer'` do subdotnum=`echo "$subfile" | awk -F'.' '{print NF-1}'` #根据子目录名中的句号数量来区分顶级域名、二级域名 if [ "$subdotnum" = "1" ];then #子目录下直接放网站目录,要求目录名为没带www的域名。 echo '' >> httpd-vhosts.conf echo ' DocumentRoot "/opt/lampp/htdocs/'$file'/'$subfile'"' >> httpd-vhosts.conf echo ' ServerName www.'$subfile >> httpd-vhosts.conf echo ' ServerAlias '$subfile >> httpd-vhosts.conf echo ' ErrorLog "logs/'$subfile'-error_log"' >> httpd-vhosts.conf echo ' CustomLog "logs/'$subfile'-access_log" common' >> httpd-vhosts.conf echo ' ' >> httpd-vhosts.conf fi if [ "$subdotnum" = "2" ];then #子目录下直接放网站目录,处理二级域名情况。 echo '' >> httpd-vhosts.conf echo ' DocumentRoot "/opt/lampp/htdocs/'$file'/'$subfile'"' >> httpd-vhosts.conf echo ' ServerName '$subfile'' >> httpd-vhosts.conf echo ' ErrorLog "logs/'$subfile'-error_log"' >> httpd-vhosts.conf echo ' CustomLog "logs/'$subfile'-access_log" common' >> httpd-vhosts.conf echo ' ' >> httpd-vhosts.conf fi done fi done
在linux下使用Shell脚本是非常方便的,能够实现非常强大的功能。另外,如果需要分析服务器日志,那么Shell更是首选,以后要继续深入研究一下。