How to enable virtual hosting / sites on your vps

Step 1 – register a domain name.
Step 2 – Start by copying the file for the domain:
newusername@yourvps:~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sitename.conf

Open the new file in your editor with root:

newusername@yourvps:~$ sudo nano /etc/apache2/sites-available/sitename.conf

The file will look something like this :

<VirtualHost *:80>

# The ServerName directive sets the request scheme, hostname and port that

# the server uses to identify itself. This is used when creating

# redirection URLs. In the context of virtual hosts, the ServerName

# specifies what hostname must appear in the request's Host: header to

# match this virtual host. For the default virtual host (this file) this

# value is not decisive as it is used as a last resort host regardless.

# However, you must set it for any further virtual host explicitly.

ServerName yourdomainname

ServerAdmin yourname@nomail.com

DocumentRoot /home/newusername/public_html/

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

# error, crit, alert, emerg.

# It is also possible to configure the loglevel for particular

# modules, e.g.

#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are

# enabled or disabled at a global level, it is possible to

# include a line for only one particular virtual host. For example the

# following line enables the CGI configuration for this host only

# after it has been globally disabled with "a2disconf".

#Include conf-available/serve-cgi-bin.conf

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

As you can see, there’s not much here. We will customize the items here for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

ServerAdmin yourname@nomail.com

Uncomment the  ServerName directive, which contains the base domain that should match for this virtual host definition. This will most likely be your domain.

Now that we have created our virtual host file, we must enable it. Apache includes some tools that allow us to do this.

We can use the a2ensite tool to enable each of our sites like this:

newusername@yourvps:~$ sudo a2ensite sitename.conf

When you are finished, you need to reload Apache to make these changes take effect:

newusername@yourvps:~$ sudo service apache2 reload

 

Leave a Reply

Your email address will not be published. Required fields are marked *