2013-01-05 53 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