So let's create a virtual host
and I'm going to explain this
as I do it.
In the previous video when we deployed our meteor application
we created a virtual host for it
and
so this is kind of a follow up to show you how
a basic
virtual host configuration would look
because with a meteor virtual host we
had to omit certain information that would normally be
in a static website's virtual host and we had to add
stuff in.
So let's go ahead and get to
creating a virtual host. So I'm gonna open up terminal here.
I'm gonna change directory first into
my html directory
and I'm gonna create a new directory called site1
and
another one called site2
and if I
list the directories, we now see that I have a site1 and site2.
In each one of these I'm gonna put an html file.
So,
nano
let's
./site1/index.html
and in here I'm just going to type, This is
our first site,
located
at site1
woah
.localhost.
Ctrl O and ctrl X and I'm gonna do the same thing
in the site2 directory
and I'm just gonna
close that
and we're not gonna use any
html mark up or anything, this is just gonna be
to
show you guys
how it's gonna work.
So, let's go over to our Apache's directory so
that's in the etc directory
apache2
and
I'm gonna create two files in here
in the sites-available directory
and
so, the first one I'm gonna create is
sites-available
site1.conf
and so what we need to do is open up virtual
host
and
I
missed the capital V
so
<VirtualHost
at any web address on port 80,
this is gonna listen to
to try and find
power,
site in here.
So the first thing we need is a server name and this is going to act as
the URL
or the domain.
So, if I was gonna point example.com to this server,
to the IP address of this server
and then once
a request
say if I go into a browser and go to example.com,
what happens would be that the request reaches the server
and then it parses all these enabled websites
to get
further information about where the site is stored and even
you know if you're using modules for you can do that in
a virtual host as well.
For right now it's just going to be site1.localhost
and then, we set a DocumentRoot
and this is an absolute
file path
into our
documents root. So in this case, it's in our html directory and it's at
a sub directory called site1
and
I'm gonna
Ctrl O to save this.
Ctrl.
Actually, oh
I gotta do this at sudo.
So,
let's run that
previous command with administrator privileges.
I'm gonna write it out again so <VirtualHost
at any web address on port 80.
and then we have to close it.
The first one is server name that's going to tell it basically
the name of the site.
Where the domain or sub domain is located at
or the sub domain we want to use for it
and then the DocumentRoot
is where the files are located.
And then I can just Ctrl O and Ctrl X
and then I'm gonna do the same thing for
site2,
so sudo nano ./sites-available/site2.conf.
So, as you can see it's all the same.
The only difference is gonna be that we're gonna reference
site2
in both the ServerName
and
the
DocumentRoot.
This is gonna be at /var/www/html/site2.
So, I'm gonna save this and exit
and we actually need to go into the hosts file
to create that internal route,
to
come to local host and so what we need to do is
run sudo nano hosts
and that is in the etc directory
but I'm in the Apache directory so
sudo nano
one directory up
and a file called hosts.
So you'll see the app.localhost which we created in the last
video,
and this is gonna be site1.localhost
and
site2.localhost.
And so the hosts file,
on a Linux system
is
basically just a way to
create your own
records of redirection
and what not and
you know different hosts to resolve
by different
domains or
URLs.
So I'm gonna save this
and exit
and I'm going to
start the Apache service.
Actually, what I've gotta do is
sudo a2ensite, we've gotta enable the site.
And then we need to restart the service so,
systemctl restart apache2.
If I open up a web browser,
we should be able to see
the different
routes actually in effect.
So,
let's go to http://site1.localhost
and it says "This is our first site, located at site1.localhost"
and if I change this to site2.localhost,
you'll see that is a second virtual host site.
And so, if I were to go into,
where is that,
var www html,
here you'll see what we've got going on and so you can have
as many websites in here as your
disk space permits
and you can serve up each at its own
domain name.
So that was
a brief
video about what a virtual host is and does.
I hope you guys found it informative.