1

I am developing a webhosting hosting controlpanel with codeigniter. So far so good. :)

Now I am working on a solution creating virtual hosts. My shell script that creates the virtual host works, so my first thought was to fire that script in a cronjob let say every 15 minutes. That should work.

But not every 15 minutes there won't be a new virtual host to create. So, I think it's to much to reload the apache config every 15 minutes.

By the way, on the codeigniter side it just make a simple textfile with the values that belongs by that new virtual host.

So, is there a save solution to do it in realtime? My gues is, that the only way to do it realtime to use shell_exec(), but that's not a save way.

I must say that my shell scipting is very novice so maybe there is a way to trigger a if or else statement to choose to create a virtual host or just do nothing. But how can i do that? Then i don't need to do it in realtime.

This is my shell script:

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

I hope someone has a simple but effective solution for this problem.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

You could just add a bool variable to the script and only restart the webserver if a new vhost was added.

Untested:

#!/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: I hope that everything in $user and $domain is safe and cannot be used to inject something other than your vhost into the config. :)

Community
  • 1
  • 1
hangy
  • 10,765
  • 6
  • 43
  • 63
2

Very useful. I made a new version of the script which also remove the vhost, looking into another .txt file. Document root folders are stored in /var/www/html/websites. Change it upon your need. The script also checks if the variable $domain is empty , to make sure script does no run in case something goes wrong whit the domain name

#!/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
Maddish
  • 81
  • 6