I am trying to create a virtual host on Apache in Ubuntu(in a virtual machine) but I cannot get it. I did a lot of configurations but any of them give me any result so I created a new virtual machine to do it from the begining. I am going to put here all the process step by step so all of you could follow what I am doing wrong.

What I have

  • VirtualBox
  • Ubuntu 14.04 (Virtual machine on VirtualBox)
  • I instaled Apache on Ubuntu (sudo apt-get install apache2)

What I need

  • When I write the url ejemplo.es on my browser the file that I created on my VirtualHost folder will be displayed and not the page that it is on www.ejemplo.es (that it is not mine).

  • Use hosts file on /etc folder instead of DNS configuration.

What I tried

  • First of all: change the root directory of my server

First of all, what I need it is to make my root directory of my server as /var/www/extras so I created this new directory on /var/www.

sudo mkdir /var/www/extras

and create an index.html with some text on it.

After creating the new directory I have to change my 000-default.conf file to change the DocumentRoot directive.

sudo gedit /etc/apache2/sites-available/000-default.conf

and change the DocumentRoot directive as follows:

DocumentRoot /var/www/extras

and after that I restart Apache to save my changes:

sudo /etc/init.d/apache2 restart

and modify the Directory block on apache2.conf file

sudo gedit /etc/apache2/apache2.conf

from <Directory /var/www/html> to <Directory /var/www/extras> and restart Apache again.

  • Second step: creating the folder and the file from the virtual host I want to create

Inside /var/www/extras directory I am going to create a new folder (ejemplo.es) which is going to be the folder of my virtual host.

sudo mkdir /var/www/extras/ejemplo.es

and inside of it an index.html file as I created for the root directory of my server with the text "This is an example".

After that, if I go to the browser and write localhost/ejemplo.es I can see the content of the file I created in the step before. But what I want it is to access to the same content but typing ejemplo.es instead of localhost/ejemplo.es.

  • Final step: Configurate the virtual host

I need to create a new file on /etc/apache2/sites-available with the configuration of my virtual host.

sudo gedit /etc/apache2/sites-available/ejemplo.es.conf

with the following configuration:

    ServerAdmin webmaster@ejemplo.es
    ServerName ejemplo.es
    ServerAlias www.ejemplo.es
    DocumentRoot /var/www/extras/ejemplo.es

and on apache2.conf file I am going to change IncludeOptional sites-enabled/*.conf to Include sites-enabled/*.conf. (This was a prove to see if maybe my Apache did not include this folder and could be the posibility that my VirtualHost did not work for that).

So now I have to activate the VirtualHost I have created:

sudo a2ensite ejemplo.es

and restart Apache to save the changes I made:

sudo /etc/init.d/apache2 restart

If I list the content of the folder /etc/apache2/sites-enabled I can see that my VirtualHost it is active now (ls command to this folder).

To end, I add a new line on my hosts file (sudo gedit /etc/hosts):   ejemplo.es

so now the file has three lines with IP's:    localhost    user    ejemplo.es

and some lines below that starts with ff00... but I think they are not relevant for my purpose.

And finally I have to execute the following command to save my last changes:

sudo /etc/init.d/networking restart

but I get the default page of www.ejemplo.es instead the file that I put on my VirtualHost folder.

I am sure I am doing something wrong but cannot figure out where. It is the first time I create one VirtualHost and all tutorials that I have seen had these steps so I think I am missing something.

I expect you could follow my steps and ask if you have some doubt about the configuration that I made.

Thanks in advance!

  • You must note that /etc/hosts only work with explicit ip>host directives, that means that subdomains wildcard do not apply. So 'ejemplo.es' is diffrent than 'www.ejemplo.es', so add the later with the same ip to the file and see if it works. Check serverfault.com/questions/118378/… if you need wildcard support. – bistoco Feb 28 '16 at 5:58
  • @bistoco but my ServerName of my VirtualHost is ejemplo.es, www.ejemplo.es it is the ServerAlias. – Error404 Feb 28 '16 at 6:04
  • Yes, but you need to point www.ejemplo.es to your vm, adding the line www.ejemplo.es on /etc/hosts – bistoco Feb 28 '16 at 6:10

I wanted to comment on the question for further information but I haven't had enough reputation point, so I just try answer this for you to give us little more information.

I did the same steps as you listed above in my new VM, and it's work fine.

I don't know exactly how you test the above domain, whether browser in your box (ubuntu - client) or in your host (which is running virtualbox).

If you're trying to visit the domain under client-ubuntu, you can add more IP into virtualhost tag:

<VirtualHost ejemplo.es:80></VirtualHost>.

Here and are example IPs that all pointing to ejemplo.es.

If you're trying to access from your host with IP 10.0.x.x, it may be not accessible. Especially if you didn't select the right network type for your VM. You can ping the IP first before checking the local domain work. In my case, I select the bridge connection (with copied NAT), so my VM can act just like any other physical device in my network.

Other thing is, if you have your site which is already on-service, and you want a local environment for developing and testing, it's better to use different domain (such as: .local, .dev) instead of using same domain.

  • First of all, thank you very much for answer! Now, by parts: 1.- Did you do exactly the same steps as me? Do you get the index.html content when you put ejemplo.es in your browser instead of another page (the real on-service page (I know that it is used but I just wanted to make some proves on my local environment)? 2.- I am trying to test the VirtualHost on my Ubuntu client, not in my host. 3.- I do not know what do you mean with virtualhost tag and what to do with the IPS that you put there in that paragraph. – Error404 Feb 28 '16 at 5:58
  • 4.- I am trying to access with the IP as you can see above which is the IP of my Ubuntu client (If I use the command ifconfig) and the network of my VM is NAT (I do not know if it is a correct mode or not). 5.- In this step, do you mean ping to the IP before trying the local domain works? 6.- No, as I put on my other comment, this is not my site, it is just a domain that I want to use on my local environment. – Error404 Feb 28 '16 at 6:01
  • Yes, I did exactly same steps as you did. I can see the content of index.html. – Brian Ng Feb 29 '16 at 2:39
  • The differences here are the network type and IPs. I can use the bound IP to visit the website in host or client. And like @bistoco commented above, you also need to added www.< domain >.es in the hosts file, if you really want to visit your site with www suffix. And sorry for not editing my answer in time, the editor was kind of cutting off that part. – Brian Ng Feb 29 '16 at 2:46

Well kind of unclear without seeing all the configs but few things I can say

  1. First check that NamedVirtualHosting is uncommented in the ports.conf file

  2. change the permissions just to be sure under the /var/www/

    sudo chown -R www-data.www-data /var/www/*

  3. check if the syntax of the symbolic link in sites-enabled folder is correct or if the symbolic link is broken broken links shows in red the syntax goes like this 000-default and whatever comes next goes like 001-test or 002-guest check by

    ll /etc/apache2/sites-enabled/*

Check if the syntax of the ejemplo.es.conf is correct

It looks like this. Make sure the directory is mentioned

enter image description here

I hope this helps good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.