Setting Up Apache HTTP Server on Linux: A Step-by-Step Guide
Introduction
In this blog post, we’ll be going over a script that sets up Apache HTTP Server (httpd) on a Linux system and launches a basic “Hello World” webpage. We’ll explain each step of the script in detail and provide some background information on httpd.
What is Apache HTTP Server?
Apache HTTP Server, commonly referred to as simply “Apache”, is a popular open-source web server software. It was initially released in 1995 and has since become one of the most widely used web servers on the internet. Apache is known for its flexibility, extensibility, and robustness, and it can serve both static and dynamic content.
Script Explanation
Let’s take a look at the script that sets up httpd and launches a basic webpage:
#!/bin/bash
# Install httpd if not already installed
if ! command -v httpd &> /dev/null
then
echo "httpd not found. Installing..."
sudo yum install httpd -y # Replace with appropriate package manager for your Linux distribution
fi
# Start httpd service and enable it to start on boot
sudo systemctl start httpd
sudo systemctl enable httpd
# Create a basic index.html file
echo "<html><body><h1>Hello World!</h1></body></html>" | sudo tee /var/www/html/index.html
# Set permissions for index.html file
sudo chmod 644 /var/www/html/index.html
# Open firewall port for http traffic
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload
# Display message with URL to access webpage
echo "Webpage can be accessed at http://$(curl -s ifconfig.co)/"
Let's go through each step in more detail:
Step 1: Check if httpd is installed
The script starts by checking if httpd is already installed on the system using the command -v
command. If httpd is not found, the script proceeds to install it using the appropriate package manager for the system. In this case, the script uses the yum
package manager, which is common on Red Hat-based systems.
# Install httpd if not already installed
if ! command -v httpd &> /dev/null
then
echo "httpd not found. Installing..."
sudo yum install httpd -y # Replace with appropriate package manager for your Linux distribution
fi
Step 2: Start the httpd service and enable it to start on boot
Once httpd is installed, the script starts the httpd service using the systemctl start
command and enables it to start automatically on boot using the systemctl enable
command.
# Start httpd service and enable it to start on boot
sudo systemctl start httpd
sudo systemctl enable httpd
Step 3: Create a basic index.html file
The script then creates a basic “Hello World” webpage by echoing some HTML code into the /var/www/html/index.html
file using the tee
command. This file is the default location for web content served by httpd.
# Create a basic index.html file
echo "<html><body><h1>Hello World!</h1></body></html>" | sudo tee /var/www/html/index.html
Step 4: Set permissions for index.html file
The script then sets the appropriate permissions for the index.html file to ensure that httpd can read and serve it properly.
# Set permissions for index.html file
sudo chmod 644
Step 5: Open firewall port for HTTP traffic
The script opens the firewall port for http traffic using the firewall-cmd
command. This allows incoming http traffic to reach the httpd server and access the webpage.
# Open firewall port for http traffic
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload
Step 6: Display message with URL to access webpage
Finally, the script displays a message with the URL to access the “Hello World” webpage. The URL is generated using the curl
command to retrieve the system's public IP address from ifconfig.co
.
# Display message with URL to access webpage
echo "Webpage can be accessed at http://$(curl -s ifconfig.co)/"
Conclusion
In this blog post, we went over a script that sets up Apache HTTP Server (httpd) on a Linux system and launches a basic “Hello World” webpage. We explained each step of the script in detail and provided some background information on httpd. We hope that this script and explanation were helpful to you, and that you were able to successfully set up httpd and launch your own webpage!