Table of Contents
Apache Virtual Host Setup
This tutorial is answer to the question: `How to setup domain on Virtual Private Server (VPS) using ubuntu? `, meaning we can setup multiple domains with single IP address using Apache Virtual Host.
Note: I’m going to consider you have your VPS or local system is ready with Ubuntu to use this tutorial.
Let’s Get started.
Before start we need to have Apache installed on your Ubuntu Server, if you haven’t installed Apache, please install by using following command:
sudo apt-get install apache2
After getting Apache installed, we need to setup a directory for your project/website.
This is the directory where we are going to store our website data or we called it as Production Code, so go ahead and use following command to create a directory.
sudo mkdir -p /var/www/yourdomain.com/public_html
You can replace ‘yourdomain.com’ to your real domain name, please take a note whatever domain you’re going to use the domain needs to have DNS forwarded to your server IP address. if you don’t have domain and you’re testing this tutorial on local system no worries you will get more info on this as well in the next steps.
We have to set permissions to the user along with the root user, follow below command to setup directory permissions, and also to all users to give read access.
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html
sudo chmod -R 755 /var/www
Now we are ready to setup Apache virtual host.
Virtual host file:
Apache provides default virtual file called `000-default.conf` which is located at `/etc/apache2/sites-available/` so the easy way to create new virtual file is just copy this default file with your domain name or whatever you want but it’s better to keep domain to identify files.
Use following lines to create duplicate/copy file of `000-default.conf`
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yourdomain.com.conf
We have our file ready to configure, let’s do that in next step
Configure Virtual host:
Edit the file we just created:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
Change `#ServerName www.example.com` to `ServerName yourdomain.com`
If you need your site to be accessible with `www` add below lines after ServerName
ServerName yourdomain.com ServerAlias www.yourdomain.com
Assign our website root directory which we had setup in first two steps
Finally `yourdomain.com.conf` file needs to look like this:
ServerAdmin firstname.lastname@example.org ServerName yourdomain.com ServerAlias www.yourdomain.com DocumentRoot /var/www/yourdomain.com/public_html
We are done with the configuration, let’s enable our virtual host:
sudo a2ensite yourdomain.com
As we have made changes in the configuration of Apache, we need to restart our Apache to complete the configuration.
sudo service apache2 restart
We are done, just needs to add test page in the root directory so that we can see message/page, or you can easily upload your website backup if you have already in place.
Let’s create `index page` to show message.
sudo nano /var/www/yourdomain.com/public_html/index.html
Add following content it the file
<html> <head> <title>Test Domain</title> </head> <body> <h1>Great, yourdomain.com is successfully setup on VPS!!</h1> </body> </html>
If you are using this tutorial to setup virtual domain on your local system, use following additional step to access your domain, you just simply needs to add records under host file.
127.0.0.1 localhost #virtual host 220.127.116.119 yourdomain.com
We are all done you can test this setting on local system by using `yourdomain.com`.
If you find this tutorial useful, please share and let me know your feedback by using comment section below.