Skip to main content

How to Install Apache 2 on Linux 2 for Angular Application and How to fix 404 Error in Angular App


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.

  1. Select your instance and click connect as highlighted below
How to Install Apache Web Server on Amazon Linux 2

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

How to Install Apache Web Server on Amazon Linux 2

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.

How to Install Apache Web Server on Amazon Linux 2

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

  1. 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.


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

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

Cick Save Rule to save the 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.

Install Apache on EC2 Linux

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

instal apache on ec2

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>
<h1>Hello World !!</h1>
<p>Welcome to CloudKatha</p>

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.

install apache on linux 2

Congratulation !!!

You have installed and customized apache web server on Linux 2.

Reference -

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 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/apache2/
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/apache2/
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.

sudo zgrep error /var/log/apache2/error.log.2.gz

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] 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.

sudo nano /etc/apache2/apache2.conf

With CentOS and other Red Hat based server use the command here instead.

sudo vi /etc/httpd/conf/httpd.conf

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



Popular posts from this blog

Digital Marketing

What actually is Digital Marketing? This post will help you understand the insights of Digital Marketing What is Digital Marketing? Digital Marketing is an integral part of the overall marketing strategies of any business. It basically covers the advertisement of products/services/business/brand via digital channels. The digital channels could be of any type like websites, search engines, social media, emails, SMS, and MMS. In case if you're using all these digital channels for the marketing, make sure to have all the statistics & workflow of your campaigns via marketing automation. What are the types of digital marketing? Well, there are 6 core digital marketing types: Search Engine Optimization (SEO) : Search Engine Optimization is nothing but a long-term process of improving your website rankings on search engine results pages (SERPs), which in turn has a wide range of tactics & strategies to implement. Although there is no specific method or a  spec

Changing the Time Zone on Amazon Linux Ec2 Instance

Amazon Linux instances are set to the UTC (Coordinated Universal Time) time zone by default, but you may wish to change the time on an instance to the local time or to another time zone in your network. Important These procedures are intended for use with Amazon Linux. For more information about other distributions, see their specific documentation. To change the time zone on an instance Identify the time zone to use on the instance. The  /usr/share/zoneinfo  directory contains a hierarchy of time zone data files. Browse the directory structure at that location to find a file for your time zone. [ec2-user ~]$ ls /usr/share/zoneinfo Africa Chile GB Indian Mideast posixrules US America CST6CDT GB-Eire Iran MST PRC UTC Antarctica Cuba GMT MST7MDT PST8PDT WET Arctic EET GMT0 Israel Navajo right W-SU ... Some of the entries at this location are directo

How do I change the time zone of my Amazon RDS database instance?

As we know bydefault time in the format of UTC in mysql.We can set local time zone to our AWS RDS Instance for our application. or any other time zone prefared Cloud Based Website Hosting Service Provider Steps 1: Go to Services and Select RDS Now to change time zone we have to change "Parameter Group" in left side that is associated with DB instance first we can check default Parameter Group for our instance is Parameter group default.mysql5.7  ( in-sync ) like this. So we have to change the time zone in this Parameter Group.  now open that parameter group (default.mysql5.7)  and click on edit parameter. then search for time_zone (because we want to change it.) then we have to change time_zone only by default it is engine-default (that is utc)  we have to select Asia/Calcutta.  More information we can ref.