Ubuntu: Is it possible to set up multiple local domains on localhost in Ubuntu 14.04?


I need unique, multiple domains all linked to my computer's local ip address. This is for testing purposes. I want to be able to have different local domains linked to different html files on my computer. For example, xxx.local and yyy.local will both be distinct but only accessible from my computer.

What I've tried: configuring apache2's sites-enabled and sites-available to have xxx.local and yyy.local's .conf files be linked to different DocumentRoots. However, it's not resolved, and when I link them to my computer's local ip address they both just link to html files already linked to my computer's localhost. Not to the html files specified by DocumentRoot in the .conf files.


Most of the work is done in the configuration file in the directory /etc/apache2/sites-enabled/, in a file called something like 000-default.conf. This files will allow you to have multiple virtual domains, such as www.example.dev, or example.local, etc. You set them up along with their directories in this file, and enter multiple sites in /etc/hosts to match. All sites have the same localhost ip address, but the name directs it to the correct virtual host.

In this file, you need to clone the default settings as many times as you need, with something like this:

Original contents

<VirtualHost *:80>     . . .  </VirtualHost>  

Additional virtual host entries

<VirtualHost *:80>      <Directory  /var/www/html/site1>         Options Indexes FollowSymLinks MultiViews         AllowOverride All         Order allow,deny         allow from all      </Directory>      DocumentRoot  /var/www/html/site1/       ServerName site1.local      ServerAlias www.site1.local  </VirtualHost>    <VirtualHost *:80>      <Directory  /var/www/html/site2>         Options Indexes FollowSymLinks MultiViews         AllowOverride All         Order allow,deny         allow from all      </Directory>      DocumentRoot  /var/www/html/site2/       ServerName site2.local      ServerAlias www.site2.local  </VirtualHost>    . . .  

In order for the rewrite magic to work, you also need to make sure the rewrite engine is enabled. This is done by creating a symbolic link in mods-enabled to a module in mods-available. If you cd to /etc/apache2/mods-enabled, then enter ln -s ../mods-available/rewrite.load, it will enable the rewrite engine.

Then, in /etc/hosts/, you have matching entries like so:  site1.local  site2.local  

This should get you going until I either complete this, or you have questions for me to answer.

Answers to questions in comments 1

One problem is the "DocumentRoot" directive: the directory specified is supposed to simply be the document root of the virtual site, which is most likely not "/var/www/arcadr.local/html" as specified. Normally the site directory would be at the end. "arcadr.local" is an arbitrary name, but matching the site is probably best. So it should probably be "'/var/www/html/arcadr.local/'".

As far as the ln error, if I had had more time, I would have explained that the bottom line is you need a symbolic link named "rewrite.load" in the mods-enabled directory that links to the actual file in the mods-available directory. So evidently, you already had it, and didn't really need to do it again, but as long as it's there, it should work.

Try the one change, and also try to understand what it's doing. It's actually pretty understandable, and once you start understanding, you'll learn more.

By the way, the "alias" directive isn't necessary, I just added that to illustrate how you could do it if you want.

Answers to questions in comments 2

The default html file is simply one of now several virtual hosts specified in the file 000-default.conf. Note I originally left off the ".conf" extension of the file. Did you find that correct file? It must be in "sites-enabled", not "sites-available". Same as for the mods, you can have multiple sites available, but you need to have one enabled, and the default is "000-default.conf" (unless it has changed since I set it up).

You say "arcadr.local just redirects me to another .local website connected to my localhost." Make sure you have this configuration file set up correctly. It should have a series of blocks, like html:

<VirtualHost *:80>     . . .  </VirtualHost>  

Each virtual host must start and end with these directives, and you cannot have one embedded in another.

You can find some good documentation and examples on the apache.org website. This is where I learned how to set up the files. My advice is to keep it simple, and if you add anything you don't understand, do it one step at a time, and remove anything that doesn't work. Here are some examples: VirtualHost Examples

Some additional pointers

Whenever you make changes to the configuration file at /etc/apache2/sites-enabled/000-default.conf, you need to restart the Apache server. I use the command sudo /etc/init.d/apache2 restart to do this. Edit: Gabriel posted a better command for this:

sudo service apache2 restart

But both commands will work, at least for now.

This same technique works for dynamic DNS servers such as noip.com, dyndns, etc. For example, I just made 3 sample sites on my home system. If my computer is on, you can try it to see. If you check, you'll see that all 3 sites have the exact same IP address, but go to different virtual sites (variations on the default, which you can see at leftcoast.noip.us

http://site1.leftcoast.noip.us/  http://site2.leftcoast.noip.us/  http://site3.leftcoast.noip.us/  


There are two steps you need to take to do that.

First it to point out to your computer that you want him to look for xxx.local and yyy.local at, which usually means localhost or your computer.

This is made by editing your /etc/hosts file.

Then you have to configure your webserver to answer differently according to which serverName is being asked for, which is done by fiddling with apache virtual hosts.

Marty's answer is pretty complete in regards to that subject. And don't forget to sudo service apache2 restart so it reloads it's configurations.

Also, can you pastebin your virtual hosts configurations?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »