Read in 3 minutes

last updated 

How To Install Apache on CentOS 7

Apache HTTP server is the most popular web server in the world. It is a free, open-source and cross-platform HTTP server providing powerful features which can be extended by a wide variety of modules. The following instructions describe how to install and manage the Apache web server on your CentOS 7 machine.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Installing Apache

Apache is available in the default CentOS repositories and the installation is pretty straight forward.

On CentOS and RHEL the Apache package and the service is called httpd. To install the package run the following command:

sudo yum install httpd

Once the installation is completed, enable and start the Apache service:

sudo systemctl enable httpd
sudo systemctl start httpd

Adjusting the Firewall

If your server is protected by a firewall you need to open HTTP and HTTPS ports, 80 and 443. Use the following commands to open the necessary ports:

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

Verifying Apache Installation

Now that we have Apache installed and running on our CentOS 7 server we can check the status and the version of the Apache service, with:

sudo systemctl status httpd
● 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)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─3049 /usr/sbin/httpd -DFOREGROUND
           ├─3050 /usr/sbin/httpd -DFOREGROUND
           ├─3051 /usr/sbin/httpd -DFOREGROUND
           ├─3052 /usr/sbin/httpd -DFOREGROUND
           ├─3053 /usr/sbin/httpd -DFOREGROUND
           └─3054 /usr/sbin/httpd -DFOREGROUND
sudo httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Oct 19 2017 20:39:16

Finally to verify if everything works properly, open your server IP address http://YOUR_IP in your browser of choice, and you will see the default CentOS 7 Apache welcome page as shown below:

Managing Apache Service

You can manage the Apache service same as any other systemd unit.

To stop the Apache service, run:

sudo systemctl stop httpd

To start it again, type:

sudo systemctl start httpd

To restart the Apache service:

sudo systemctl restart httpd

To reload the Apache service after you made some configuration changes:

sudo systemctl reload httpd

If you want to disable the Apache service to start at boot:

sudo systemctl disable httpd

And to re-enable it again:

sudo systemctl enable httpd

Apache Configuration File’s Structure and Best Practices

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

Conclusion

You have successfully installed Apache on your CentOS 7 server. You’re now ready to start deploying your applications and use Apache as a web or proxy server.

If you have any question or feedback feel free to leave a comment.

This post is a part of the Install LAMP Stack on CentOS 7 series.
Other posts in this series:

How To Install Apache on CentOS 7