history 34 of 36
Install, Configure and Test Apache on CentOS and Red Hat
Installing Apache httpd with yum on CentOS or Red Hat, starting it at boot, enabling name-based virtual hosting on port 80 and testing the default page.
Written
- Web servers
- Written for CentOS
- Written for Red Hat
- Written for Apache
From my Linux study notes, 2018. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.
These are my steps for installing the Apache HTTP Server (httpd) on CentOS or Red Hat, getting it to start at boot, turning on name-based virtual hosting and checking that it works. They use service, chkconfig and the NameVirtualHost directive, which belong to older releases such as CentOS 5 and 6 (Apache 2.2). See the notes at the end for current systems.
Before you start
-
Run the commands as root (or with
sudo). -
Back up the main config file before editing it:
Terminal window cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
How to install Apache Server on CentOS, Red Hat and Linux
-
Install the Apache HTTP Server:
Terminal window yum install httpdNote: this is often installed with CentOS by default, depending on the packages chosen during installation. If it is already installed,
yumtells you so and makes no changes.
How to configure Apache Server on CentOS, Red Hat and Linux
-
Set the Apache service to start on boot:
Terminal window chkconfig --levels 235 httpd onThis turns
httpdon for runlevels 2, 3 and 5. Runlevel 3 is multi-user text mode and 5 is the graphical desktop, which are the ones normally used on a server. Runningchkconfig httpd onwithout--levelsuses the default runlevels (2, 3, 4 and 5). Check the result with:Terminal window chkconfig --list httpd -
Enable name-based virtual hosting on port 80. Name-based virtual hosting lets one server and one IP address host several websites, with Apache choosing the site from the domain name the browser asks for.
-
Open the httpd configuration file located at
/etc/httpd/conf/httpd.conf:Terminal window vi /etc/httpd/conf/httpd.conf -
Uncomment the line containing the text
NameVirtualHost *:80, by removing the#at the start of the line:NameVirtualHost *:80 -
Save the file.
-
Restart the Apache HTTP Server daemon:
Terminal window service httpd restartNote: ignore the “NameVirtualHost *:80 has no VirtualHosts” warning for now. It only means you have not yet added any
<VirtualHost *:80>blocks; it goes away once you do.
-
How to test Apache Server on CentOS, Red Hat and Linux
- Visit
localhostin your web browser on the server. You should see an Apache test page.
If the server has no desktop or browser, test from the command line:
curl -I http://localhostA line starting with HTTP/ shows that Apache answered. The default test page may return a 403 Forbidden status when there is no index.html yet; that still means Apache is running.
To test from another computer, browse to the server’s IP address. If that fails but localhost works, the firewall is probably blocking port 80 (see below).
Notes for today
-
CentOS end of life: CentOS 6 reached end of life on 30 November 2020 and CentOS 7 on 30 June 2024. Rocky Linux and AlmaLinux are the common free replacements; Debian and Ubuntu are alternatives.
-
Start at boot on systemd systems: runlevels and
chkconfigwere replaced. Use:Terminal window sudo systemctl enable --now httpdenablestarts Apache at boot and--nowalso starts it immediately. Restart withsudo systemctl restart httpd. -
NameVirtualHost is no longer needed. In Apache 2.4 (CentOS 7 and later) the
NameVirtualHostdirective has no effect and is deprecated; name-based virtual hosting works automatically when you add<VirtualHost *:80>blocks. You can skip step 2 on current systems. -
Packages:
yumbecamednfon newer Red Hat-family systems. On Debian and Ubuntu the package and service are calledapache2:sudo apt install apache2. -
Firewall: on Rocky Linux and AlmaLinux, allow web traffic with:
Terminal window sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --reload -
Before restarting after any config change, check the syntax with
apachectl configtest, which should printSyntax OK. -
Apache’s official documentation is at https://httpd.apache.org/docs/.