In this tutorial I’ll be setting up a virtual host for rastaval.com you can change that domain for your own.
Apache’s Virtual Hosts
The term Virtual Host refers to the practice of running more than one web site (such as company1.rastaval.com and company2.rastaval.com) on a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every web site, or “name-based”, meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.
In order for you to follow the whole tutorial, you must have a root access or a sudoer account to the server.
Assuming you have a fresh installed machine that has no apache2 installed on it. To do so, use the following command:
sudo apt-get install apache2
1. Creating the Directory
Create the directory where our webfiles will reside. This working directory will be our “Document Root” for the Apache Virtual Configuration. In creating a directory with your ssh command we have to add -p option so it would automatically create including the parents of the new directory.
Note: Please make sure that the DNS of the domain or its IP address is pointing and to the server where you are setting up the Virtual Host. (rastaval.com’s ip address is already pointing to the server where i wish the site to be hosted.)
sudo mkdir -p /var/www/rastaval.com/www_data
Note: If you want to use an unapproved domain name to test the process, you may refer to my XAMPP-APACHE virtual host configuration.
2. Grant apache the ownership to the Document Root.
sudo chown -R www-data:www-data /var/www/rastaval.com/www_data
Next, make the directory readable to everyone/public.
sudo chmod -R 755 /var/www
chmod with option -R means you are granting READ permission to files and directories recursively
3. Create your test page with a valid starting filename.
index.html is commonly used as a starting page, you should put this file inside your “Document Root” directory.
sudo nano /var/www/rastaval.com/www_data/index.html
Here is a basic HTML code for your starting page.
<h1>It should Work!</h1>
Save & Exit.
4. Create a new Virtual Host File
You may duplicate or copy the default V-Host file and change its filename for your domain.
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/rastaval.com
5. Enable Virtual Hosts
Edit the newly created V-Host file.
sudo nano /etc/apache2/sites-available/rastaval.com
It will be the Virtual Host of our domain.
To begin, insert a line for the ServerName and ServerAlias under the ServerAdmin line.
The ServerName specifies the domain name that the virtual host uses.
Setting up the ServerAlias would make our site accessible with or without www.
It should now looks like this:
The next step is to specify the path of the directory where rastaval.com will get its web data. Misconfigured path of your “Document Root” will never let the Apache Server Pull up the webfiles.
The correct path of our defined “Document Root” looks like this:
Save and Exit.
Finally we are now ready to activate our Virtual Host with the following command:
sudo a2ensite rastaval.com
6. Restart our Web Server
Modification of the configuration will only take effect after Apache is completely restarted.
sudo service apache2 restart
Defining our domain name to our server’s HOSTS file is no longer necessary since the IP of our domain resolves to our server’s IP.
7. Checking our work.
Access our domain, rastaval.com from a different computer. To double check, you may also type into the browser it’s ip. (Example: http://22.214.171.124/)
The previously-made index.html should be parsed or be accessed in either way.
You should see this in our browser:
It should Work!!
We can host multiple sites, with multiple domains on a server with single IP address. All we have to do is to segregate every domain to its corresponding “Document Root” and simply repeat this procedure above.