Install the Apache Web Server (2024)

Note:

  • This tutorial requires access to Oracle Cloud. To sign up for a free account, see Get started with Oracle Cloud Infrastructure Free Tier.
  • It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.

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

  • Any system with Oracle Linux 8 installed

Objectives

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

  • Install and configure the Apache package
  • Configure the firewall
  • Apply Apache configurations
  • Secure web services

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 --permanentsudo 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/htmlsudo echo "example.com" > /var/www/example.com/html/index.htmlsudo 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_sslsudo 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 onSSLCertificateFile /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 --permanentsudo 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.

Title and Copyright Information

Install the Apache Web Server (2024)

FAQs

How do I install Apache web server? ›

Step by step Apache install on Windows
  1. Download the installation media in the form of a ZIP file.
  2. Extract the contents of the Apache Web Server 2.4 zip to the file system.
  3. Locate the extracted Apache24 folder and copy this folder to the root of C:\
  4. Open the C:\Apache24\bin folder and run the httpd.exe command.
Jan 15, 2022

How to install Apache server in Linux command? ›

Type sudo apt-get install apache2 in Terminal.

This command will download and install the Apache server on your Linux machine. This command will work in Ubuntu and other Debian Linux versions.

How do I get to Apache server? ›

To access your web server, simple enter 'localhost' or the standard IP address, '127.0. 0.1' into the search bar of any web browser.

Is the Apache web server free? ›

Apache is an open-source and free web server software that powers around 40% of websites around the world.

Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5882

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.