Step 1: Launch an EC2 Instance(Linux 2)
Before we install apache web server on the EC2 instance, we will need an EC2 instance up and running. You can refer my previous tutorial to launch a Linux 2 instance in AWS.
Link to tutorial: How to Launch EC2 Instance Step by Step in AWS
Step 2: Connect to your Linux 2 instance
Once your instance is up and running, you need to connect to your instance. I will be using EC2 instance connect feature for this as it allows to SSH into instance from browser itself.
However, if you want to usual SSH, feel free to use below command from your terminal.
ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip
To SSH from browser using Instance connect, follow below steps.
- Select your instance and click connect as highlighted below

2. Once, you click Connect, you will see a screen with default username for a Linux 2 instance

verify that ec2-user is showing in username field and click Connect.
A new browser window will open and you will be connected into your instance like below.

Amazing !!!
Now you are ready to run commands on your EC2 instance. Let’s move to the installation part in next step
Step 3: Install Apache Web Server
Now, we need to run below set of commands one by one, to install apache web server on our instance. Please note that we will use sudo(root privilege ) to run all these command. The reason is, whenever you try to install, remove or change any software, you must have root privilege to do such tasks.
sudo yum update -y
sudo yum install -y httpd.x86_64
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
Explanation of commands
- Update latest package available on the system
It’s a best practice to update al the packages to latest before installing anything new.
sudo yum update -y
2. Install Apache Web Server
sudo yum install -y httpd.x86_64
3. Start Apache Server
Above command will install apache web server but it will not start it. You need to explicitly start the server using below command
sudo systemctl start httpd.service
4. Configure Apache to run on system boot
It is your web server and you always would like it to auto start on system boot. Isn’t it?
You can do so by below command
sudo systemctl enable httpd.service
At this moment, your apache web server is already installed and started in your Linux 2 instance. But, you can’t access it right now. if you try to hit the public IP you will get error like This site can’t be reached.
Why?
Because, our EC2 instance doesn’t allow web traffic yet.
On the security group, only SSH is allowed as of now.
Let’s change that.
Step 4: Change Security Group of instance to allow port 80 and 443
Let’s allow web traffic on port 80 and 443(Internet traffic for http and https).
Click on your instance Id to see the instance details. Scroll down and click on Security Tab and you should see security group like below.

Click on Security Group id link -> Click on Edit Inbound Rule

Use Add rule button to add more rule one by one.
Specify rules for HTTP and HTTPS Web traffic from anywhere like above.
Step 5: Verify the Installation
We have installed and started the apache web server.
Our instance allows web traffic now, it’s time to grab the public IP or public dns of the instance.

Click on open address and you should be able to see the tomcat default page like below.
Note: You can see change in IP in above and below screen. The reason is, I stopped and started my instance so public IP changed. For you both of this should remain same.
Feel free to ask me in comment if you have questions.

Step 6: Customize the web page
We have seen the default apache page served by apache. Lets modify that.
We will create our own index.html in the Document Root folder which is /var/www/html in our case.
DocumentRoot: Document Root is the directory from which apache looks for and serves web files on your request. So we will create an index.html in /var/www/html folder
You can use various command to create and put content into index.html inside folder. You can check this tutorial
For example- one of the simplest option is to use:
sudo echo “Hello World from $(hostname -f)” > /var/www/html/index.html
But ‘>’ creates a problem and the the above command ends up running as ec2-user instead of root resulting in permission denied like below.
-bash: /var/www/html/index.html: Permission denied

It will work if you switch your user as root using sudo su or sudo sh but I wanted to avoid that.
You might ask me, why?
Well, it is advisable to stick to sudo
when performing tasks that require root privileges. By doing so, the current user is only granted privileged for the specified command. On the other hand, su
switches to the root user completely and every commnad runs as root which is not secured.
What worked for me?
I used sudo nano command to create the file and put content into it.
sudo nano /var/www/html/index.html
Once, file opens up, paste below content into the file
<!DOCTYPE html>
<html>
<body>
<h1>Hello World !!</h1>
<p>Welcome to CloudKatha</p>
</body>
</html>
Save and close the file.
Step 7: View the customized web page
This time when you enter public ip or dns into the browser, you will see your customized page like below.

Congratulation !!!
You have installed and customized apache web server on Linux 2.
Reference - https://upcloud.com/resources/tutorials/fix-common-problems-apache2
If your server uses a custom virtual host configuration, like when hosting multiple websites on one server, check that each virtual host file has the correct domain name and points to the correct root directory. On Debian and Ubuntu machines have a virtual host file by default and it is stored in /etc/apache2/sites-enabled/. Open the file for edit.sudo nano /etc/apache2/sites-enabled/000-default.conf
This file usually has some instructions on what each parameter means in the comments, which have been left out in the example below, but the important parts are ServerName and DocumentRoot as already mentioned.<VirtualHost *:80> ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined #Include conf-available/serve-cgi-bin.conf </VirtualHost>
CentOS and httpd do not have the same virtual host file set by default but instead uses the httpd service configuration to store the default settings. Check the configuration file with the command below.sudo vi /etc/httpd/conf/httpd.conf
Look for the same parameters ServerName and DocumentRoot, and make sure they are correctly set.
If you made any changes to the configuration files, the service needs to be reloaded for the changes to take an effect. Restarting the service does the job, but if you wish to avoid downtime on your web server use reload instead with one of the following commands.sudo systemctl reload apache2
sudo systemctl reload httpd
Check Logs
When everything on the service side is working as expected and you cannot find a fault, but the website just still won’t load, it’s always a good time to dig through logs. Apache2 keeps two sets of logs, access and error. You can find the logs stored at /var/log/apache2/ or /var/log/httpd depending on your choice of Linux distribution. You can list all files in your web server’s log directory using the commands below.
sudo ls /var/log/httpd/
The log lists will differ slightly as different systems name the logs a little differently. Ubuntu and Debian servers store the current uptime logs to access.log or error.log, previous logs are marked with a running number, 1 being the latest, and older logs than that are also compressed. On CentOS and other Red Hat variants, the logs are named access_log and error_log, older logs have their name appended with the date the log was written on e.g. access_log-20150108.
An easy way to start reading the logs, when you don’t necessarily know what you are looking for, is to use the filtering app ‘grep’. Search for any errors using one of the commands below which corresponds with your system’s web application name.
sudo grep -i -r error /var/log/httpd/
Ubuntu and Debian users might also need to check through the compressed log files. This can be done using ‘zgrep’ instead, but due to its limitations, you can only search one log at the time, for example using this following command.
Not all errors logged by your web service necessarily mean there is something wrong with your server, but keep an eye out for any repeating problems like missing files in the example error below.[Fri Jul 17 12:36:08.431065 2015] [:error] [pid 4649] [client 80.69.160.92] script '/var/www/html/index.php' not found or unable to start
You may also wish to enable more verbose logging if searching for errors is not yielding any results. The log output amount is controlled by a parameter called ‘LogLevel’ in the configuration file. On Debian and Ubuntu systems, open your configuration file with the following command.
With CentOS and other Red Hat based server use the command here instead.
Find the LogLevel parameter and change it from the default ‘warn’ value to ‘debug’ instead, then save the file and exit. Again for any changes to be applied, the service needs to be reloaded. You can do this with one of the following commands appropriate for your system.
sudo systemctl reload httpd
To generate some new entries using the new log levels, try to access your website again a couple of times even if it doesn’t work. The more verbose logs should help with narrowing down any underlying issues.
Credit Goes to or ref
- https://cloudkatha.com/how-to-install-apache-web-server-on-amazon-linux-2/
Comments
Post a Comment