How to host multiple web sites in your VPS

Recently I have received a call from my friend regarding the Virtual Hosts setup. Down below, I scribbled his question for you to read. In summary, all he need is to set up a virtual host on his VPS, so that he can host just one more blog without spending any more on hosting.

Question: I have purchased a Virtual Private Server(VPS) to host my private web site.  I then mapped my domain to the VPS hosting space by making changes in the DNS settings of the domain. I created a new A record with IP addresss provided by the VPS service provider. In the server, I have deployed all the web pages in the root folder of apache (var/www/html) and everything seems working fine. I can reach my site without any problems. However, I also want to set up another blog for my wife but don’t want to spend on hosting. What I decided is to share the VPS. Please show me the steps to set up multiple web sites on a VPS. 

We can host multiple sites on a VPS using apache virtual hosts. In this post I’m gonna show you how exactly I had set up hosting for our two domains in our VPS server.

To follow along this tutorial, I assume you’ve successfully installed apache web server on your server machine. In my box, apache is running its default settings and I deployed the some web pages at the root document folder (/var/www/html). I also made changes in the DNS settings of our domain to point it to the vps hosting.

So, at this point, If I type beta.yourdomain.com, then apache will successfully render all pages to my browser.

Make changes in DNS settings.

As a first step, you need to create a new A record in the Domain’s DNS that points to your VPS server. So if you have two domains, let say beta.yourdomain.com and blog.yourdomain.com, then create two A records

create A record DNS
creating an A record in godaddy DNS

Create Virtual Hosts in VPS

Before we jump into any configuration, we need to create a new directory to keep the new website’s pages. Later on, this new directory will become the Document Root for the new site in the configuration file.

sudo mkdir /var/www/html/blog

After creating the directory, make sure to give the require permission upon it.

sudo chown -R $USER:$USER /var/www/html/blog
sudo chmod -R 755 /var/www

Now create a site file in apache’s available sites configuration folder.  A site file is also called a virtual host configuration file.

sudo nano /ect/apache2/sistes-available/blog.conf

Write the following configuration and save it


<VirtualHost *:80>
   ServerName blog.yourdomain.com

   ServerAdmin webmaster@localhost
   DocumentRoot /var/www/html/blog

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined

   <Directory /var/www/html/blog/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
   </Directory>
</VirtualHost>

Now we have almost done with the settings. In order to see the changes we need to enable the newly added site.

sudo a2ensite blog.conf

After that, reload the apache web server.

sudo service apache2 reload

If you have followed without making any error, then you can reach your blog by typing blog.yourdomain.com

Read More

How to create an alias for SSH

I have found myself in typing out that long ssh command string every time when I need to connect our VPS server is not only boring but also time consuming. If you’re like me, you should also be tired of typing long ssh command strings in your day-to-day work. So, wouldn’t it be nice to have a shorter ssh string to save that extra key strokes?

Let us create an alias for the ssh command to make it shorter.

Fire up your terminal, then execute the following command to create an alias file in your home.

sudo nano ~/.bash_aliases

After creating the file, write our alias command for ssh into it. Make sure to choose an easy to remember name for the alias.

alias sshserver='ssh root@127.0.0.1'

save and close the file

source ~/.bashrc

Now, instead of executing ssh root@127.0.0.1 to make an SSH connection, you can use the alias command sshserver

Read More