Note:

Install the Apache Web Server

Introduction

Apache has been in active development since 1993 and over time has become one of the most popular web servers in the world. The Apache web server is a key component of the LAMP (Linux, Apache, Oracle MySQL and Perl/PHP) software stack and continues to be widely used today.

The Apache web server is directly available from the Application Streams repository in Oracle Linux 8 or later and is simple to deploy and configure.

Prerequisites

Objectives

At the end of this tutorial, you should be able to do the following:

Installing and configuring the web server package

  1. Install the httpd package and its dependencies.

    sudo dnf install httpd
    
  2. Enable and start the httpd service.

    This command enables and starts the httpd service for immediate use and also starts the service automatically after a system reboot:

    sudo systemctl enable --now httpd.service
    

    To check the status of the service, type:

    sudo systemctl status httpd
    
  3. (Optional) If you are using a custom firewall profile or an Oracle Cloud Infrastructure instance, open the firewall port for the Apache web service (80).

    These commands enable the firewall port for the Apache web service and reload the default firewall service:

    sudo firewall-cmd --add-service=http --permanent
    sudo firewall-cmd --reload
    
  4. Test the deployment.

    With your web browser, go to the domain name or IP address of your system, for example, http://myserver.example.com. The Apache web server opens the default test page.

Configuring Apache

To change the root path for your web server, do not edit the /etc/httpd/conf/httpd.conf file directly. Instead, as a preferred method, create a site-specific configuration file in the /etc/httpd/conf.d directory.

In the following example, the file /etc/httpd/conf.d/example.com.conf is created to contain configurations for virtual hosts.

  1. Create virtual hosts by adding the following information in /etc/httpd/conf.d/example.com.conf:

    Listen *:80

    <VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/html/

    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined

    </VirtualHost>

    ServerName and ServerAlias can be hostnames, domain names, or IP addresses that can be used to access the service.

  2. Create the document root folder.

    The root folder hosts the web pages that Apache would provide to browsers.

    sudo mkdir -p /var/www/example.com/html
    sudo echo "example.com" > /var/www/example.com/html/index.html
    sudo chown -R apache:apache /var/www/example.com/html
    

    On systems where SELinux is enabled in enforcing mode and pages are not served from within the /var/www directory, you must apply the correct security context to the DocumentRoot directory. For example, to serve web pages from the /mnt/example.com directory, type:

    sudo semanage fcontext -a -t httpd_sys_content_t "/mnt/example.com(/.*)?"
    sudo restorecon -Rv /mnt/example.com/
    
  3. Apply your configuration.

    sudo systemctl restart httpd
    

Securing the web service

As a best practice, secure all communications between a web browser and your Apache server by using HTTPS. For a secure setup, a TLS certificate is required.

  1. Configure your TLS/SSL certificates.

Note:
Oracle strongly recommends that you use a TLS certificate that has been signed by an external Certificate Authority (CA). See Oracle Linux: Managing Certificates and Public Key Infrastructure for more information.

  1. Install the HTTPS add-on for Apache.

    sudo dnf install mod_ssl
    sudo systemctl restart httpd
    
  2. Update the Apache configuration. For example, in /etc/httpd/conf.d/example.com.conf, add the following configuration:

    Listen *:443

    <VirtualHost *:443>

    ServerName example.com
    ServerAlias www.example.com

    SSLEngine on SSLCertificateFile /etc/pki/tls/private/certificate.crt
    SSLCertificateKeyFile /etc/pki/tls/private/private.key

    DocumentRoot /var/www/example.com/html/

    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined

    </VirtualHost>

  3. Redirect HTTP requests to HTTPS.

    Replace your existing HTTP VirtualHost configuration with a redirect, as follows:

    <VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    Redirect “/” “https://example.com/”

    </VirtualHost>

  4. Restart the Apache service to load the new configuration.

    sudo systemctl restart httpd
    
  5. (Optional) Configure the firewall.

    These commands enable the firewall port 443 for the Apache HTTPS service and reloads the default firewall service:

    sudo firewall-cmd --add-service=https --permanent
    sudo firewall-cmd --reload
    

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.