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 myVirtualHost
folder will be displayed and not the page that it is onwww.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:
<VirtualHost 10.0.2.15:80>
ServerAdmin webmaster@ejemplo.es
ServerName ejemplo.es
ServerAlias www.ejemplo.es
DocumentRoot /var/www/extras/ejemplo.es
</VirtualHost>
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
):
10.0.2.15 ejemplo.es
so now the file has three lines with IP's:
127.0.0.1 localhost
127.0.1.1 user
10.0.2.15 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!
ServerName
of my VirtualHost isejemplo.es
,www.ejemplo.es
it is theServerAlias
. – Error404 Feb 28 '16 at 6:04www.ejemplo.es
to your vm, adding the line10.0.2.15 www.ejemplo.es
on/etc/hosts
– bistoco Feb 28 '16 at 6:10