How to Install Apache on CentOS 7
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
This guide explains how to install and configure the 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 Linode.
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 the Users and Groups guide.Replace each instance of
example.com
in this guide with the domain name of the website.
Before you begin
Ensure you have followed both the Getting Started and Securing Your Server guides.
Check that the Linode’s hostname is set. To check the hostname run:
hostname hostname -f
The first command displays a short hostname, and the second displays the Fully Qualified Domain Name (FQDN).
Use
yum
to update the system and make sure everything is up to date:sudo yum update
Configure firewalld to allow web traffic
By default, CentOS 7 is set to block web traffic. Run the following commands to allow web traffic through the firewall:
sudo firewall-cmd --add-service=http --permanent && sudo firewall-cmd --add-service=https --permanent
sudo systemctl restart firewalld
Install and configure Apache
Installing Apache
Install Apache 2.4 using
yum
:sudo yum install httpd
After you confirm the installation, let
yum
install Apache and its dependencies.
Configuring httpd.conf
Before changing any configuration files, Linode recommends that you make a backup. To make a backup of the
httpd.conf
file, use:cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
Update the
httpd.conf
file with the document root directory in order to point Apache to the files of the website and also add the<IfModule prefork.c>
section to adjust the resource use settings (these are a good starting point for a Linode 2GB)- File: /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>
NoteThese settings may also be added to a separate file. The file must be located in either theconf.module.d
orconf
directories and must end in.conf
(as this is the format of files included in the resulting configuration).
Configuring a name-based virtual host
A single domain
There are multiple ways to set up a virtual host, but this section explains and recommends one of the easier methods.
Within the
conf.d
directory, create the filevhost.conf
to store the virtual host configurations.Edit
vhost.conf
using the following example. Remember to substitute the domain name forexample.com
.- File: /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>
Create the directories referenced in the previous step:
sudo mkdir -p /var/www/html/example.com/{public_html,logs}
Note
TheErrorLog
andCustomLog
entries are suggested for more specific logging, but are not required. If they are defined as in the previous step, create thelogs
directories before you restart Apache.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
Visit your domain to test the Apache server. A default Apache page will be visible if no index page is found in the document root declared in
httpd.conf
:
Multiple Domains
Additional domains may be used with the vhost.conf
file as necessary. When new requests come in from the internet, Apache checks which VirtualHost block matches the requested URL and serves the appropriate content:
To add additional domains, copy the example in the previous section and add it to the end of the existing file, modify the values for the new domain, create the directories, restart Apache, and test the newly added domain. Repeat as necessary.
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 the server, here are some optional steps.
Next Steps: Additional security and high availability
Secure the server with SELinux
SELinux is a mandatory access control (MAC) system that confines privileged processes and automates security policy creation. To enable it on your Linode, see the Beginner’s Guide to SELinux on CentOS 7.
Secure the site with SSL
To add additional security to the site, consider enabling a secure sockets layer (SSL) certificate.
Install and Configure GlusterFS, Galera, and XtraDB for High Availability
Consult the 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.
This page was originally published on