
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 have’t installed Apache, please install by using following command:
Install Apache:
1 |
sudo apt-get install apache2 |
Create Directory
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.
1 |
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 your going to use the domain needs to have DNS forwarded to your server IP address. if you don’t have domain and your testing this tutorial on local system no worries you will get more info on this as well in the next steps.
Directory Permissions
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.
1 |
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html |
1 |
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
1 |
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:
1 |
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
1 2 |
ServerName yourdomain.com ServerAlias www.yourdomain.com |
Assign our website root directory which we had setup in first two steps
1 |
DocumentRoot /var/www/yourdomain.com/public_html |
Finally yourdomain.com.conf
file needs to look like this:
1 2 3 4 |
ServerAdmin webmaster@yourdomain.com 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:
1 |
sudo a2ensite yourdomain.com |
As we have made changes in the configuration of apache we need to restart our apache to complete the configuration.
1 |
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.
1 |
sudo nano /var/www/yourdomain.com/public_html/index.html |
Add following content it the file
1 2 3 4 5 6 7 8 |
<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.
1 |
nano /etc/hosts |
1 2 3 |
127.0.0.1 localhost #virtual host 12.34.56.789 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.