• Post author:Bhaskar Soni
  • Post published:
  • Post category:Linux
  • Post comments:1 Comment
  • Reading time:3 mins read

Nginx is a fast, lightweight web server and reverse proxy. This guide shows how to install Nginx on CentOS-family systems, manage the service, open the firewall and organise your configuration.

A quick note first: CentOS Linux 7 reached end of life on 30 June 2024, and CentOS Linux 8 ended in 2021. If you still run them, plan a migration. The steps below work on CentOS Stream 9, Rocky Linux 9 and AlmaLinux 9, and are very similar on version 10.

install Nginx on CentOS

Prerequisites

  • A server running CentOS Stream, Rocky Linux or AlmaLinux.
  • A user with sudo rights.
  • Optionally, a domain name pointing to the server’s IP address.

Step 1: Install Nginx

Option A: from the distribution repository

The AppStream repository includes a stable Nginx build, so this is the simplest method:

sudo dnf install -y nginx

Option B: from the official nginx.org repository

Use this method if you need a newer version. Create /etc/yum.repos.d/nginx.repo:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

Then install:

sudo dnf install -y nginx

Check the version with nginx -v.

Step 2: Start and enable the service

sudo systemctl enable --now nginx
sudo systemctl status nginx

The enable --now command starts Nginx immediately and also at every boot.

Step 3: Open the firewall

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

Now open http://your_server_ip in a browser. You should see the default welcome page.

Step 4: Manage Nginx

TaskCommand
Stopsudo systemctl stop nginx
Startsudo systemctl start nginx
Restartsudo systemctl restart nginx
Reload config without dropping connectionssudo systemctl reload nginx
Test configurationsudo nginx -t

Always run nginx -t before a reload. As a result, a typo will not take your site offline.

Step 5: Understand the configuration files

PathPurpose
/etc/nginx/nginx.confMain configuration file
/etc/nginx/conf.d/*.confSite (server block) configuration files
/usr/share/nginx/htmlDefault web root
/var/log/nginx/access.logAccess log
/var/log/nginx/error.logError log

Step 6: Create a server block

Create a web root and a test page:

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

Then create /etc/nginx/conf.d/example.com.conf:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html;
    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log;
}

On systems with SELinux enforcing, label the new directory so Nginx can read it:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?"
sudo restorecon -Rv /var/www

Finally, test and reload:

sudo nginx -t && sudo systemctl reload nginx

Best practices

  • Use one file per site in conf.d, named after the domain.
  • Hide the version number with server_tokens off; in the http block.
  • Add HTTPS as soon as the site works. See how to set up an SSL certificate for Nginx.
  • Keep SELinux enforcing rather than disabling it.

Related guides

Sources

Running Linux servers in production? Our Linux server hardening & security audit checks configuration, patching, SSH and logging. You can also use our free Linux server hardening checklist, or contact us.

Post author avatar

Bhaskar Soni

Bhaskar Soni is a Security Engineer and the founder of Youngster Company, based in Ahmedabad, Gujarat. He works hands-on with Linux server hardening, vulnerability assessment and penetration testing (VAPT), ISO 27001 compliance and OSINT investigations. He writes practical, tested guides for IT professionals and Indian businesses, and every article on this site is written or reviewed by him.

This Post Has One Comment

Leave a Reply