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.

Prerequisites
- A server running CentOS Stream, Rocky Linux or AlmaLinux.
- A user with
sudorights. - 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 nginxOption 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=trueThen install:
sudo dnf install -y nginxCheck the version with nginx -v.
Step 2: Start and enable the service
sudo systemctl enable --now nginx
sudo systemctl status nginxThe 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 --reloadNow open http://your_server_ip in a browser. You should see the default welcome page.
Step 4: Manage Nginx
| Task | Command |
|---|---|
| Stop | sudo systemctl stop nginx |
| Start | sudo systemctl start nginx |
| Restart | sudo systemctl restart nginx |
| Reload config without dropping connections | sudo systemctl reload nginx |
| Test configuration | sudo 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
| Path | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main configuration file |
/etc/nginx/conf.d/*.conf | Site (server block) configuration files |
/usr/share/nginx/html | Default web root |
/var/log/nginx/access.log | Access log |
/var/log/nginx/error.log | Error 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.htmlThen 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/wwwFinally, test and reload:
sudo nginx -t && sudo systemctl reload nginxBest practices
- Use one file per site in
conf.d, named after the domain. - Hide the version number with
server_tokens off;in thehttpblock. - 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.

Pingback: Nginx interview Questions and Answers - Youngster Company