Running Multiple Sites on Your Local Apache Server via Virtual Hosts
Posted by Johnny on April 7th, 2009 filed in Guides, TechnologyDisclaimer: I only recommend doing this on a local dev box, as I don’t know what sort of (in)security implications this may have.
There are times when, for the sake of convenience or testing, you may want to run multiple sites off your local machine. This can be accomplished by setting up a virtual host in Apache and tinkering with the port numbers. Looking in your /etc/apache2/sites-available/ directory you’ll notice a default file. Make a copy of this and name it whatever you’d like (perhaps the name of your site), then open it up in a text editor. Change port 80 in the first line, <VirtualHost *:80>, to some unused port number, and the DocumentRoot directory to that of your other site. You may also want to change some other variables such as ErrorLog so as to not conflict with other sites that you’re running.
Then open up NameVirtualHost *:<port_number> Listen <port_number> For additional information on virtual hosts, including how to have IP-based (rather than port-based) virtual hosts, take a look at Apache's virtual host documentation. /etc/apache2/ports.conf and add
where <port_number> is the one you specified in the new site configuration, after the existing defaults for port 80. Going back to /etc/apache2/sites-available/, run the a2ensite command on the site configuration file you just created, then restart Apache with sudo apache2ctl restart. You should now be able to hit your other site via http://localhost:<port_number>/. To later disable this site, run the a2dissite <config_file> command.
Leave a Comment