Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 6: Web Servers — Nginx and Apache

Hello, friends! So far we have launched an EC2 server and logged in to it. But a server alone does not show a website – for that you need a web server. In this chapter we will install Nginx and Apache, get to know their config files and learn the commands you need every day. The example stays the same: the order website of a dairy brand like Amul (sample data, for practice) – today we start the first web server on its EC2. Open the terminal and type every command with me.

What you will learn in this chapter

  • What a web server does
  • HTTP status codes
  • Nginx vs Apache
  • Package, service, paths per OS
  • Install on AL2023, Ubuntu, CentOS
  • firewalld and SELinux basics
  • Reading the config files
  • Everyday management commands

Concepts in this chapter

  1. 6.1What is a web server?
  2. 6.2Nginx vs Apache
  3. 6.3Quick reference: package, service, paths per OS
  4. 6.4Amazon Linux 2023
  5. 6.5Ubuntu 22.04 / 24.04
  6. 6.6CentOS Stream 9
  7. 6.7Understanding the configuration files
  8. 6.8Everyday management commands

Review and practice

  1. ★Common Mistakes I See Students Make
  2. ★Interview Questions
  3. ★Practice Questions and Lab Exercises

Before you start: where web servers fit

Almost every website you use passes through a web server. When you open amazon.in, irctc.co.in or your college result page, the first program that answers is usually nginx or Apache, or a cloud load balancer in front of them. The web server serves files, adds HTTPS and passes dynamic requests to an application server: PHP-FPM for PHP, Gunicorn for Python, PM2 for Node.js, Tomcat for Java or Kestrel for .NET.

In this chapter you will install nginx and Apache on Amazon Linux 2023, Ubuntu and CentOS Stream 9, learn where their files live, and practise the safe daily routine: test, reload, read the logs. Every later chapter (static hosting, dynamic sites, WordPress, load balancing) builds on this one, so go slowly and type each command yourself.

Request pathBrowsernginx / ApacheApp server(FPM, Gunicorn, PM2,Tomcat, Kestrel)Database
The web server is the front door. It answers static files itself and passes everything else to the app server behind it.

Real-world picture. Large sites put a CDN and a load balancer (such as AWS CloudFront and an Application Load Balancer) in front of many nginx servers. On a small EC2 server, you do all of this with one nginx. The ideas are the same at both sizes.

Real examples you will relate to

Indian company: IRCTC's ticket site gets huge spikes at tatkal booking time. A well-tuned web server layer (with caching and load balancing in front) is part of handling that. Global: Netflix has written publicly about using nginx in its content delivery. Everyday: a railway enquiry counter answers simple questions itself and sends complex cases to the right office. That is exactly what a reverse proxy does.

After this part you will be able to

  1. Install and start nginx or Apache on three Linux distributions.
  2. Find the config, web root and log paths quickly.
  3. Test, reload and verify changes safely.
  4. Explain 403, 404, 502 and 504 with one cause each.

Key terms

  • Web server: Serves files and proxies apps: nginx, Apache.
  • Application server: Runs code: PHP-FPM, Gunicorn, PM2, Tomcat, Kestrel.
  • Reverse proxy: Receives requests and forwards them to an app.
  • Status 403: Forbidden: permissions, SELinux or no index file.
  • Status 502: Bad gateway: the app behind the proxy is down.
  • SELinux: Mandatory access control on RHEL-family systems.

Mini case study

Suppose we are a junior cloud engineer at a Bengaluru e-commerce startup (sample data). The team runs a PHP admin, a Node.js storefront API and a Python report job on two EC2 instances. Using this chapter, you write a one-page runbook: which service listens where, how to test and reload each one, which logs to read, and which ports are public (only 80 and 443, plus 22 from the office IP). New team members can then fix most problems on their own.

Practice Tasks

About this scenario

Mumbai Dabba Express is a food delivery business in Mumbai. Tukaram Gaikwad looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy Restart a service after a change

Why: Config changes only apply after the service restarts or reloads.

What to do: Restart the nginx service.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check if a service is running

Why: When a site is down, first check whether the web server process is running.

What to do: Show the status of the httpd service.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Install a package on Ubuntu

Why: Ubuntu uses apt. Package names differ: on Ubuntu Apache is apache2.

What to do: Install the apache2 package on Ubuntu without being asked to confirm.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Install a package on Amazon Linux

Why: dnf is the package manager on Amazon Linux 2023. -y answers yes for you.

What to do: Install the httpd package on Amazon Linux 2023 without being asked to confirm.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Test a website from the server

Why: curl shows whether the web server answers, even before DNS is ready.

What to do: Request http://localhost and show only the response headers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Download code from GitHub

Why: git clone copies the whole repository to the server, ready to deploy.

What to do: Clone https://github.com/mumbaidabba/website.git into the current folder.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Test the nginx config

Why: A test catches typos before a restart takes your site down.

What to do: Check the nginx configuration files for errors.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Start a service now and at boot

Why: enable makes it start after reboot; --now also starts it right away.

What to do: Enable the nginx service and start it immediately, in one command.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Open port 80 to the world

Why: Visitors can reach your website only if the security group allows HTTP.

What to do: Allow inbound TCP port 80 from 0.0.0.0/0 on security group sg-01a03134416a4c3b2.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium CloudFormation: web security group

Why: Opening only 443 keeps the server safer.

What to do: Template with an AWS::EC2::SecurityGroup that allows TCP 443 from 0.0.0.0/0. Add a GroupDescription.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Create a security group

Why: A security group is the firewall around your instance.

What to do: Create a security group named mumbaidabba-web-sg with description "Web servers" in vpc-00313441cadeadbee.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Enable an nginx site on Ubuntu

Why: On Ubuntu, a site is enabled by a symbolic link from sites-available to sites-enabled.

What to do: Create a symbolic link to /etc/nginx/sites-available/mumbaidabba inside /etc/nginx/sites-enabled/.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.