Lecture NO 3 and 4
Lecture NO 3 and 4
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.
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:
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:
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.
bash
sudo dnf update -y
Install the Apache web server package using the dnf package manager.
bash
sudo dnf install httpd -y
bash
sudo systemctl start httpd
sudo systemctl enable httpd
bash
sudo systemctl status httpd
Step 4: Configure Firewall
bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
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
bash
sudo systemctl restart httpd
Prerequisites
bash
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
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.
bash
sudo mkdir -p /var/www/html/site1
sudo mkdir -p /var/www/html/site2
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
bash
sudo chown -R apache:apache /var/www/html/site1
sudo chown -R apache:apache /var/www/html/site2
bash
sudo systemctl restart httpd
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
bash
sudo systemctl restart NetworkManager
bash
sudo tail -f /var/log/httpd/error_log
Prerequisites
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
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>
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
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
bash
CopyEdit
sudo systemctl restart httpd
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
bash
CopyEdit
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
bash
CopyEdit
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
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
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.
bash
CopyEdit
sudo mkdir -p /var/www/html/example1
sudo mkdir -p /var/www/html/example2
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
bash
CopyEdit
sudo chown -R apache:apache /var/www/html/example1
sudo chown -R apache:apache /var/www/html/example2
If you don’t have DNS set up, add entries to the /etc/hosts file for local testing:
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
bash
CopyEdit
sudo systemctl restart httpd
bash
CopyEdit
sudo tail -f /var/log/httpd/error_log