How to Install Apache on CentOS 7
Updated by Edward Angert Written by Linode
Apache is an open-source web server that can be configured to serve a single or multiple websites using the same Linode. This guide explains how to install and configure the Apache web server on CentOS 7.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.Replace each instance of
example.com
in this guide with your site’s domain name.
Before You Begin
Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.
To check your hostname run:
hostname hostname -f
The first command should show your short hostname, and the second should show your Fully Qualified Domain Name (FQDN).
Update your system:
sudo yum update
Apache
Install and Configure Apache
Install Apache 2.4:
sudo yum install httpd
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 Linode 2GB:Note
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
- /etc/httpd/conf/httpd.conf
-
1 2 3 4 5 6 7 8 9 10 11
DocumentRoot "/var/www/html/example.com/public_html" ... <IfModule prefork.c> StartServers 5 MinSpareServers 20 MaxSpareServers 40 MaxRequestWorkers 256 MaxConnectionsPerChild 5500 </IfModule>
These settings can also be added to a separate file. The file must be located in the
conf.module.d
orconf
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.
Within the
conf.d
directory createvhost.conf
to store your virtual host configurations. The example below is a template for websiteexample.com
; change the necessary values for your domain:- /etc/httpd/conf.d/vhost.conf
-
1 2 3 4 5 6 7 8 9 10
NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/html/example.com/public_html/ ErrorLog /var/www/html/example.com/logs/error.log CustomLog /var/www/html/example.com/logs/access.log combined </VirtualHost>
Additional domains can be added to the
vhost.conf
file as needed. To add domains, copy theVirtualHost
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:Note
ErrorLog
andCustomLog
entries are suggested for more specific logging, but are not required. If they are defined (as shown above), thelogs
directories must be created before you restart Apache.Create the directories referenced above:
sudo mkdir -p /var/www/html/example.com/{public_html,logs}
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
:
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.
Secure Your Site with SSL
To add additional security to your site, consider enabling a Secure Sockets Layer (SSL) certificate.
Install and Configure GlusterFS, Galera, and XtraDB for High Availability
Consult our Host a Website with High Availability guide to mitigate downtime through redundancy, monitoring, and failover.
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.
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.