How to create virtual host on a AWS EC2 ubuntu instance with apache webserver

If you want to host a website or multiple websites on an AWS EC2 ubuntu with apache then this blog post is for you. In this post I will explain how you can host one or multiple websites on AWS EC2 ubuntu instance. In this article I am assuming you already have installed apache or nginx on aws ec2 instance. Read this post incase you haven’t installed and want to know how to install apache on AWS EC2 ubuntu instance. Also I am assuming you know how to connect to AWS EC2 instance and already connected to EC2 instance with ubuntu user, incase you do not know then read through this post to know how to connect to aws ec2 instance. Through this post you will come to know how to setup virtualhost on AWS EC2 ubuntu instance (14.04/16.04/17.10/18.04) with apache webserver.

Create VirtualHost in Apache:

To create virtualhost in apache either you can copy default configuration file located under /etc/apache2/sites-available or create a entirely new file under this directory in the name of site you want to host (not necessary to keep the name same as site name, but good for identification purpose). The name of this file 000-default.conf.

Copy default file:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/webnlinux.com.conf

Open the new conf file:

sudo nano /etc/apache2/sites-available/webnlinux.com.conf

Make below changes to the file, you can update the document root, server name, alias etc according to your usecase. After making changes to the file, save and exit:

<VirtualHost *:80>
ServerAdmin webmaster@webnlinux.com
ServerName webnlinux.com
ServerAlias www.webnlinux.com
DocumentRoot /var/www/webnlinux.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable Site and Restart Apache2:

sudo a2ensite webnlinux.com.conf
sudo service apache2 restart

That’s it you are done setting up virtual host on AWS EC2 instance with apache webserver. In the similar manner you can setup multiple virtual host and set up multiple sites. If you have already pointed your domains A record on AWS EC2 ip then you can browse your website.

Add a Comment

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