Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Apache»How to Install and Configure Apache on CentOS/RHEL 8

    How to Install and Configure Apache on CentOS/RHEL 8

    RahulBy RahulNovember 8, 20194 Mins ReadUpdated:January 12, 2021

    CentOS 8 is the latest release of CentOS Linux operating system, which is based on Red Hat Enterprise Linux 8. In this tutorial, we will help you to install the Apache web server on CentOS 8 or RHEL 8 system with additional configuration and security.

    Prerequsities

    • SSH access to CentOS/RHEL 8 system
    • Sudo privileges to user to install packages

    Step 1 – Install Apache on CentOS 8

    First of all, Login to your CentOS 8 or RHEL 8 system via SSH. Then install Apache2 HTTP server packages using the following command. This will also install additional required packages on your system.

    sudo dnf install httpd
    

    Wait for the installation complete

    Step 2 – Manage Apache Service

    Apache service is managed with systemctl command line on CentOS/RHEL 8. After installation, use the following command to enable the Apache service and then start it.

    sudo systemctl enable httpd.service
    sudo systemctl start httpd.service
    

    Here are the other commands to stop and restart Apache service via command line.

    sudo systemctl stop apache2.service
    sudo systemctl restart apache2.service
    

    Step 3 – Test Apache Setup

    You can view the installed Apache version details using the following command.

    httpd -v
    
    Server version: Apache/2.4.37 (centos)
    Server built:   Oct  7 2019 21:42:02
    

    Create a test html page under default document root directory (/var/www/html).

    sudo echo "Hello TecAdmin.net" > /var/www/html/index.html
    

    Now access your Apache server using the server’s IP address or a domain pointed to the server IP.

    Apache on CentOS 8

    Step 4 – Creating VirtualHost

    Let’s create the first virtual host on your Apache server. For the tutorial, we are using the sample domain “example.com”. Here we will create a virtual host for example.com on port 80.

    Create a sample index file in a directory:

    sudo mkdir -p /var/www/example.com
    echo "hello example.com" | sudo tee /var/www/example.com/index.html
    

    Then create Virtualhost configuration file and edit in editor:

    sudo vim /etc/httpd/conf.d/example.com.conf
    

    Add the following content at the end of the configuration file. You may change the domain name as per your domain.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <VirtualHost *:80>
        ServerAdmin admin@example.com
        DocumentRoot /var/www/example.com
        ServerName example.com
        ServerAlias www.example.com
     
        <Directory /var/www/example.com>
               #Allowoverride all    ###Uncomment if required
        </Directory>
     
        ErrorLog logs/example.com_error.log
        CustomLog logs/example.com_access.log combined
    </VirtualHost>

    Save the Virtualhost configuration file and reload the Apache service using the following commands:

    sudo systemctl reload httpd.service
    

    Step 5 – Configure SSL VirtualHost

    You can skip this step if you don’t need SSL. But the security is always the primary concern for any website. To use SSL with Apache, install the mod_ssl package on your system.

    sudo dnf install mod_ssl
    

    For the tutorial, I have followed these instructions to generate a self signed SSL certificate for our domain.

    You can either use /etc/httpd/conf/ssl.conf for SSL virtual host or You can use a separate Virtual host configuration file for your domain. For example:

    sudo vim /etc/httpd/conf.d/example.com_ssl.conf
    

    with the following content:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <VirtualHost *:443>
        ServerAdmin admin@example.com
        DocumentRoot /var/www/example.com
        ServerName example.com
        ServerAlias www.example.com
        <Directory /var/www/example.com>
               #Allowoverride all    ###Uncomment if required
        </Directory>
     
        SSLEngine on
        SSLCertificateFile /etc/pki/tls/certs/example.com.crt
        SSLCertificateKeyFile /etc/pki/tls/certs/example.com.key
        ErrorLog logs/example.com_ssl-error.log
        CustomLog logs/example.com_ssl-access.log combined
    </VirtualHost>

    Here is three terms used to configure SSL virtualhost:

    • SSLEngine – Set this to “on”
    • SSLCertificateFile – Set the path of your SSL certificate
    • SSLCertificateKeyFile – This is the private key files used to generate SSL certificate

    After that enable the Virtualhost and reload the Apache service using the following commands:

    sudo systemctl reload apache2.service
    

    Step 6 – Secure Apache Server

    Security is the most important part of hosting. Hackers are ready to exploit your web server. Edit Apache main configuration file

    sudo vim /etc/httpd/conf/httpd.conf
    

    Add the following values at the end of the file:

    1
    2
    3
    ServerTokens Prod
    ServerSignature Off
    TraceEnable Off

    After that edit the Apache default SSL configuration file:

    sudo vim /etc/httpd/conf.d/ssl.conf
    

    Here are the multiple security-related settings. Add or Update the following settings. We are not going in detailed descriptions about it but these settings are very useful for the production servers.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #Rules taken from https://cipherli.st/
     
    SSLCipherSuite EECDH+AESGCM:EDH+AESGCM
    # Requires Apache 2.4.36 & OpenSSL 1.1.1
    SSLProtocol -all +TLSv1.3 +TLSv1.2
    SSLOpenSSLConfCmd Curves X25519:secp521r1:secp384r1:prime256v1
    # Older versions
    # SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLHonorCipherOrder On
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    Header always set X-Frame-Options DENY
    Header always set X-Content-Type-Options nosniff
    # Requires Apache >= 2.4
    SSLCompression off
    SSLUseStapling on
    SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
    # Requires Apache >= 2.4.11
    SSLSessionTickets Off

    After making changes restart the Apache service to apply the new configuration.

    sudo systemctl reload apache2.service
    

    Conclusion

    All done, You are running a secured Apache server on your CentOS 8 or RHEL 8 Linux system.

    Apache CentOS 8 RHEL 8
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install and Configure Apache on Debian 10
    Next Article How To Install PostgreSQL Server on CentOS 8

    Related Posts

    How To Install Apache Solr 9.0 on Fedora 36/35

    Updated:May 26, 20223 Mins Read

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How To Disable HTTP Methods in Apache

    Updated:December 31, 20212 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    Common Apache Commands on Ubuntu & Debian

    4 Mins Read

    2 Comments

    1. Mike on January 11, 2021 11:28 pm

      Too much ambiguity. You are giving the https virtual host the same name in the same directory as the http host. Shouldn’t the http host be handing off any http queries to https? So both are needed and the http cannot be overwritten.

      Reply
    2. psb on July 22, 2020 11:38 am

      how to upgrade httpd server

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.