How to Install Apache on CentOS 7

How to Install and Configure Apache Web Server on CentOS 7

Apache is an open-source web server that can be configured to serve a single or multiple websites using the same host. This guide explains how to install and configure the Apache web server on CentOS 7.


Install and Configure Apache

  1. Install Apache 2.4:

    sudo yum install httpd
  2. Modify httpd.conf with your document root directory to point Apache to your site’s files. Add the <IfModule prefork.c> section below to adjust the resource use settings. The settings shown below are a good starting point for a host 2GB:


    Before changing any configuration files, we recommend that you make a backup of the file. To make a backup:

    cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup

    DocumentRoot "/var/www/html/"
    <IfModule prefork.c>
        StartServers        5
        MinSpareServers     20
        MaxSpareServers     40
        MaxRequestWorkers   256
        MaxConnectionsPerChild 5500

    These settings can also be added to a separate file. The file must be located in the conf.module.d or conf directories, and must end in .conf, since this is the format of files included in the resulting configuration.

Configure Name-based Virtual Hosts

You can choose many ways to set up a virtual host. In this section we recommend and explain one of the easier methods.

  1. Within the conf.d directory create vhost.conf to store your virtual host configurations. The example below is a template for website; change the necessary values for your domain:

    NameVirtualHost *:80
    <VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/
        ErrorLog /var/www/html/
        CustomLog /var/www/html/ combined

    Additional domains can be added to the vhost.conf file as needed. To add domains, copy the VirtualHost block above and modify its values for each additional virtual host. When new requests come in from the internet, Apache checks which VirtualHost block matches the requested url, and serves the appropriate content:

    Apache VirtualHost Traffic Flow

    ErrorLog and CustomLog entries are suggested for more specific logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache.
  2. Create the directories referenced above:

    sudo mkdir -p /var/www/html/{public_html,logs}
  3. Enable Apache to start at boot, and restart the service for the above changes to take effect:

    sudo systemctl enable httpd.service
    sudo systemctl restart httpd.service

    You can now visit your domain to test the Apache server. A default Apache page will be visible if no index page is found in your Document Root as declared in /etc/httpd/conf/httpd.conf:

    Apache on CentOS 7 Welcome Screen

Configure firewalld to Allow Web Traffic

CentOS 7’s built-in firewall is set to block web traffic by default. Run the following commands to allow web traffic:

sudo firewall-cmd --add-service=http --permanent && sudo firewall-cmd --add-service=https --permanent
sudo systemctl restart firewalld

Next Steps: Add SSL for Security and Install GlusterFS for High Availability

Congratulations! You’ve set up Apache and you’re now ready to host websites. If you’re wondering what additional configuration changes are available to get the most out of your server, some optional steps can be found below.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.