Aws Apache Web Server



Apache has been the most popular web server on the Internet Apache HTTP Server httpd was launched in 1995 and it has been the most popular web server on the Internet since April 1996.Latest release from the 2.4.x stable branch represents the best available version of Apache HTTP Server. Install the Apache web server with PHP and MySQL support on your EC2 instance. AWS Documentation Amazon EC2 User Guide for Linux Instances Step 1: Prepare the LAMP server Step 2: Test your Lamp server Step 3: Secure the database server Step 4: (Optional) Install phpMyAdmin Troubleshoot Related topics.

Hello Fiends, In this article I will help you to set up Apache server on EC2 Linux instance. I will cover the details in step by step. And this will help you to set up your own Apache server. I have divided this article in two sections mentioned below:

  • Spinning up a Linux EC2 instance
  • Configuring the EC2 instance to host Apache server.

Spinning up a Linux EC2 instance:

  1. Login to your aws console preferably using an IAM user. Please never use a root account in real time scenarios to login and create new services.
  2. Go the Services tab in the top left corner in the AWS console. Select EC2 under compute section.
  3. Once you are on EC2 Dashboard, Click on the Running instance link. Under the running instance slink you can see that there are no instances running if you are logging for the first time in the console.
  4. Going forward on the top of the screen click Launch Instances.
  5. Now you should be able to see the screen as Step 1: Choose an Amazon Machine Image(AMI).
  6. Select any one of the AMI from the available list. Make sure that the AMI you select is Free tier eligible for not getting billed. I have selected the one shown in the figure below.
  7. Now choose the instance type. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications. Here I have selected t2. micro which is again free tier eligible. We get 750 hours of usage free of cost within the first one year of creation of the AWS account.
  8. Once you have selected the instance type. Click Next: Configure Instance Details at the right bottom of the page. In this screen you have to select the VPC (VPC is a virtual data center created for you to host your resources. It separates your resources from other resources in the cloud). I have already written post on how to create a public subnet in a VPC in one of my post : https://www.dotnetforall.com/creating-your-first-aws-vpc-for-publicly-accessible-resource-on-internet/.

    As you can see in the figure above, the VPC I used it already created by me in previous post. And finally the subnet used is the public subnet.

  9. In the “Add Storage” screen keep the default options.
  10. In the “Add Tags” add the Key as “Name” and Value as “WebDMZ”.
  11. Next in the “Configure Security Group” screen, select the “Add Security Group” option. Here we will create a new security group which allows SSH and HTTP traffic from the web.

    Though it is not recommended to allow SSH from the 0.0.0.0/0 IP address range as it opens a vulnerability for the EC2 instance. Notably ours is a demo instance that is I allowed here.

  12. Finally Click “Review and Launch” and “Launch”. Eventually we are in the “Select Key pair screen” to select the key pair. In one of my previous posts I have mentioned how to create a key pair and hence login to Linux instance from windows : https://www.dotnetforall.com/how-to-login-to-aws-ec2-linux-instance-from-windows-using-putty/ . Accordingly I have selected a key pair which I had already crated.

AWS Cloud EC2 Linux Instance with Apache

  1. Once you are able to login to EC2 instance run the command “sudo su” to execute further commands as root user.
  2. Thereupon execute the below commands one by one.

sudo yum update (to update the EC2 instance with latest updates)

yum install httpd -y (to install the apache service)

service httpd start (to start the apache service)

chkconfig httpd on (always start the apache service on restart of instance)

service httpd status (to check the status of service)

Creating My First Web Application On Apache Server

  1. Once you done with above steps. Go to the folder “/var/www/html” as shown in the below figure.
  2. Use “ls” commands to see the contents of the folder. There are no files present in the folder currently. Furthermore use “nano index.html” to create a new file in the above folder.
  3. Once you done with creating of the file you can browse the file from any where in the world using the public IP of the aws ec2 instance.

Conclusion:

In this article I have configured an AWS Cloud EC2 Linux Instance with Apache and installed my first web application. Moreover it was a simple single page hello world example but it can definitely help someone who is struggling to do the initial setup. Once you can successfully do the necessary setup, consequently it is more easier to try more complicated things.

My Learning Resource

Excel your system design interview

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.

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.

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

Server

Aws Apache Web Server Download

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:-

Apache Web Server Linux

Connect to us: Facebook | Twitter