0% found this document useful (0 votes)
2 views

Lecture NO 3 and 4

Apache HTTP Server is a widely used open-source web server software that supports various operating systems and is highly customizable. It handles HTTP requests, serves static and dynamic content, and can be configured for virtual hosting based on IP addresses or domain names. DNF is the modern package manager for Red Hat-based systems, replacing YUM, with improved performance and reliability, and is used to install and manage Apache on CentOS 9.

Uploaded by

Raja Saad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture NO 3 and 4

Apache HTTP Server is a widely used open-source web server software that supports various operating systems and is highly customizable. It handles HTTP requests, serves static and dynamic content, and can be configured for virtual hosting based on IP addresses or domain names. DNF is the modern package manager for Red Hat-based systems, replacing YUM, with improved performance and reliability, and is used to install and manage Apache on CentOS 9.

Uploaded by

Raja Saad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture NO 3 and 4

What is Apache Server?

Apache HTTP Server, commonly referred to as Apache, is an open-source, cross-platform web


server software. It is one of the most widely used web servers globally and plays a critical role
in hosting websites and web applications.

Key Features of Apache Server:

1. Open Source: Apache is free to use and is distributed under the Apache License 2.0.
2. Cross-Platform Support: It runs on various operating systems, including Linux, Unix,
Windows, and macOS.
3. Extensibility: Apache supports modules that extend its functionality, such as PHP,
SSL, and proxy modules.
4. Customizable: You can configure Apache to suit specific needs through its
configuration files (e.g., httpd.conf).
5. Secure: Includes features like HTTPS support, authentication mechanisms, and access
control.
6. Community Support: Apache is backed by a large community, ensuring continuous
updates and support.

How Apache Works:

1. Handles HTTP Requests: Apache listens for HTTP requests from web browsers.
2. Processes Requests: Based on the request, it serves static files (like HTML, CSS) or
forwards requests to application servers (e.g., PHP or Python).
3. Returns Responses: Sends the appropriate response (e.g., a webpage) back to the
client.

Use Cases:

• Hosting websites and web applications.


• Serving static and dynamic content.
• Acting as a reverse proxy server.
Advantages:

1. Reliable and Stable: Proven track record of stability.


2. Highly Configurable: Supports URL rewriting, custom error pages, and virtual hosts.
3. Compatible with Other Technologies: Works seamlessly with databases,
programming languages, and CMS platforms like WordPress.

• Difference Between DNF and YUM

Both DNF and YUM are package managers used in Red Hat-based Linux distributions like
CentOS and Fedora. However, DNF is the modern replacement for YUM and addresses many
of its limitations. Below is a detailed comparison:

YUM (Yellowdog Updater,


Feature DNF (Dandified Yum)
Modified)
Introduced in Fedora 18 and became the Used in older versions of
Introduction
default in Fedora 22. CentOS, RHEL, and Fedora.
Dependency Uses the libsolv library for faster and Uses older dependency
Resolution more reliable resolution. resolution algorithms.
Faster and more efficient, especially Slower and can consume more
Performance
with large repositories. memory.
Lower memory consumption during Higher memory consumption
Memory Usage
operations. in comparison.
Better error handling and debugging Limited error handling
Error Handling
messages. capabilities.
Supports the same plugins as YUM but
Plugins Plugin support is more basic.
introduces additional functionality.
Almost identical to YUM, but some Simpler syntax, but lacks some
Command Syntax
advanced features are different. modern features.
Metadata Handles and downloads metadata more Inefficient metadata handling
Handling efficiently, reducing disk usage. compared to DNF.
Support and Actively maintained and supported in Deprecated and not actively
Updates newer distributions. developed.
Backward Provides aliases for YUM commands to Fully backward compatible
Compatibility ensure a smooth transition. with legacy systems.

Examples of Common Commands:


Action DNF Command YUM Command
Install a package dnf install package yum install package

Remove a package dnf remove package yum remove package

Update all packages dnf update yum update

List installed packages dnf list installed yum list installed


Search for a package dnf search package yum search package

Clean metadata cache dnf clean all yum clean all

Key Takeaway:

• DNF is the next-generation package manager designed to replace YUM with better
performance, reliability, and support for modern systems.
• CentOS 8 and later (including CentOS Stream and CentOS 9) use DNF as the default
package manager, while older systems rely on YUM.

If you're using CentOS 9, DNF is the default and recommended choice.

Step 1: Update the System

Ensure your system is up-to-date before installing any software.

bash
sudo dnf update -y

Step 2: Install Apache

Install the Apache web server package using the dnf package manager.

bash
sudo dnf install httpd -y

Step 3: Start and Enable Apache

Start the Apache service and enable it to start at boot.

bash
sudo systemctl start httpd
sudo systemctl enable httpd

You can check the status to ensure it’s running:

bash
sudo systemctl status httpd
Step 4: Configure Firewall

Allow HTTP and HTTPS traffic through the firewall.

bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 5: Verify Installation

To verify that Apache is installed and running:

1. Open a web browser.


2. Enter your server’s IP address or http://localhost.

You should see the default Apache welcome page.

Step 6: Configure Apache Using IP

1. Locate the Configuration File: Apache's default configuration file is located at:

bash
/etc/httpd/conf/httpd.conf

2. Edit the Configuration File: Open the file in a text editor, such as nano:

bash
sudo nano /etc/httpd/conf/httpd.conf

Look for the Listen directive and ensure it binds to your desired IP address (e.g.,
192.168.1.10):

mathematica
Listen 192.168.1.10:80

3. Set the ServerName: Add the ServerName directive to specify the IP address or
hostname:

ServerName 192.168.1.10

4. Save and Exit: Save changes and exit the text editor.
5. Restart Apache: Restart Apache to apply the changes.

bash
sudo systemctl restart httpd
Step 7: Test Apache Using IP

1. Open a web browser.


2. Enter your server’s IP address, such as http://192.168.1.10.
3. You should see the Apache welcome page.

Optional: Host a Website

To host your own website:

1. Place your files in the /var/www/html directory.


2. Restart Apache:

bash
sudo systemctl restart httpd

• Configure Apache on CentOS 9


To configure Apache on CentOS 9 to serve content based on IP addresses, follow these steps:

Prerequisites

1. Ensure Apache is installed and running. Use the commands:

bash
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

2. Allow HTTP traffic through the firewall:

bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Step 1: Configure Apache for IP-Based Hosting

Apache's default configuration file is /etc/httpd/conf/httpd.conf. To serve different


content based on IP addresses, use Virtual Hosts. Here’s how:

1. Edit the Configuration File: Open the configuration file in a text editor:

bash
sudo nano /etc/httpd/conf/httpd.conf
2. Add VirtualHost Directives: Add the following configuration for each IP address
you want to use. Replace 192.168.1.10 and 192.168.1.20 with the actual IP
addresses configured on your server.

apache

<VirtualHost 192.168.1.10:80>
DocumentRoot "/var/www/html/site1"
ServerName 192.168.1.10
ErrorLog "/var/log/httpd/site1_error.log"
CustomLog "/var/log/httpd/site1_access.log" combined
</VirtualHost>

<VirtualHost 192.168.1.20:80>
DocumentRoot "/var/www/html/site2"
ServerName 192.168.1.20
ErrorLog "/var/log/httpd/site2_error.log"
CustomLog "/var/log/httpd/site2_access.log" combined
</VirtualHost>

3. Save and Exit: Save changes to the file and exit the editor.

Step 2: Create Directories for Each Site

1. Create separate directories for the content of each site:

bash
sudo mkdir -p /var/www/html/site1
sudo mkdir -p /var/www/html/site2

2. Add an index.html file for each site:

bash
echo "Welcome to Site 1" | sudo tee /var/www/html/site1/index.html
echo "Welcome to Site 2" | sudo tee /var/www/html/site2/index.html

3. Set permissions for the directories:

bash
sudo chown -R apache:apache /var/www/html/site1
sudo chown -R apache:apache /var/www/html/site2

Step 3: Restart Apache

Restart the Apache service to apply the changes:

bash
sudo systemctl restart httpd

Step 4: Assign IPs to the Server


Ensure your server has the IP addresses configured. Use the following steps to assign
multiple IPs:

1. Edit Network Configuration: Open the network script file for your network
interface (e.g., eth0):

bash
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

2. Add Additional IP Addresses: Add lines like this for each IP:

makefile

IPADDR2=192.168.1.20
PREFIX2=24

3. Restart the Network Service:

bash
sudo systemctl restart NetworkManager

Step 5: Test the Configuration

1. Open a browser or use curl to access the configured IPs:


o http://192.168.1.10 → Displays content from /var/www/html/site1.
o http://192.168.1.20 → Displays content from /var/www/html/site2.
2. Check the Apache logs for any errors:

bash
sudo tail -f /var/log/httpd/error_log

To configure Apache on CentOS 9 for port-based virtual hosting, follow


these steps:

Prerequisites

1. Ensure Apache is installed and running:

bash
CopyEdit
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

2. Allow custom ports through the firewall. For example, if you want to use ports 8080
and 9090:

bash
CopyEdit
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --permanent --add-port=9090/tcp
sudo firewall-cmd --reload

Step 1: Edit Apache Configuration

1. Open the main Apache configuration file:

bash
CopyEdit
sudo nano /etc/httpd/conf/httpd.conf

2. Locate the line that starts with Listen 80 and add additional Listen directives for
the ports you want to use. For example:

apache
CopyEdit
Listen 80
Listen 8080
Listen 9090

3. Add the following VirtualHost configurations for each port. Replace DocumentRoot
paths and other values as needed:

apache
CopyEdit
<VirtualHost *:80>
DocumentRoot "/var/www/html/site1"
ServerName localhost
ErrorLog "/var/log/httpd/site1_error.log"
CustomLog "/var/log/httpd/site1_access.log" combined
</VirtualHost>

<VirtualHost *:8080>
DocumentRoot "/var/www/html/site2"
ServerName localhost
ErrorLog "/var/log/httpd/site2_error.log"
CustomLog "/var/log/httpd/site2_access.log" combined
</VirtualHost>

<VirtualHost *:9090>
DocumentRoot "/var/www/html/site3"
ServerName localhost
ErrorLog "/var/log/httpd/site3_error.log"
CustomLog "/var/log/httpd/site3_access.log" combined
</VirtualHost>

4. Save and exit the file.

Step 2: Create Directories for Each Port

1. Create directories for each virtual host's content:


bash
CopyEdit
sudo mkdir -p /var/www/html/site1
sudo mkdir -p /var/www/html/site2
sudo mkdir -p /var/www/html/site3

2. Add an index.html file to each directory:

bash
CopyEdit
echo "Welcome to Site 1 on Port 80" | sudo tee
/var/www/html/site1/index.html
echo "Welcome to Site 2 on Port 8080" | sudo tee
/var/www/html/site2/index.html
echo "Welcome to Site 3 on Port 9090" | sudo tee
/var/www/html/site3/index.html

3. Set ownership and permissions:

bash
CopyEdit
sudo chown -R apache:apache /var/www/html/site1
sudo chown -R apache:apache /var/www/html/site2
sudo chown -R apache:apache /var/www/html/site3

Step 3: Restart Apache

Restart the Apache service to apply the changes:

bash
CopyEdit
sudo systemctl restart httpd

Step 4: Test the Configuration

1. Open a browser or use curl to test the configuration:


o http://<your-server-ip>:80 → Displays content from
/var/www/html/site1.
o http://<your-server-ip>:8080 → Displays content from
/var/www/html/site2.
o http://<your-server-ip>:9090 → Displays content from
/var/www/html/site3.
2. Check Apache logs for any errors:

bash
CopyEdit
sudo tail -f /var/log/httpd/error_log

To configure Apache for name-based virtual hosting on CentOS 9, follow these steps:
Prerequisites

1. Ensure Apache is installed and running:

bash
CopyEdit
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

2. Allow HTTP traffic through the firewall:

bash
CopyEdit
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Step 1: Edit Apache Configuration for Name-Based Virtual Hosts

1. Open the main Apache configuration file:

bash
CopyEdit
sudo nano /etc/httpd/conf/httpd.conf

2. Locate the line starting with #NameVirtualHost. Uncomment it or add the following
directive if it's missing:

apache
CopyEdit
NameVirtualHost *:80

3. Add VirtualHost configurations for each domain or hostname:

apache
CopyEdit
<VirtualHost *:80>
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot "/var/www/html/example1"
ErrorLog "/var/log/httpd/example1_error.log"
CustomLog "/var/log/httpd/example1_access.log" combined
</VirtualHost>

<VirtualHost *:80>
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot "/var/www/html/example2"
ErrorLog "/var/log/httpd/example2_error.log"
CustomLog "/var/log/httpd/example2_access.log" combined
</VirtualHost>
4. Save and exit the file.

Step 2: Create Document Roots for Each Site

1. Create directories for the virtual hosts:

bash
CopyEdit
sudo mkdir -p /var/www/html/example1
sudo mkdir -p /var/www/html/example2

2. Add an index.html file to each directory:

bash
CopyEdit
echo "Welcome to Example1" | sudo tee
/var/www/html/example1/index.html
echo "Welcome to Example2" | sudo tee
/var/www/html/example2/index.html

3. Set ownership and permissions:

bash
CopyEdit
sudo chown -R apache:apache /var/www/html/example1
sudo chown -R apache:apache /var/www/html/example2

Step 3: Update the Hosts File (for Testing Locally)

If you don’t have DNS set up, add entries to the /etc/hosts file for local testing:

1. Open the hosts file:

bash
CopyEdit
sudo nano /etc/hosts

2. Add the following lines, replacing <server-ip> with your server's IP address:

php
CopyEdit
<server-ip> example1.com
<server-ip> example2.com

3. Save and exit the file.

Step 4: Restart Apache


Restart the Apache service to apply the changes:

bash
CopyEdit
sudo systemctl restart httpd

Step 5: Test the Configuration

1. Open a browser or use curl to test:


o http://example1.com → Displays content from /var/www/html/example1.
o http://example2.com → Displays content from /var/www/html/example2.
2. Check Apache logs for any issues:

bash
CopyEdit
sudo tail -f /var/log/httpd/error_log

You might also like