How to setup apache (httpd) and host website on Linux?

Set Up Apache (httpd) and Host a Website on Linux

Setting up Apache HTTP Server (httpd) on a Linux system is a straightforward process that allows you to host a website on your own server. In this detailed guide, we'll go through the steps to install Apache, create a basic web page, configure the firewall, and access your website from a web browser.

Step-by-Step Guide

1. Check Available Packages

Before installing Apache, it's a good idea to check if the httpd package is available in your repository. Open your terminal and run:

dnf list httpd

This command will list the details of the httpd package, including its version and repository information.

2. Install Apache

To install Apache, use the dnf package manager with the following command:

dnf install httpd -y

The -y flag automatically confirms the installation without prompting you for confirmation.

3. Create a Simple Web Page

Now that Apache is installed, let's create a basic HTML file to test the server. We'll use the echo command to write a simple HTML message to the index.html file in the default web directory:

echo "<marquee><h1><font color=teal>Site is working...</font></h1></marquee>" > /var/www/html/index.html

This command creates an HTML file that displays a scrolling marquee message: "Site is working..."

4. Start and Enable Apache

Next, start the Apache service and ensure it starts automatically on boot with the following commands:

systemctl restart httpd
systemctl enable --now httpd
  • systemctl restart httpd: Restarts the Apache service.
  • systemctl enable --now httpd: Enables the Apache service to start at boot and starts it immediately.

5. Configure the Firewall

To allow HTTP traffic through the firewall, you need to add HTTP as an allowed service. Use the following commands:

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --zone=libvirt --add-service=http
firewall-cmd --reload
  • firewall-cmd --permanent --add-service=http: Allows HTTP traffic permanently.

  • firewall-cmd --permanent --zone=libvirt --add-service=http: Allows HTTP traffic in the libvirt zone permanently.

  • firewall-cmd --reload: Reloads the firewall configuration to apply the changes.

6. Access Your Website

Finally, open a web browser and navigate to the IP address of your server to see the new site. For example:

firefox http://10.0.0.100

Conclusion

You've now successfully set up Apache on your Linux system and hosted a simple website. This guide covers the basic steps, but Apache offers extensive configuration options for more complex setups. Whether you're setting up a personal blog or a business website, Apache is a powerful and flexible web server that can meet your needs. Happy hosting!