Installing Nginx on Rocky Linux 9

Installing Nginx on Rocky Linux 9

Hello everyone, I am Xing Ge. Nginx has become one of the mainstream choices for web service deployment due to its high performance, low resource consumption, and excellent reverse proxy capabilities.

This article will guide you through the process of installing and configuring the Nginx service from scratch on Rocky Linux 9, suitable for beginners and operations enthusiasts to get started quickly.

Installing Nginx on Rocky Linux 9

Quick Installation

gitee (domestic):
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/Rocky_Linux_9_Dnf_Install_Nginx.sh
sh Rocky_Linux_9_Dnf_Install_Nginx.sh

github:
wget https://raw.githubusercontent.com/funet8/Rocky-Linux-Shell/refs/heads/main/shell/Rocky_Linux_9_Dnf_Install_Nginx.sh
sh Rocky_Linux_9_Dnf_Install_Nginx.sh

# Main features introduction
# 1. dnf install nginx
# 2. firewall-cmd open ports 80 and 443
# 3. Nginx configuration files:
# Main configuration file: /data/conf/nginx.conf
# Site configuration file: /data/conf/sites-available/nginx_*

1. Update System and Install EPEL

# Update system
dnf update -y

# Install EPEL repository (to avoid dependencies)
dnf install epel-release -y

2. Install Nginx

Start and set to start on boot

dnf install nginx -y
# Start and set to start on boot
systemctl start nginx
systemctl enable nginx

3. Configure Firewall to Allow HTTP and HTTPS

# Configure firewall to allow HTTP and HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload

4. Configure Nginx Directory and Configuration Files

# Set configuration file directory
wget -q -O - https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/create_dirs.sh | bash -sh
# Move Nginx configuration file
cp -p /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak
rm -rf /etc/nginx/nginx.conf
cd /data/conf/
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/nginx.conf
ln -s /data/conf/nginx.conf /etc/nginx/
echo "nginx.conf move success"

# Site configuration
cd /data/conf/sites-available/
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/nginx_main.conf

5. Create User and User Group

Create www user

and set directory permissions

# Add www group and www user
groupadd www
useradd -g www www

# Set directory permissions##########################################################################
chown -R www:www /data/wwwroot/web
chown -R www:www /data/conf/sites-available/
# Permission issues will cause error 403
chmod 755 -R /data/

# Delete default site files
rm -rf /usr/share/nginx/html/*
echo 'index page' > /usr/share/nginx/html/index.html
chown www.www -R /usr/share/nginx/html/

6. Check if Nginx Started Successfully

# Check if started successfully
systemctl restart nginx
systemctl status nginx | grep Active

echo "Nginx installation and startup completed."
echo "Please visit http://<your server IP> to verify if Nginx is running."

7. Scheduled Task for Log Rotation

Crontab scheduled daily log rotation

### Log rotation
cd /data/conf/shell/
wget https://gitee.com/funet8/Rocky-Linux-Shell/raw/main/shell/nginx_cut_web_log.sh
chmod +x /data/conf/shell/nginx_cut_web_log.sh
echo "00 00 * * * root /data/conf/shell/nginx_cut_web_log.sh" >> /etc/crontab
systemctl restart crond

Basic Configuration File Path Description

Item Path
Main configuration file <span>/data/conf/nginx.conf</span>
Site configuration directory <span>/data/conf/sites-available/nginx_*</span>
Default site file <span>/usr/share/nginx/html/index.html</span>
Log files <span>/data/wwwroot/log/nginx_access.log</span> <span>/data/wwwroot/log/nginx_error.log</span>

Common Nginx Commands

# Reload configuration
sudo nginx -s reload

# Check if configuration is correct
sudo nginx -t

# Stop service
sudo systemctl stop nginx

# Restart service
sudo systemctl restart nginx

At this point, you have successfully deployed Nginx on Rocky Linux 9 and completed the basic service startup and firewall configuration. You can continue to expand on advanced configurations such as HTTPS, reverse proxy, load balancing, and static resource optimization.

Leave a Comment