1

私は、codeigniter を使用して Web ホスティング ホスティング コントロールパネルを開発しています。ここまでは順調ですね。:)

現在、仮想ホストを作成するソリューションに取り組んでいます。仮想ホストを作成するシェル スクリプトが機能するので、最初に考えたのは、そのスクリプトを 15 分ごとに cron ジョブで起動することでした。それはうまくいくはずです。

ただし、15 分ごとに作成する新しい仮想ホストがなくなるわけではありません。そのため、15 分ごとに apache 構成をリロードするのは多すぎると思います。

ちなみに、codeigniter 側では、その新しい仮想ホストに属する値を含む単純なテキスト ファイルを作成するだけです。

それで、リアルタイムでそれを行うための保存ソリューションはありますか? 私の推測では、shell_exec() を使用してリアルタイムで実行する唯一の方法ですが、それは保存方法ではありません。

私のシェルスクリプトは非常に初心者なので、仮想ホストを作成するか、何もしないかを選択する if または else ステートメントをトリガーする方法があると言わざるを得ません。しかし、どうすればそれを行うことができますか? それから私はリアルタイムでそれをする必要はありません。

これは私のシェルスクリプトです:

#!/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

誰かがこの問題に対するシンプルだが効果的な解決策を持っていることを願っています。

4

2 に答える 2

2

スクリプトにbool 変数を追加するだけで、新しい vhost が追加された場合にのみ Web サーバーを再起動できます。

未テスト:

#!/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、仮想ホスト以外のものを構成に挿入するために使用できないことを願っています。:)

于 2013-01-05T11:42:47.150 に答える
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
于 2016-08-11T16:22:19.197 に答える