WindowsMacSoftwareSettingsSecurityAndroidProductivityLinuxPerformanceAppleDevice Manageme.. All

How to Install and Configure Nginx on Fedora

Edited 1 week ago by ExtremeHow Editorial Team

FedoraNginxInstallationWeb ServerConfigurationSoftwareCommand LineTerminalHostingComputers

How to Install and Configure Nginx on Fedora

This content is available in 7 different language

Introduction

The purpose of this guide is to help you install and configure Nginx on a Fedora system. Nginx is a popular open-source web server known for its high performance, stability, and low resource consumption. It is capable of handling a large number of concurrent connections, making it a preferred choice for many developers. By the end of this guide, you should have a working Nginx installation ready to serve web content.

Preparing your system

Before starting the installation, make sure your system is updated to the latest version. Updating ensures that you get the latest features and security fixes for all packages, including Nginx.

To update your system, open a terminal and run the following command:

sudo dnf update -y

-y flag automatically answers 'yes' during the update process. After the update is complete, proceed to install Nginx.

Installing Nginx on Fedora

Fedora's package manager, DNF, can be used to install Nginx from its repositories. The installation process is simple.

Enter the following commands into your terminal:

sudo dnf install nginx -y

This command tells DNF to install the latest version of Nginx available in the repository. If prompted, enter your password to authorize the installation process. Once finished, Nginx is installed on your system but not yet started.

Starting and enabling Nginx

After installation, you’ll want to start the Nginx service and make sure it runs automatically on boot.

Use the following command to start Nginx:

sudo systemctl start nginx

To start Nginx on boot, use:

sudo systemctl enable nginx

To verify that Nginx is running, execute:

sudo systemctl status nginx

If everything is set up correctly, the status command will show Nginx active and running.

Configuring Nginx

Configuration of Nginx is made possible through its configuration files, primarily located at /etc/nginx/nginx.conf. This file defines the main settings, including user permissions, error logs, and worker connections.

Understanding the main configuration file structure

The nginx.conf file contains several directives that are organized into contexts, such as main {}, events {}, and http {} Inside http context, you can have server blocks that define server-specific configuration.

A basic server block might look like this:

server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } }

This block tells Nginx to listen on port 80 for requests directed to 'example.com'. root directive specifies where the HTML files are stored, while index directive lists the default files to serve.

Virtual host

Nginx supports hosting multiple websites using the concept of virtual hosts. Each site is defined by server block in the configuration file.

Here's an example of two virtual hosts on the same server:

server { listen 80; server_name site1.com; location / { root /var/www/site1; index index.html; } } server { listen 80; server_name site2.com; location / { root /var/www/site2; index index.html; } }

Each server_name refers to a different website. Requests for 'site1.com' or 'site2.com' will be served different content depending on their respective root directories.

Testing the configuration

After editing the configuration files, it is necessary to test them for syntax errors before deploying them. You can do this easily:

sudo nginx -t

If no errors are reported, go ahead and reload the Nginx service to apply the new configuration:

sudo systemctl reload nginx

Firewall configuration

Make sure your firewall settings allow HTTP and HTTPS traffic for Nginx to serve web pages. Use the following firewall-cmd command to allow these services:

sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload

Securing Nginx with SSL

Securing Nginx with SSL certificates provides a layer of security by encrypting data transmitted between your server and clients. You can use free SSL certificates from Let's Encrypt.

First, install the Certbot tool for Let's Encrypt:

sudo dnf install certbot python3-certbot-nginx -y

Get a certificate using Certbot:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Follow the on-screen instructions to complete the setup and install a valid SSL certificate. Certbot will automatically configure your Nginx server block to use SSL.

Monitoring and logs

Nginx logs are important for monitoring traffic and troubleshooting problems. Access logs record client requests, while error logs store Nginx server errors.

By default, these logs are located in the /var/log/nginx/ directory:

Access Log: /var/log/nginx/access.log Error Log: /var/log/nginx/error.log

Examine these logs using a command like tail -f /var/log/nginx/access.log to see incoming requests in real-time.

Conclusion

Nginx is a versatile and robust web server solution for Fedora, capable of hosting multiple sites and serving a large number of clients simultaneously. By understanding its configuration and basic operations, you can effectively leverage Nginx to meet your web server needs.

If you find anything wrong with the article content, you can


Comments