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.

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

Copy

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

sudo systemctl enable httpd
sudo systemctl start httpd

Copy

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

Copy

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

Copy

● 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

Copy

sudo httpd -v

Copy

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

Copy

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:

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

To stop the Apache service, run:

sudo systemctl stop httpd

Copy

To start it again, type:

sudo systemctl start httpd

Copy

To restart the Apache service:

sudo systemctl restart httpd

Copy

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

sudo systemctl reload httpd

Copy

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

sudo systemctl disable httpd

Copy

And to re-enable it again:

sudo systemctl enable httpd

Copy


  • 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.ddirectory. 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>