Apache Web Server on AWS -How to launch ?

This tutorial exploains, how to launch Apache Web Server on AWS.

In this tutorial , we will launch an EC2(Elastic Cloud Compute) instance on AWS and configure the Apache httpd on it. EC2 is nothing but a Raw Virtual Machine on AWS where you have to install your services.This is tutorial is beginner friendly and provides basic configuration of Apache and also provides Information about Virtual Host.

Read About : 21 Important AWS Services that you must know

&& What is DNS and How DNS works

 

How to Launch Apache Web Server on AWS ?

Apache is a very well known open-source Web Server. It is not only popular but also very old Web Server. Like any other Web Servers, Apache also accepts requests from the clients, search for the requested queries and then send the response back to them.

Launching EC2 Instance on AWS

Login to your AWS Account. If you do not have an AWS account, you can create a free account on AWS and use it free for one year. Follow the link following link to create a free account on AWS.

https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc

 

Click on Services and navigate to EC2

Click on Launch Instance

Select  Amazon Machine Image(AMI) to launch your Linux box. I would recommend choosing Amazon Linux 2 AMI.

Select an Instance Type. I am choosing t2.micro(Free tier eligible) here. You can use a Free tier Instance,750 hours free for each month for the first 12 months.

Read more about free tier usage on AWS official website.

https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/free-tier-limits.html

 

In the Configure Instance Tab, select Network, Subnet and keep ” Auto-assign Public IP”  enable to assign a Public IP. Keep other settings as it is.

In the add storage tab enter the size of the Volume.

Add Tag

Put  “Name” as key and  “Apache-Server” as value. Tag is basically a Name that appears in the Instance tab in EC2 Section.

 

Configure your Security Group

Security Group is basically a Firewall for your AWS Network. I am opening port 22 for everyone to access this Server. You can put your IP to allow it from your location only.

Review your settings to launch the Server.

Create a new key pair and download it. In the below screenshot, I created a key pair named apache-server. Make sure to download the key pair and save it to your drive because it is the only key for login into your server. If you lost this key you will not be able to log in to your server.

As soon as you click on the launch, your Server will be launched. Navigate to the Instance Tab in EC2 Section and search for your Server. Note down the Public IP so as to connect it from the Internet.

 

Apache Web Server Installation

Now Connect Apache-Server from the key you downloaded using Public IP from your local Linux Machine.

 $ sudo ssh -i  server-key [email protected]

Switch to Root Account for Installation.

$ sudo su

Update your Linux Box

$ yum update -y

Install Apache-Server

 $ yum install httpd -y 

Start httpd service

 $ service httpd start

check the httpd service status

$ service httpd status

Run chkconfig command to run the httpd service automatically after a system reboot

$ chkconfig httpd on

Now your Apache-Server is installed and ready.

To browse Apache Web Server from the Internet, open port 80 and 443 in the security group

Navigate to EC2—>Instances—> Search for your Web Server—-> Click on the Security group as shown in the square box in the below screenshot.

 

Configuring Apache Server

Let us understand the Apache Server step by step from basic to advanced configuration.

As soon as your installation is done and port is opened in the security group, without doing any configuration change just enter the public IP in the web browser of any computer. You will see the default page as shown below.

 

 

To access the server from your domain name, create A record for your web server in your DNS zone configuration.

I have created A record for my domain as follows:

devopsmyway.in ————-> IP Address of my Server.

Now I can browse the apache Server from my domain name devopsmyway.in. The same test page will come as I did not change any configuration yet.

 

Let’s do some basic changes to open your Web Server (Web Site)  as per your configuration.

Create an index.html file in “/var/www/html” directory and write some content in this to serve in the web browser. I am using echo command here to create and write content in index.html.

  $ echo “Hello , Welcome to Devopsmyway.in ” > /var/www/html/index.html

 

 

As soon as you create and write content in index.html file in Document Directory “/var/www/html” your website will start serving the content written in index.html.

Now we have done the basic configuration of Apache Web Server. Let us move ahead and learn some advanced settings.

Virtual Host

Virtual host comes into picture when you want to host multiple Websites on a Single Server. Virtual host are of two types:

  1. Name-based virtual host
  2. IP based virtual host

Name-based Virtual Host

Name-based Virtual Host is used to configure multiple websites on a Single Server having a single IP Address. To configure Name-based Virtual hosts we need to do configuration changes in Apache Configuration file.

Apache Configuration file :  /etc/httpd/conf/httpd.conf

Let us configure two websites www.devopsmyway.in and www.devopsmyway.net on the same Server with same IP address. To do so , open /etc/http/conf/httpd.conf and add the following lines at the bottom of the file.

$ vi /etc/httpd/conf/httpd.conf

<VirtualHost 172.31.22.60:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/devopsmyway.in
ServerName www.devopsmyway.in
ErrorLog logs/www.devopsmywa.in-error_log
CustomLog logs/www.devopsmyway.in-access_log common
</VirtualHost>

<VirtualHost 172.31.22.60:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/devopsmyway.net
ServerName www.devopsmyway.net
ErrorLog logs/www.devopsmyway.net-error_log
CustomLog logs/www.devopsmyway.net-access_log common
</VirtualHost>

Now Create to Directories as follows:

 $ mkdir -p /var/www/html/devopmyway.net

 $ mkdir -p /var/www/html/devopmyway.in

Create index.html file in each folder and add some content in it.

$ echo “Hello , Welcome to Devopsmyway.in ” > /var/www/html/devopsmyway.in/index.html

 $ echo “Hello , Welcome to Devopsmyway.net ” > /var/www/html/devopsmyway.net/index.html

Now check the configuration and restart the httpd service

 $ httpd -t

 $ service httpd restart

Note: If you donot have two websites in Public DNS , you can do host entry in /etc/hosts on the same server as follows for testing

Now you will able to browse both the URL.

IP based virtual host

IP bases virtual host is used to configure multiple websites on a Single Server with multiple IP Addresses. To configure IP-based Virtual hosts we need to do the following configuration changes in the Apache configuration file.

Let us configure two websites www.devopsmyway.in and www.devopsmyway.net on the same Server on two IP addresses. To do so , open /etc/http/conf/httpd.conf and add the following lines at the bottom of the file.

<VirtualHost 172.31.22.60:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/devopsmyway.in
ServerName www.devopsmyway.in
ErrorLog logs/www.devopsmywa.in-error_log
CustomLog logs/www.devopsmyway.in-access_log common
</VirtualHost>

<VirtualHost 172.31.27.122:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/devopsmyway.net
ServerName www.devopsmyway.net
ErrorLog logs/www.devopsmyway.net-error_log
CustomLog logs/www.devopsmyway.net-access_log common
</VirtualHost>

Again check your configuration, restart the httpd service and browse both the sites using the curl command.

I hope you enjoyed this tutorial and learned to launch Apache Web Server on AWS . If you think this is really helpful, please do share my website https://devopsmyway.com to others as well. I will continue for the tutorial for Apache in my next blog. Also, please give your valuable feedback in the comment box.

 

If you think we helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

You may also like…

Leave a Reply

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