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.

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

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

install apache on linux 2

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

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

- https://cloudkatha.com/how-to-install-apache-web-server-on-amazon-linux-2/

https://www.youtube.com/watch?v=PfKiBcfoh34

Comments

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

Android & iOS Application Development For Dummies

App Development Process App Development Process explained in the most simplified way This tutorial will guide all the beginners how an app is developed from scratch and will help in letting the beginners know how the process works. Phase-1 (Conceptualization): Even a concept or an idea starts with an approach to introduce something after acknowledging the inkling of amiss or what's missing in the existing market. Developing the aftermath ideas involves critical questioning and rational thinking towards the wire-framing stage. Phase-2 (Design):  After analyzing the idea thoroughly and looking through all possible dimensions, comes the design part, where a feasible layout of the model is planned to move ahead for the further development processes. Phase-3 (Coding):  Development process begins with designing a user-friendly UI. Prototypes of the model are prepared with the combined agreement of developers and designers to increase the efficiency and capita

Amazon EC2 Server Setup & Installing JDK 8 and Tomcat 8, Running on Port 80 & 443 and Redirect Request from port 80 to 8080 and 443 to 8443

Amazon EC2 Server Setup & Installing JDK 8 and Tomcat 8, Running on Port 80 & 443 and Redirect Request from port 80 to 8080 and 443 to 8443. Step 1 : Log in to your aws account by following this link then click on my account and choose option aws management console. Note: I am assuming you created your account with aws and you are ready with you account if you haven’t done then you can check out on google you will get many and it's a straight forward steps if you have still problem while creating an account you can comment in comment box i will also provide tutorial for that. Once you logged in aws management console you are able to see window like this one Note : Before we go ahead we have to select proper reason from right and side. I choose ohio region for this example. Step 2 : Now you have to choose EC2 Server from Services tab on left side top corner then choose EC2 Services from “Compute option ” You will get window like this one and r