Apache and Tomcat

AJEET GUPTAAJEET GUPTA
6 min read

Definitions

  1. HTTPD

    Httpd stands for Hypertext Transfer Protocol Daemon. It's essentially a software program that runs in the background on a web server. Think of it as the engine that keeps things running smoothly.

    It listens for requests from web browser (client) and delivers content (web pages, images) using HTTP language.

  2. Apache HTTP Server(Apache Web Server)

    Apache HTTP Server, commonly known as Apache, is a widely-used open-source web server software. It allows web hosting of static content (HTML, CSS, JavaScript) and serves as a front-end proxy for dynamic content.

  3. Apache Tomcat Server(Tomcat Web Server)

    Apache Tomcat is an open-source Java Servlet Container that implements several Java EE specifications, including Java Servlet, JavaServer Pages (JSP), and WebSocket. It provides a "pure Java" HTTP web server environment for running Java code.

Apache vs Tomcat

Apache HTTP ServerApache Tomcat
Think of it as a traffic copThink of it as a translator
Free and open-source web server Directs incoming traffic (client requests)- Delivers static content (web pages, images, videos)Open-source servlet container Specializes in handling dynamic web applications
Fast and versatile Handles various file formats Works well with different programming languagesExcels at running web applications built with Java servlets and JSP Handles dynamic content that responds to user input
Not for complex, dynamic content Can handle some scripting Not designed for interactive web applicationsOften used alongside Apache HTTP Server Apache handles static content Tomcat handles dynamic Java applications

Here's an analogy:

  • Imagine a restaurant website.

  • Apache HTTP Server (the traffic cop) receives the order (request) for the menu (static HTML page).

  • If the order involves placing a customized pizza (dynamic content), Apache might hand it off to Tomcat (the translator) who understands the chef's instructions (Java code) and gets your pizza made (processes the request).

  • Finally, both Apache and Tomcat work together to deliver your delicious website experience (the final response).

Apache HTTP Server

Installing Apache HTTP Server

[ajeet@centos1 ~]$ su -
Password:
[root@centos1 ~]# yum install httpd
CentOS Stream 9 - BaseOS                                                                                    1.2 MB/s | 8.1 MB     00:06
CentOS Stream 9 - AppStream                                                                                 1.3 MB/s |  20 MB     00:14
CentOS Stream 9 - Extras packages                                                                           4.4 kB/s |  17 kB     00:03
Dependencies resolved.
============================================================================================================================================
 Package                                 Architecture                Version                           Repository                      Size
============================================================================================================================================
Installing:
 httpd                                   x86_64                      2.4.57-8.el9                      appstream                       48 k
Installing dependencies:
 apr                                     x86_64                      1.7.0-12.el9                      appstream                      123 k
 apr-util                                x86_64                      1.6.1-23.el9                      appstream                       95 k
 apr-util-bdb                            x86_64                      1.6.1-23.el9                      appstream                       13 k
 centos-logos-httpd                      noarch                      90.8-1.el9                        appstream                      1.5 M
 httpd-core                              x86_64                      2.4.57-8.el9                      appstream                      1.5 M
 httpd-filesystem                        noarch                      2.4.57-8.el9                      appstream                       13 k
 httpd-tools                             x86_64                      2.4.57-8.el9                      appstream                       83 k
Installing weak dependencies:
 apr-util-openssl                        x86_64                      1.6.1-23.el9                      appstream                       15 k
 mod_http2                               x86_64                      2.0.26-2.el9                      appstream                      163 k
 mod_lua                                 x86_64                      2.4.57-8.el9                      appstream                       60 k

Transaction Summary
============================================================================================================================================
Install  11 Packages

Total download size: 3.6 M
Installed size: 8.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/11): apr-util-bdb-1.6.1-23.el9.x86_64.rpm                                                                 14 kB/s |  13 kB     00:00
(2/11): apr-util-openssl-1.6.1-23.el9.x86_64.rpm                                                            101 kB/s |  15 kB     00:00
(3/11): apr-util-1.6.1-23.el9.x86_64.rpm                                                                     46 kB/s |  95 kB     00:02
(4/11): httpd-2.4.57-8.el9.x86_64.rpm                                                                       285 kB/s |  48 kB     00:00
(5/11): centos-logos-httpd-90.8-1.el9.noarch.rpm                                                            472 kB/s | 1.5 MB     00:03
(6/11): httpd-filesystem-2.4.57-8.el9.noarch.rpm                                                             73 kB/s |  13 kB     00:00
(7/11): httpd-tools-2.4.57-8.el9.x86_64.rpm                                                                 401 kB/s |  83 kB     00:00
(8/11): apr-1.7.0-12.el9.x86_64.rpm                                                                          21 kB/s | 123 kB     00:05
(9/11): httpd-core-2.4.57-8.el9.x86_64.rpm                                                                  402 kB/s | 1.5 MB     00:03
(10/11): mod_lua-2.4.57-8.el9.x86_64.rpm                                                                    302 kB/s |  60 kB     00:00
(11/11): mod_http2-2.0.26-2.el9.x86_64.rpm                                                                   14 kB/s | 163 kB     00:12
--------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                       205 kB/s | 3.6 MB     00:18
CentOS Stream 9 - AppStream                                                                                 1.1 MB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                    1/1
  Installing       : apr-1.7.0-12.el9.x86_64                                                                                           1/11
  Installing       : apr-util-bdb-1.6.1-23.el9.x86_64                                                                                  2/11
  Installing       : apr-util-openssl-1.6.1-23.el9.x86_64                                                                              3/11
  Installing       : apr-util-1.6.1-23.el9.x86_64                                                                                      4/11
  Installing       : httpd-tools-2.4.57-8.el9.x86_64                                                                                   5/11
  Running scriptlet: httpd-filesystem-2.4.57-8.el9.noarch                                                                              6/11
  Installing       : httpd-filesystem-2.4.57-8.el9.noarch                                                                              6/11
  Installing       : httpd-core-2.4.57-8.el9.x86_64                                                                                    7/11
  Installing       : mod_lua-2.4.57-8.el9.x86_64                                                                                       8/11
  Installing       : centos-logos-httpd-90.8-1.el9.noarch                                                                              9/11
  Installing       : mod_http2-2.0.26-2.el9.x86_64                                                                                    10/11
  Installing       : httpd-2.4.57-8.el9.x86_64                                                                                        11/11
  Running scriptlet: httpd-2.4.57-8.el9.x86_64                                                                                        11/11
  Verifying        : apr-1.7.0-12.el9.x86_64                                                                                           1/11
  Verifying        : apr-util-1.6.1-23.el9.x86_64                                                                                      2/11
  Verifying        : apr-util-bdb-1.6.1-23.el9.x86_64                                                                                  3/11
  Verifying        : apr-util-openssl-1.6.1-23.el9.x86_64                                                                              4/11
  Verifying        : centos-logos-httpd-90.8-1.el9.noarch                                                                              5/11
  Verifying        : httpd-2.4.57-8.el9.x86_64                                                                                         6/11
  Verifying        : httpd-core-2.4.57-8.el9.x86_64                                                                                    7/11
  Verifying        : httpd-filesystem-2.4.57-8.el9.noarch                                                                              8/11
  Verifying        : httpd-tools-2.4.57-8.el9.x86_64                                                                                   9/11
  Verifying        : mod_http2-2.0.26-2.el9.x86_64                                                                                    10/11
  Verifying        : mod_lua-2.4.57-8.el9.x86_64                                                                                      11/11

Installed:
  apr-1.7.0-12.el9.x86_64              apr-util-1.6.1-23.el9.x86_64  apr-util-bdb-1.6.1-23.el9.x86_64 apr-util-openssl-1.6.1-23.el9.x86_64
  centos-logos-httpd-90.8-1.el9.noarch httpd-2.4.57-8.el9.x86_64     httpd-core-2.4.57-8.el9.x86_64   httpd-filesystem-2.4.57-8.el9.noarch
  httpd-tools-2.4.57-8.el9.x86_64      mod_http2-2.0.26-2.el9.x86_64 mod_lua-2.4.57-8.el9.x86_64

Complete!
[root@centos1 ~]# httpd -version
Server version: Apache/2.4.57 (CentOS Stream)
Server built:   Feb  7 2024 00:00:00
[root@centos1 ~]# systemctl status httpd
○ httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
       Docs: man:httpd.service(8)

Configuring Apache

Now, let's navigate to the Apache configuration directory and inspect the main configuration file:

[root@centos1 ~]# cd /etc/httpd/conf
[root@centos1 conf]# ls
httpd.conf  magic
[root@centos1 conf]# cat httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
#
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
#
#
#
IncludeOptional conf.d/*.conf

Setting DocumentRoot

The DocumentRoot directive specifies the directory from which Apache will serve files. By default, it's set to /var/www/html. Let's navigate to this directory and create a sample index.html file:

[root@centos1 conf]# cd /var/www/html
[root@centos1 html]# ls
[root@centos1 html]# vi index.html
[root@centos1 html]# ls
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Apache Test Page</title>
</head>
<body>
    <h1>Hello, Apache!</h1>
    <p>This is a simple HTML file served by Apache web server.</p>
</body>
</html>

Starting Apache

After creating the index.html file, let's start the Apache service:

[root@centos1 html]# systemctl start httpd

Testing the Web Server

Finally, let's test our web server by accessing it through a web browser. You can use the server's IP address to access the webpage. However, if you encounter any issues accessing the webpage, it might be due to firewall settings.

Temporarily Disabling Firewall

For testing purposes, let's temporarily stop the firewall service:

[root@centos1 html]# systemctl stop firewalld.service

By stopping the firewall, we can ensure that it's not blocking access to our web server. Remember to re-enable the firewall after testing.

Allowing HTTP or Port 80/TCP

if you prefer to keep the firewall enabled, you can allow HTTP traffic through the firewall by running the following commands:

# Allow HTTP service permanently
[root@centos1 html]# firewall-cmd --add-service=http --permanent
success

# Reload firewall configuration
[root@centos1 html]# firewall-cmd --reload
success

# Alternatively, we can directly add port 80 (HTTP) to the firewall rules
[root@centos1 html]# firewall-cmd --add-port=80/tcp --permanent
success

# Reload firewall configuration
[root@centos1 html]# firewall-cmd --reload
success

# Check the list of open ports and services to ensure HTTP is allowed
[root@centos1 html]# firewall-cmd --list-ports
80/tcp
[root@centos1 html]# firewall-cmd --list-services
cockpit dhcpv6-client http ssh

By allowing HTTP traffic through the firewall, we ensure that external requests to port 80 are permitted, allowing users outside the VM to access our web server. Without these firewall rules, external access to port 80 would be blocked, which is why the webpage is accessible within the VM's browser but not from outside the VM.

Summary

🐧Thank you for joining me on this journey through Linux Web Server Management. I truly appreciate your passion and dedication to learning. Keep exploring, stay curious, and happy coding!

0
Subscribe to my newsletter

Read articles from AJEET GUPTA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

AJEET GUPTA
AJEET GUPTA

Hi there! I’m Ajeet, a System Administrator with three years of experience in managing both on-premises and cloud infrastructures. I’m all about using ITIL practices to automate tasks, solve problems, and keep systems running smoothly.