2013-01-05 50 views
0

我正在开发一个带有codeigniter的虚拟主机控制面板。 目前为止这么好。 :)困境:实时创建虚拟主机还是使用crontab?

现在我正在研究创建虚拟主机的解决方案。 我的shell脚本创建虚拟主机的工作,所以我的第一个想法是在cronjob中每15分钟发一个脚本。这应该工作。

但是不是每15分钟就不会有一个新的虚拟主机来创建。 所以,我认为这需要每15分钟重新载入一次apache配置。

顺便说一句,在codeigniter方面,它只是使用属于该新虚拟主机的值创建一个简单的文本文件。

那么,是否有保存解决方案来实时进行? 我的感觉是,这是实时使用shell_exec()的唯一方法,但这不是一种保存方式。

我必须说,我的shell scipting是非常新手,所以也许有一种方法来触发if或else语句来选择创建虚拟主机或者什么也不做。 但我该怎么做?那么我不需要实时做。

这是我的shell脚本:

#!/bin/bash 
vhroot='/etc/apache2/sites-available/' 
NEW_DOMAINS="/home/domain.txt" 

cat ${NEW_DOMAINS} | \ 

while read domain user email 
do 

echo "<VirtualHost *:80> 
    ServerName "$domain" 
    ServerAlias www."$domain" 
    ServerAdmin "$email" 
    DocumentRoot /home/"$user"/domains/"$domain"/public_html 
</VirtualHost>" > $vhroot/$domain 

#mkdir /home/$user/domains/domain 
#mkdir /home/$user/domains/$domain/public_html 
#chown -R $user.$user /home/$user/domains/$domain 

echo "111.21.111.111  $domain" >> host.txt 
#a2ensite $hostname 
done 

echo "" > /home/domain.txt 

# /etc/init.d/apache2 reload 

我希望有人有针对此问题一个简单而有效的解决方案。

回答

1

您只需将bool variable添加到脚本中,并且只在添加新虚拟主机时才重新启动网络服务器。

未经测试:

#!/bin/bash 
vhroot='/etc/apache2/sites-available/' 
NEW_DOMAINS="/home/domain.txt" 
has_new_domains=false #No new domains by default = do not reload the apache config. 

cat ${NEW_DOMAINS} | \ 

while read domain user email 
do 
    has_new_domains=true #true = at least one new domain = reload apache config 
    echo "<VirtualHost *:80> 
    ServerName "$domain" 
    ServerAlias www."$domain" 
    ServerAdmin "$email" 
    DocumentRoot /home/"$user"/domains/"$domain"/public_html 
</VirtualHost>" > $vhroot/$domain 

    #mkdir /home/$user/domains/domain 
    #mkdir /home/$user/domains/$domain/public_html 
    #chown -R $user.$user /home/$user/domains/$domain 

    echo "111.21.111.111  $domain" >> host.txt 
    #a2ensite $hostname 
done 

echo "" > /home/domain.txt 

if $has_new_domains ; then #only reload the apache config if there is at least one new domain 
    /etc/init.d/apache2 reload 
fi 

BTW:我希望一切都在$user$domain是安全的,不能用于注入其他的东西比你的虚拟主机进入配置。 :)

1

非常有用。 我做了一个新版本的脚本,它也删除了虚拟主机,查看另一个.txt文件。文档根文件夹存储在/ var/www/html/websites中。根据需要改变它。 该脚本还检查变量$ domain是否为空,以确保脚本在没有运行的情况下不会运行以防出现问题域名

#!/bin/bash 
vhroot='/etc/apache2/sites-available/' 
NEW_DOMAINS="adddomain.txt" 
has_new_domains=false #No new domains by default = do not reload the apache config. 

cat ${NEW_DOMAINS} | \ 

while read domain 
do 
    if [ ! -z "$domain" ]; 
    then 
     has_new_domains=true #true = at least one new domain = reload apache config 
     echo "<VirtualHost *:80> 
     ServerName "$domain" 
     ServerAlias www."$domain" 
     ServerAdmin [email protected]"$domain" 
     DocumentRoot /var/www/html/websites/"$domain" 
     </VirtualHost>" > $vhroot/"$domain".conf #.conf extension needed to make a2ensite work in apache -debian 
     mkdir /var/www/html/websites/ 
     mkdir /var/www/html/websites/$domain 
     chown -R root:www-data /var/www/html/websites 
     chmod -R 755 /var/www/html/websites 

     #create index.html file 
     echo "<!DOCTYPE html> 
     <html> 
     <head> 
     <title>Welcome to nginx on Debian!</title> 
     <style> 
     body { 
      width: 35em; 
      margin: 0 auto; 
     font-family: Tahoma, Verdana, Arial, sans-serif; 
     } 
     </style> 
     </head> 
     <body> 
     <h1>Welcome to "$domain"</h1> 
     <p>If you see this page, the Apache web server is successfully installed and working.</p> 

     <p> 
     You can start building your website 
     </p> 

     </body> </html>">/var/www/html/websites/$domain/index.html 
     #echo "111.21.111.111  $domain" >> host.txt 
     a2ensite "$domain".conf 
    else echo 'No new domains' 
    fi 
done 

> adddomain.txt # with echo "" an empty line is still present in file 
DEL_DOMAINS="deldomain.txt" 
cat ${DEL_DOMAINS} | \ 

while read deldomain 
do 
    has_new_domains=true #true = at least one new domain = reload apache config 
    #Make sure we don't delete all parent directory , in case variable is empty 
    if [ ! -z "$deldomain" ]; then 
     a2dissite "$deldomain".conf 
     echo "dominio "$deldomain" eliminado" 
     rm -r /var/www/html/websites/$deldomain 
     rm $vhroot/"$deldomain".conf 
    fi 
done 
> deldomain.txt 
if $has_new_domains ; then #only reload the apache config if there is at least one new domain 
    /etc/init.d/apache2 reload 
fi