Need a Shell or PHP Script to Create Domain on Apache

Status
Not open for further replies.

chatmasta

New member
Jan 7, 2007
2,613
68
0
NYC
Hey,

I'm about to launch a project which includes mass domain creation, so I need a script to create domains (i.e. virtual hosts) on Apache. Shell script is preferred, but PHP is alright too.

It would also be cool if someone has a script to create nameservers on Named.
 


Easy to write. However you need to restart named/apache.
One solution is to use mydns (database) and apache2 (reload).
 
I need to restart apache to create a vhost? How does cpanel do it? I don't think it restarts apache...really what I want to do is replicate that process.

Sorry, I don't know a whole lot about linux or apache. :p
 
Depends on your apache release. In theory, you can do a reload-only for both of them (kill -USR1 pid for apache1) but you will have to test. It seems it is better to do a restart for apache1. I don't know what cPanel does exactly but I think it has it's own integrated webserver so an apache restart has no consequence on it.
If you're running apache2, no problem (-k graceful will update the configuration).
 
I'll just ask my server company to make sure they install apache2. :p

Anyway, do you have a shell script for this?
 
Hey,

I'm about to launch a project which includes mass domain creation, so I need a script to create domains (i.e. virtual hosts) on Apache. Shell script is preferred, but PHP is alright too.

One way to do it is to setup an IP-based website, point your virtual hosts there, then use mod_rewrite to send requests to a certain directory based on the request header.

Here is another way to do it in Apache 1.x httpd.conf. Might work on Apache 2.x, not sure:

Code:
<VirtualHost 201.10.10.30:80>
        ServerName vhost-1.yourdomain.com
        VirtualDocumentRoot /home/virtual/%-2.0.%-1.0
</VirtualHost>

That will set the doc root for a request for www.domain123.com to go to /home/virtual/domain123.com/

If you do write code to add vhost entries, then you can use "apachectl graceful" to restart Apache without breaking existing connections.

And you don't need to create nameservers in named, you can just set BIND to listen to all IPs on the server. BIND doesn't care about what nameservers it is answering as.
 
cPanel restarts apache after it adds a virtualhost, you will notice that because if you abort it and it doesn't add the group for example then apache will not be running.
When I mass added domains I just used cat to add it to the end of the file, for example this is what I done,
setup.sh
Code:
cat >>/etc/httpd/conf/domains.conf <<EOM
<VirtualHost *:80>
    ServerAdmin [EMAIL="sysadmin@admingeekz.com"]sysadmin@admingeekz.com[/EMAIL]
    ServerName server.admingeekz.com
    ServerName $1
    ServerAlias *.$1
    DocumentRoot /home/httpd/domains/$1
</VirtualHost>
EOM
Then,
chmod 700 setup.sh
./setup.sh google.com
That will setup google.com based on the assumption that,
- You use /etc/httpd/conf/domains.conf (I simply included this in the main httpd.conf)
- You want your domain path to be /home/httpd/domains/domainhere
Since we just use these as parking domains this is a perfect setup. We generally do it to add the dns zone aswell which uses this
Code:
#!/bin/bash
if [ ! -d "/home/httpd/domains/$1" ]
then
mkdir /home/httpd/domains/$1
cat >>/etc/httpd/conf/domains.conf <<EOM
<VirtualHost *:80>
    ServerAdmin [EMAIL="sysadmin@admingeekz.com"]sysadmin@admingeekz.com[/EMAIL]
    ServerName server.admingeekz.com
    ServerName $1
    ServerAlias *.$1
    DocumentRoot /home/httpd/domains/$1
</VirtualHost>
EOM

cat >>/etc/named.conf <<EOM
zone "$1" {
        type master;
        file "/var/named/$1.db";
};
EOM

cp -f /etc/template.named /etc/buffernamed
replace "cdomain.com" "$1" -- /etc/buffernamed
mv /etc/buffernamed /var/named/$1.db
chown named:named /var/named/$1.db
chown -R node:node /home/httpd/domains/$1
/etc/init.d/named reload
/etc/init.d/httpd reload
echo "Added $1 has been setup"
else
        echo "$1 is already setup"
fi

Note this reloads apache and named (dns) and chowns it to the user and group node (we use this as a central ftp to all the domains rather than thousands of different logins). It does one fault check which is only to see if the directory exists (it creates it otherwise). If you are mass adding domains you should comment out the 2 reload lines simply because it will slow it down it will take around 10 minutes to add 2000 domains if you reload each time where as if you comment it out and then loop and then reload once all are added it will be around 20 seconds.
An example to setup lots of domains would be to create a file say domains.txt and then for loop such as
Code:
for i in `cat domains`;do ./setup.sh $i;done
Hope this helps.
-Scott
 
  • Like
Reactions: yast
Status
Not open for further replies.