ag@linux:~$

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

  1. Install the Apache HTTP Server:

    Terminal window
    yum install httpd

    Note: this is often installed with CentOS by default, depending on the packages chosen during installation. If it is already installed, yum tells you so and makes no changes.

How to configure Apache Server on CentOS, Red Hat and Linux

  1. Set the Apache service to start on boot:

    Terminal window
    chkconfig --levels 235 httpd on

    This turns httpd on 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. Running chkconfig httpd on without --levels uses the default runlevels (2, 3, 4 and 5). Check the result with:

    Terminal window
    chkconfig --list httpd
  2. 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.

    1. Open the httpd configuration file located at /etc/httpd/conf/httpd.conf:

      Terminal window
      vi /etc/httpd/conf/httpd.conf
    2. Uncomment the line containing the text NameVirtualHost *:80, by removing the # at the start of the line:

      NameVirtualHost *:80
    3. Save the file.

    4. Restart the Apache HTTP Server daemon:

      Terminal window
      service httpd restart

      Note: 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

  1. Visit localhost in 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:

Terminal window
curl -I http://localhost

A 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 chkconfig were replaced. Use:

    Terminal window
    sudo systemctl enable --now httpd

    enable starts Apache at boot and --now also starts it immediately. Restart with sudo systemctl restart httpd.

  • NameVirtualHost is no longer needed. In Apache 2.4 (CentOS 7 and later) the NameVirtualHost directive 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: yum became dnf on newer Red Hat-family systems. On Debian and Ubuntu the package and service are called apache2: sudo apt install apache2.

  • Firewall: on Rocky Linux and AlmaLinux, allow web traffic with:

    Terminal window
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --reload
  • Before restarting after any config change, check the syntax with apachectl configtest, which should print Syntax OK.

  • Apache’s official documentation is at https://httpd.apache.org/docs/.