Website Vidya
DeploymentStep-by-Step Guide

How to Install Apache on CentOS 7: 6 Easy Steps

This guide will walk you through the step-by-step process of installing Apache, allowing you to host websites and serve web content on your CentOS 7 server. Installing Apache on CentOS 7 is a straightforward procedure that can be accomplish...

RR
Website Vidya TeamLead Systems Educator
Published:
7 min read
How to Install Apache on CentOS 7: 6 Easy Steps
AI Executive SummaryCORE CIS AI

7 min read

This guide will walk you through the step-by-step process of installing Apache, allowing you to host websites and serve web content on your CentOS 7 server. Installing Apache on CentOS 7 is a straightforward procedure that can be accomplish...

Key Highlights
  • Prerequisites
  • How to Install Apache on CentOS 7: Step-by-Step Guide
  • Conclusion
Grounded in full engineering runbookCORE CIS Ai Engine

This guide will walk you through the step-by-step process of installing Apache, allowing you to host websites and serve web content on your CentOS 7 server.

Installing Apache on CentOS 7 is a straightforward procedure that can be accomplished in a few simple steps. By following this guide, you will learn how to acquire and install Apache using the package manager, configure essential settings, and verify the successful installation.

Apache is a widely used and highly reliable web server that powers a significant portion of the internet. Whether you are setting up a personal website, deploying web applications, or creating a staging environment, Apache is an excellent choice due to its stability, performance, and extensive feature set.

With this comprehensive installation guide on how to install Apache on CentOS 7, you will gain the knowledge to deploy a powerful web server and start serving web content in no time.

You may also like:

Prerequisites

  • An up-to-date CentOS 7 Server
  • sudo privileges

How to Install Apache on CentOS 7: Step-by-Step Guide

Step 1: Update System Packages

Before installing Apache, it's essential to ensure that your CentOS 7 server is up to date. Open a terminal window and execute the following commands:

sudo yum -y update

This command will update all the packages on your system to the latest versions, including security patches and bug fixes. Wait for the process to complete, then proceed to the next step.

Step 2: Install Apache

Once your system is updated, you can proceed with installing Apache using the following command:

sudo yum install httpd -y

This command will install the Apache web server package and its dependencies. When prompted, type y and press Enter to confirm the installation. The package manager will then download and install Apache on your system.

Step 3: Start Apache Service

After the installation is complete, you can start the Apache service using the following command:

sudo systemctl start httpd

This command will initiate the Apache service on your CentOS 7 server. To ensure that Apache starts automatically at system boot, run the following command:

sudo systemctl enable httpd

With this, Apache will automatically start whenever the server is rebooted.

To check the status of the Apache server running on CentOS, you can use the following command:

sudo systemctl status httpd

This command will display the current status of the Apache service, including whether it is running or not. If the Apache server is active and running, the output will indicate that it is active (running).

On the other hand, if it is not running, the output will show that it is inactive (dead) or stopped.

Additionally, you can also use the following command to check if the Apache server is actively listening on the standard HTTP port (port 80):

netstat -tuln | grep :80

If the Apache server is running and actively listening on port 80, you will see an output that indicates the port is open and the process associated with it is httpd or apache2. If there is no output, it means that the Apache server is not running or not configured to listen on port 80.

Step 4: Adjust Firewall

By default, CentOS 7 uses a firewall called firewalld. To allow incoming web traffic to your Apache server, you need to configure the firewall to permit HTTP and HTTPS connections. Run the following commands:

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

These commands will enable the necessary firewall rules to allow web traffic through ports 80 (HTTP) and 443 (HTTPS).

Step 5: Test Apache Installation

To confirm that Apache is functioning correctly, you can perform the following steps:

Open a web browser on your computer, enter your server's IP address or domain name in the browser's address bar, and press Enter or Return to load the page.

If Apache is running properly and the necessary configurations are in place, you should see the default Apache web page. This page serves as a confirmation that Apache has been successfully installed and is functioning correctly.

It's important to ensure that the server's IP address or domain name is correctly entered in the browser. If you encounter any issues or errors, double-check the Apache server's configuration, including the virtual hosts, document root, and any firewall settings, to ensure they are properly set up.

Remember to replace "your server's IP address or domain name" in the instructions above with the actual IP address or domain name associated with your Apache server.

To check the IP address of your server, you can follow the methods mentioned below:

To check the IP address of a CentOS server, you can use various methods:

Method 1: Using the ip command

Open a terminal or SSH and run the following command:

ip addr show

This will display detailed information about network interfaces, including their IP addresses.

Method 2: Using the ifconfig command

To identify the IP Address using ifconfig, run the following command:

ifconfig

The output will show the IP addresses of all network interfaces on the server.

Method 3: Using the hostname command

To identify the IP Address using hostname, run:

hostname -I

This command will display the server's primary IP address.

Method 4: Using the nmcli command (NetworkManager)

To identify the IP Address using nmcli, run the following command,

nmcli -p device show

This command will provide detailed information about network devices, including their IP addresses.

Choose the method that works best for your setup or preferences. These commands will help you identify your CentOS server's IP address(es).

Step 6: Configure Virtual Hosts

Apache allows you to host multiple websites on a single server using virtual hosts. By default, Apache is preconfigured with a main host. However, if you want to host additional websites, you'll need to create virtual host configuration files.

Create a new virtual host configuration file by executing the following command:

sudo nano /etc/httpd/conf.d/example.com.conf

Replace example.com with the domain or hostname of your website. In the configuration file, add the following content:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example.com
    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>

Save the file and exit the editor.

Now, create the necessary directory and assign appropriate permissions using the following commands:

sudo mkdir /var/www/html/example.com
sudo chown -R apache:apache /var/www/html/example.com

Now create a dummy index.html page under the /var/www/html/example.com directory that we created earlier by navigating to the /var/www/html/example.com directory using the following command:

cd /var/www/html/example.com

Now, create the index.html file using your favorite text editor. In my case, I’m using Vim editor.

sudo vim index.html

Inside this HTML page, we will add some HTML content:

<!DOCTYPE html>
<html>
  <head>
      <title>Welcome to example.com</title>
  </head>
  <body>
      <h1>Welcome to example.com</h1>
      <p>This is a dummy page.</p>
  </body>
</html>

Save the file and exit the text editor.

To save and exit the VIM editor in CentOS, follow these steps:

Press the ESC key to ensure you are in command mode.

Now, type :wq or :x to save the changes and exit the editor.

  • The :wq command writes (saves) the changes and quits VIM.
  • The :x command is a shortcut for :wq and performs the same action.

Press Enter to execute the command. VIM will save the changes made to the file and exit the editor.

Once you have followed these steps, the dummy index.html page will be created in the /var/www/html/example.com directory on your CentOS server.

You can access it through a web browser by entering the server's IP address or domain name followed by /example.com (e.g., http://your_server_ip/example.com) to see the content of the index.html page.

Finally, restart the Apache service for the changes to take effect:

sudo systemctl restart httpd

Conclusion

Congratulations! You have successfully installed and configured Apache on your CentOS 7 server. You can now host websites and enjoy the robust features and performance offered by Apache.

Remember to regularly update your server's packages and monitor the Apache logs for any issues or security concerns. Additionally, explore Apache's extensive documentation to customize further and optimize your web server. With Apache and CentOS 7, you have a reliable and secure foundation for your web hosting needs.

Happy hosting!

Written by

Website Vidya Team

Technology educator and researcher at Website Vidya.

Discussion

0
Share your thoughts, ask a question, or suggest a tip...
Write →

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Deployment•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Deployment•

25 Best Linux Commands Every Sysadmin Should Know

If you’ve spent any time managing Linux servers, you already know this truth: the command line is not optional. It’s where real troubleshooting happens, where performance issues are understood, and where outages are prevented before users e...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Snapd Consuming High CPU / RAM on Linux

If you’ve ever opened your system monitor and noticed snapd quietly eating up your CPU or RAM, you’re not alone. Many Linux users—especially on Ubuntu and Ubuntu-based distributions—eventually run into this issue and wonder what’s going on ...

RR
Rajesh Rai
Read More