How to Install Apache on CentOS 7

Choose a different version or distribution

Introduction

Before we begin talking about how to install Apache on CentOS 7, let’s briefly understand – What Apache is?

Apache is a popular open-source software foundation known for its versatile web server software. It enables websites to operate efficiently and securely by handling HTTP requests from browsers.

Moreover, Apache supports various programming languages, making it adaptable and user-friendly for developers. With its widespread usage and constant updates, Apache remains a reliable choice for hosting websites across the globe.

In this tutorial, you will install Apache on CentOS 7. Apache is a well-known cross-platform, free, open-source HTTP server.

Advantages of Apache

  1. Versatile web server: Apache is highly adaptable, supporting various platforms and operating systems.
  2. Open-source: As free software, Apache is cost-effective, with a vast community for support and continuous improvement.
  3. Security: With regular updates, it ensures robust security measures to protect websites and user data.
  4. Flexibility: Apache works seamlessly with multiple programming languages and modules.
  5. Performance: Its efficient handling of HTTP requests ensures fast and reliable website operations.

Prerequisites to Install Apache on CentOS 7

You'd need to log in as a user with sudo privileges.

Step 1 - Installing Apache

Apache is available in the default repositories of CentOS, making the installation straightforward.

1) The service is referred to as httpd on CentOS and RHEL. Install the package with the help of the following command:

sudo yum install httpd

2) On completion, you need to enable and start the Apache service:

sudo systemctl enable httpd
sudo systemctl start httpd

Step 2 - Adjusting the firewall

You need to open HTTP and HTTPS ports 80 and 443 in case you have a firewall enabled:

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

Step 3 - Verifying Apache Installation

1) You can then verify the installation with the help of the following commands and you shall see similar outputs:

sudo systemctl status httpd
Output

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-04-26 07:13:07 UTC; 11s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 3049 (httpd)
...
sudo httpd -v
Output

Server version: Apache/2.4.6 (CentOS)
Server built:   Oct 19 2017 20:39:16

2) When you enter http://YOUR_IP in your default browser, you shall see the default CentOS 7 Apache welcome page.

Step 4 - Managing Apache Service

The Apache service can be managed the same way as any other systemd unit.

1) You can stop the service using:

sudo systemctl stop httpd

2) To start the service again, use the following command:

sudo systemctl start httpd

3) To restart Apache:

sudo systemctl restart httpd

4) You can use the following command to reload the service after you altered some configurations:

sudo systemctl reload httpd

5) You can disable the Apache service to start on boot:

sudo systemctl disable httpd

6) To re-enable the service, use the below command:

sudo systemctl enable httpd

Apache Configuration File’s Structure and Best Practices

  • All Apache configuration files can be found in the /etc/httpd directory.
  • /etc/httpd/conf/httpd.conf is the main Apache configuration file.
  • All config files ending with .conf found in the /etc/httpd/conf.d directory is included in the main Apache configuration file.
  • Configuration files that are used for loading various Apache modules can be found in the /etc/httpd/conf.modules.d directory.
  • For better functioning, it is recommended to create a separate configuration file (vhost) for each domain.
  • New Apache vhost files must end with .conf and be saved in /etc/httpd/conf.d directory. You can have as many vhosts as you wish.
  • It is a good idea to follow a standard nomenclature, for instance, if the domain name is mydomain.com then the configuration file should be named /etc/httpd/conf.d/mydomain.com.conf
  • Apache log files (access_log and error_log) are found in the /var/log/httpd/ directory. It is recommended to have a different access and error log files for each vhost.
  • Set your domain document root directory to any location. The most common ones for webroot include:
  • /home/<user_name>/<site_name>
  • /var/www/<site_name>
  • /var/www/html/<site_name>
  • /opt/<site_name>

FAQs to Install Apache on CentOS 7

What are the system requirements for Apache on CentOS 7?

Apache on CentOS 7 requires a minimal installation of CentOS 7 with root privileges.

How do I start and stop the Apache service?

To start Apache, use: sudo systemctl start httpd. To stop it, use: sudo systemctl stop httpd.

Can I enable Apache to start on system boot?

Yes, you can enable Apache to start on boot by running: sudo systemctl enable httpd.

Where can I find the Apache configuration files on CentOS 7?

The main configuration file is located at /etc/httpd/conf/httpd.conf.

Can I change the default web directory for Apache on CentOS 7?

Yes, you can modify the web directory by editing the DocumentRoot directive in the configuration file.

Are there any firewall settings required for Apache to work on CentOS 7?

Yes, you need to allow HTTP and HTTPS traffic through the firewall: sudo firewall-cmd --permanent --add-service=http and sudo firewall-cmd --permanent --add-service=https.

Can I host multiple websites on Apache with CentOS 7?

Absolutely! Apache supports virtual hosts, allowing you to host multiple websites on a single server. Simply create separate configuration files for each site in /etc/httpd/conf.d/.

Conclusion

We hope this detailed tutorial helped you understand how to install, configure and modify Apache on CentOS 7. To learn more about Apache installation on CentOS 7, check out the official Apache installation Documentation.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.