0

I'm working on Windows XP.

I need the batch file to do the following:

  1. Create a directory $DOMAINNAME on D:/Webserver/domains/%DOMAINNAME/www/
  2. Open the file - D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
  3. Append the following to that file:

    <VirtualHost *:1337>
        DocumentRoot "D:/Webserver/domains/$DOMAINNAME/www"
        ServerName "$DOMAINNAME"
        ServerAlias "$DOMAINNAME"
        ErrorLog "logs/$DOMAINNAME-error.log"
        CustomLog "logs/$DOMAINNAME-access.log" common
    <Directory "D:/Webserver/domains/$DOMAINNAME">
    Allow From all
    AllowOverride All
    </Directory>
    </VirtualHost>
    
  4. Open the file - C:/WINDOWS/sysstem32/drivers/etc/hosts

  5. Append this to that file:

    127.0.0.1 $DOMAINNAME

  6. Close the files.

I do this manually but it's tiring. Please help.

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
Kirill Firsov
  • 75
  • 1
  • 5
  • As an aside, my antivirus goes mental every time I modify the hosts file. Your antivirus may also similarly object to a script modifying the file and may even veto it. – Ben Pilbrow Jun 08 '11 at 20:53
  • 3
    Are you asking for someone here to write this script for you? Probably not going to get any takers. Have you tried writing this yourself and not had success? Describe what you've done and what isn't working and you'll probably get better answers. – mfinni Jun 08 '11 at 20:54
  • @Ben Pilbrow, I think that the antivirus can add an exception. If not, how to make the first paragraphs? – Kirill Firsov Jun 08 '11 at 20:55
  • As @mfinni says, you have to take a stab at this before you'll get help. If it helps, this appears straightforward. We have confidence in your ability to pull it off! – uSlackr Jun 08 '11 at 21:04
  • 1
    I was wrong - you did get two good answers and I voted for both of them. Good for them - but you really have to give things a first shot yourself. Simple scripting like this is so easy, that once you've described in detail what you need it to do, you've written 80% of it. – mfinni Jun 09 '11 at 02:13

2 Answers2

4

Here you go:

mkdir D:\Webserver\domains\%1
mkdir D:\Webserver\domains\%1\www
echo ^<VirtualHost %1:1337^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo DocumentRoot "D:/Webserver/domains/%1/www" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ServerName "%1" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ServerAlias "%1" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ErrorLog "logs/%1-error.log" >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo CustomLog "logs/%1-access.log" common >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ^<Directory "D:/Webserver/domains/%1"^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo Allow From all >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo AllowOverride All >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ^</Directory^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo ^</VirtualHost^> >> D:/Webserver/apache2/conf/extra/httpd-vhosts.conf
echo 127.0.0.1 %1 >> C:/WINDOWS/system32/drivers/etc/hosts

Just save it as a .bat file and run it using the domain name you want as a parameter, like this:

script.bat MyDomain

I've also fixed an error in your configuration: you can't have multiple default sites listening on the same port, so the <VirtualHost> directive needs to contain the domain name.

Massimo
  • 70,200
  • 57
  • 200
  • 323
3

You would be better served using PowerShell, which will run on XP. But if you're stuck with .bat:

CreateDomain.bat newdomain.com

cd D:\Webserver\domains\
mkdir %1
mkdir %1\www
cd D:\Webserver\apache2\conf\extra\

echo DocumentRoot "D:/Webserver/domains/%1/www"  >> httpd-vhosts.conf
echo ServerName "%1" >> httpd-vhosts.conf
echo ServerAlias "%1" >> httpd-vhosts.conf
echo ErrorLog "logs/%1-error.log" >> httpd-vhosts.conf
echo CustomLog "logs/%1-access.log" common >> httpd-vhosts.conf
echo Allow From all >> httpd-vhosts.conf
echo AllowOverride All >> httpd-vhosts.conf

cd c:\Windows\System32\Drivers\etc

echo "127.0.0.1  %1" >> hosts

This is quick and dirty, and includes zero error-catching (what happens when a mkdir fails? This script doesn't care). And keep in mind that AV software gets really, really paranoid about modifications to the hosts file, so it may not be possible to run.

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300