Note:

Install the NGINX Web Server and Proxy on Oracle Linux 8

Before You Begin

This tutorial provides instructions for installing the NGINX web server and enabling it on Oracle Linux 8.

Background

NGINX is a lightweight HTTP/S server that is capable of higher performance and lower memory use than a typical Apache web server deployment. However, this performance gain comes at the cost of some functionality and flexibility. NGINX has also gained in popularity as a powerful proxy service that is capable of functioning as a direct HTTP proxy, a reverse proxy with caching, an SMTP, POP3 or IMAP proxy or as a generic TCP/UDP proxy. NGINX also provides load balancing services with fault tolerance.

On earlier Oracle Linux versions, the NGINX web server was provided as part of a software collection package. The web server package is now directly available from the Oracle Linux 8 Application Streams repository and therefore simpler to deploy and configure.

What Do You Need?

(Hands-on Lab) Connect to the Compute Instance

Note: This step is specific to the Oracle provided free lab environment.

The Desktop environment will display before the instance(s) are ready. Deployment of this environment can take two to five minutes, depending on the number of resources and provisioning steps needed.

First, to access the lab compute instance(s), connect to the Oracle Cloud Console and copy the compute instance Public IP address.

  1. Sign in to Oracle Cloud Console, and select your Compartment.

  2. Click Instances.

  3. Copy the Public IP to a temporary location (such as a text file) on your computer.

    copy public ip

    To copy, highlight the IP address with the mouse and press Ctrl+C.

  4. Right-click the Virtual Desktop and select Open Terminal Here.

  5. Connect to the instance.

    ssh oracle@<IP_ADDRESS_OF_COMPUTE_INSTANCE>
    

    Where <IP_ADDRESS_OF_COMPUTE_INSTANCE> is the IP address copied from the Oracle Cloud Console.

  6. Accept the ECDSA key fingerprint by typing yes at the prompt.

  7. You are now connected to the compute instance for this lab.

If the connection fails with the Permission denied (publickey,gssapi-keyex,gssapi-with-mic) message, wait a bit longer for the provisioning process to complete and try making the ssh connection again.

Install and enable NGINX

Install the NGINX package

The following command installs the NGINX package and all of its dependencies:

sudo dnf install -y nginx

Enable and start the NGINX service

To enable and start the NGINX service for immediate access and make the service start automatically after a reboot, run the following command:

sudo systemctl enable --now nginx.service

The service starts a web server that listens on TCP port 80 by default. To check the status of the service, run this command:

sudo systemctl status nginx

Configure firewall rules (Optional)

If you are using a custom firewall profile or an Oracle Cloud Infrastructure instance, open the firewall port for the NGINX web service (80).

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

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

Note that in the demonstration environment, the instance has a public facing IP address and no network security is applied. Depending on your production environment, you may need to configure additional security list rules or update your network security group configuration.

Test your deployment

With your web browser, go to the domain name or IP address of the compute instance. This is the same IP address that you used to SSH into the instance.

http://<IP_address>/

The NGINX web server opens the default test page /usr/share/nginx/html/index.html.

Create a custom NGINX configuration

To change the root path for your web server, do not edit the /etc/nginx/nginx.conf file directly. Instead, as a preferred method, create a site-specific configuration in the /etc/nginx/conf.d directory. For example, create the file /etc/nginx/conf.d/default.conf and populate it with a configuration for your site.

The following steps can be used to set up a dedicated site configuration:

  1. Create a directory to host a new site

    sudo mkdir /srv/website
    
  2. Create an index.html file to display to visitors to the new site.

    cat << EOF | sudo tee /srv/website/index.html
    <html>
    <head>
    <title>Hello</title>
    </head>
    <body><p>Hello World!</p></body>
    </html>
    EOF
    
  3. Update the permissions to provide ownership of the directory to the nginx process and to set the appropriate SELinux security context.

    sudo chown -R nginx:nginx /srv/website
    sudo chcon -Rt httpd_sys_content_t /srv/website
    
  4. Create a custom NGINX configuration at /etc/nginx/conf.d/default.conf for visitors to the server IP address.

    cat <<EOF | sudo tee /etc/nginx/conf.d/default.conf
    server {
      server_name    <IP_address>;
      root           /srv/website;
      index          index.html;
    }
    EOF
    

Replace the <IP_address> value with the public IP address for the instance.

  1. Restart the NGINX web service to load the new configuration.

    sudo systemctl restart nginx
    
  2. Confirm that the page is updated when you visit the public IP address for the instance by using a web browser.

  3. You can debug and view any connection issues by tailing the log files:

    sudo tail -f /var/log/nginx/access.log -f /var/log/nginx/error.log
    

    Use Ctrl-C to exit the tail application when you are finished watching the log files.

Configure HTTPS to secure your service

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

Configure your TLS/SSL certificates

Oracle strongly recommends using a TLS certificate that has been signed by an external Certficate Authority (CA). See https://docs.oracle.com/en/operating-systems/oracle-linux/certmanage/ for more information.

  1. For the purpose of this demonstration, you can use a self-signed certificate. To create the certificate and key, run the following command:

    openssl req -new -x509 -days 30 -nodes -newkey rsa:2048 -keyout server.key\
    -out server.crt -subj "/C=US/ST=Ca/L=Sunnydale/CN=<IP_address>"
    

    Replace the <IP_address> value with the public IP address for the instance.

  2. Create a directory to store the keys and certificates for NGINX.

    sudo mkdir -p /etc/pki/nginx/private
    
  3. Copy the certificate to /etc/pki/nginx/server.crt and the key file to /etc/pki/nginx/private/server.key.

    sudo cp server.crt /etc/pki/nginx/
    sudo cp server.key /etc/pki/nginx/private
    

Update the NGINX configuration

  1. Replace the /etc/nginx/conf.d/default.conf file to include a configuration for a TLS enabled web site and include a 301 redirect for HTTP traffic to be referred to the HTTPS site.

    cat <<'EOF' | sudo tee /etc/nginx/conf.d/default.conf
     server {
     server_name   <IP_address>;
     return 301 https://$host$request_uri;
    }
    
    server {
     listen       443 ssl http2;
     listen       [::]:443 ssl http2;
     server_name    <IP_address>;
     root           /srv/website;
     index          index.html;
     ssl_certificate "/etc/pki/nginx/server.crt";
     ssl_certificate_key "/etc/pki/nginx/private/server.key";
     ssl_session_cache shared:SSL:1m;
     ssl_session_timeout  10m;
     ssl_ciphers PROFILE=SYSTEM;
     ssl_prefer_server_ciphers on;
    }
    EOF
    

    Replace the <IP_address> values with the public IP address for the instance.

    Note that if you are hosting for multiple domains, you can specify different ssl_certificate and ssl_certificate_key values for each server_name configuration that you create in the /etc/nginx/conf.d directory.

  2. Restart the NGINX service to load the new configuration.

    sudo systemctl restart nginx
    

Configure the firewall (optional)

Enable the firewall port (443) for the NGINX HTTPS web service and reload the default firewall service.

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

Confirm that HTTPS is working correctly

Open a browser and navigate to http://<IP_address>/. The browser should redirect to https://<IP_address>/.

Most browsers display a security risk warning when accessing a site that uses a self-signed certificate. You can accept the risk warning in this case to confirm that the site is working as expected.

The warning is not displayed if you use a CA signed certificate.

Additional Information

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.