【问题标题】:Dilemma: realtime create virtual hosts or with a crontab?困境:实时创建虚拟主机还是使用 crontab?
【发布时间】:2012-12-19 17:30:42
【问题描述】:

我正在使用 codeigniter 开发一个虚拟主机托管控制面板。 到目前为止,一切都很好。 :)

现在我正在研究创建虚拟主机的解决方案。 我创建虚拟主机的 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

我希望有人对这个问题有一个简单但有效的解决方案。

【问题讨论】:

    标签: php apache bash shell virtual-hosts


    【解决方案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
    

    顺便说一句:我希望$user$domain 中的所有内容都是安全的,并且不能用于将除您的虚拟主机之外的其他内容注入到配置中。 :)

    【讨论】:

      【解决方案2】:

      非常有用。 我制作了一个新版本的脚本,它也删除了虚拟主机,查看了另一个 .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 postmaster@"$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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-03
        • 2019-08-17
        • 2012-02-03
        • 2014-08-23
        • 1970-01-01
        • 2012-01-29
        • 2022-09-27
        相关资源
        最近更新 更多