Introduction
If you have set up a website on a VPS you may want the users to access your website via domain name only.
Let's say your domain name is
example.com
and the IP address of your server is 123.123.123.123
.You may want the user to be able to see your website by typing
example.com
on the URL bar of the browser, but not to do that by typing your IP address.If you just install apache on your machine and add your website content, infact, someone could see your website by generically requring an HTTP connection to your server.
This can be avoided by setting up some virtual hosting configuration.
Why should you want that?
Here's a few reasons:
- Because you don't want people to know. Maybe you used your VPS for other services not related to your website, and so you don't want people to snoop on your website by only knowing your IP address or another domain name associated with it.
- Because it's not elegant. Honestly, it's not really good looking to browse a website with an IP address on the URL bar :\ .
- Because it's more correct. Domain names and IPs don't have a 1-1 relationship. A
web server may have many different domain names because a web host is
different from a web server. The two entities are on a different
abstraction level.
When you want people to come and visit your website you want them to access the website associated with the domain nameexample.com
. So you want them to get in contact with the host, and not with the whole server.
How to - apache2
For doing that you have to edit the configuration file
/etc/apache2/sites-enabled/000-default.conf
Paste this portion of text at the beginning
<VirtualHost *:80>
ServerName 123.123.123.123
DocumentRoot /var/www/foo
</VirtualHost>
ServerName 123.123.123.123
DocumentRoot /var/www/foo
</VirtualHost>
The resulting file should look something like this
<VirtualHost *:80>
ServerName 123.123.123.123
DocumentRoot /var/www/default
</VirtualHost>
<VirtualHost *:80>
ServerName exemple.com
DocumentRoot /var/www/exemple.com
</VirtualHost>
ServerName 123.123.123.123
DocumentRoot /var/www/default
</VirtualHost>
<VirtualHost *:80>
ServerName exemple.com
DocumentRoot /var/www/exemple.com
</VirtualHost>
Remember to restart apache2 by typing
sudo service apache2 restart
.By doing so when a browser requires a document to your web server to the host
123.123.123.123
or another domain pointing to your VPS's IP address it will get the content from the directory /var/www/foo
.You can place there a placeholder or a different website content as you prefer.
If the HTTP request received by your server is sent to a host that is not specified in any other
VirtualHost
declaration apache will choose this virtual host because it's the first of the list.Instead, if the HTTP request specifies the host
exemple.com
apache2 will use the content of
/var/www/exemple.com
You may want instead to redirect the user who connects without providing the domain name to your website.
In that case add this text at the beginning of
/etc/apache2/sites-enabled/000-default.conf
instead of just providing a different website content.
<VirtualHost *:80>
ServerName 123.123.123.123
Redirect permanent / http://www.foo.com/
</VirtualHost>
ServerName 123.123.123.123
Redirect permanent / http://www.foo.com/
</VirtualHost>
And restart apache as before.
In this case in a situation as the one mentioned before the user will be redirected to
http://www.foo.com
.How to - nginx
If you use nginx and you want to return a different content for those who don't provide the domain name you have to edit the configuration file
/etc/nginx/sites-available/default
and paste this portion of text at the beginning
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/default;
index index.html index.htm;
}
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/default;
index index.html index.htm;
}
And restart nginx by typing
sudo service nginx restart
So if your VPS receives a HTTP connection with a host name that is not spcified in any other
server
tag of the configuration file, nginx will give back the content of the directory
/var/www/default