Introduction
Nginx is a high-performance web server, reverse proxy server, and load balancer, widely used in modern internet architectures. It is known for its lightweight design, high concurrency handling capabilities, and low resource consumption. This article will provide a detailed guide on how to install and configure Nginx on a Linux system.
Installation Method Comparison
Source Installation
Advantages:
- Allows customization of compilation options as needed.
- Suitable for obtaining the latest version of software, especially those not yet available in distribution repositories.
Disadvantages:
- Requires manual resolution of dependencies, which can be complex and time-consuming.
- Software installed this way may not be easily tracked by package management systems, leading to management difficulties.
Package Manager Installation (e.g., APT, YUM)
Advantages:
- Automatically handles dependencies, simplifying the installation process.
- Software updates and uninstallation are convenient, as maintenance can be easily performed through the package manager.
- Provides a certain level of security, as packages are usually reviewed by distribution maintainers.
Disadvantages:
- May not provide the latest software versions, especially for older stable Linux distributions.
- Limited customization options for specific installation needs.
Docker Installation
Advantages:
- Provides a lightweight virtualization environment, allowing applications to run in isolated environments, reducing conflicts with other applications on the host.
- Simplifies the deployment process, especially during development and testing phases, enabling quick start and stop of services.
- Facilitates portability, as containerized applications can be easily migrated between different machines.
Disadvantages:
- Increases the learning curve, as users need to understand the basic concepts and commands of Docker.
- Relatively higher resource consumption, although smaller than traditional virtual machines, it is still higher than direct installation using package managers.
Other Installation Methods:
- Flatpak/Snap and other universal package formats: These are cross-distribution application distribution mechanisms designed to provide a more unified way to distribute and install applications. They offer container-like isolation but are aimed at desktop applications rather than server applications.
- Binary File Installation: Some software vendors may provide precompiled binary files or compressed packages directly, allowing users to download and extract them to a specified directory for use. This method is between source installation and package manager installation, suitable for those who do not want to deal with the compilation process but cannot obtain the required software through a package manager.
Package Manager Installation
“
Note: If yum installation fails, refer to the FAQ for solutions.
# Update all packages on the system to the latest version
sudo yum update
# Install Nginx
sudo yum install nginx -y
# Start Nginx service
sudo systemctl start nginx
# Set Nginx to start on boot
sudo systemctl enable nginx
# Check Nginx status
sudo systemctl status nginx
Package Manager Uninstallation
# 1. Stop Nginx service
sudo systemctl stop nginx # Stop service
sudo systemctl disable nginx # Disable startup on boot
# 2. Uninstall Nginx package
sudo yum remove nginx -y # Case 1: Uninstall Nginx installed via yum
sudo rpm -e nginx # Case 2: Uninstall Nginx installed manually via rpm
# 3. Remove residual files and configurations (optional)
sudo rm -rf /etc/nginx # Remove configuration directory
sudo rm -rf /var/log/nginx # Remove log files
sudo rm -rf /usr/share/nginx # Remove default web files
# 4. Clean up dependency packages (optional)
sudo yum autoremove -y # Automatically remove unused dependencies
# 5. Verify uninstallation
nginx -v # Should prompt "command not found"
sudo systemctl status nginx # Should prompt "Unit nginx.service could not be found"
# Complete uninstallation script (one-click execution)
sudo systemctl stop nginx
sudo systemctl disable nginx
sudo yum remove nginx -y
sudo rm -rf /etc/nginx /var/log/nginx /usr/share/nginx
sudo yum autoremove -y
Docker Installation
# 1. Create a local directory for mounting configuration files and web content
cd /opt && mkdir -p ./nginx/{config/{conf.d,cert},html,logs}
# 2. Copy configuration
# 1. Start a temporary container
docker run --name tmp-nginx -d nginx
# 2. Copy configuration
docker cp tmp-nginx:/etc/nginx/nginx.conf /opt/nginx/config/ &&
docker cp tmp-nginx:/etc/nginx/conf.d/default.conf /opt/nginx/config/conf.d/ &&
docker cp tmp-nginx:/usr/share/nginx/html/index.html /opt/nginx/html/
# 3. Remove temporary container
docker rm -f tmp-nginx
# 3. Start Nginx container
docker run -d \
--name nginx \
--restart unless-stopped \
-p 80:80 \
-p 443:443 \
-v /opt/nginx/config/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/config/conf.d:/etc/nginx/conf.d \
-v /opt/nginx/config/cert:/etc/nginx/cert \
-v /opt/nginx/html:/usr/share/nginx/html \
-v /opt/nginx/logs:/var/log/nginx \
nginx:latest
# 4. Other operations (optional)
docker exec -it nginx nginx -t # Test configuration file syntax
docker exec -it nginx nginx -s reload # Reload configuration
docker logs nginx # View logs
tail -f /opt/nginx/logs/access.log # View mounted log file
tail -f /opt/nginx/logs/error.log # View mounted log file
Docker Uninstallation
docker stop nginx
docker rm nginx
Docker-Compose Installation
Reference: https://gitee.com/neondeep/script/tree/master/linux/docker-compose/nginx, copy the relevant files to your installation directory.
version: '3.7'
services:
nginx:
image:nginx:1.26
container_name:nginx
restart:unless-stopped
ports:
-"80:80"
-"443:443"
volumes:
-/etc/localtime:/etc/localtime:ro
-./config/nginx.conf:/etc/nginx/nginx.conf
-./config/conf.d:/etc/nginx/conf.d
-./config/cert:/etc/nginx/cert
-./logs:/var/log/nginx
-./html:/usr/share/nginx/html
docker-compose up -d
Docker-Compose Uninstallation
docker-compose down
FAQ
CentOS Official Repository Unreachable
The CentOS official repository has been taken offline (maintenance stopped after 2024), and the official mirror sources may be unreachable. Therefore, it is recommended to change the YUM mirror source to Aliyun / Tencent Cloud / Huawei Cloud mirror sources.
# Step 1: Backup the original repository configuration
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/backup/
# Step 2: Download Aliyun mirror source
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# Step 3: Update YUM cache
yum clean all # Clean cache
yum makecache # Regenerate cache
Enable EPEL Repository
EPEL (Extra Packages for Enterprise Linux) is an extended repository for CentOS/RHEL that contains many additional software packages (such as Nginx).
- The default CentOS official repository does not include Nginx (Nginx is not in the CentOS base repository).
- The Aliyun mirror only synchronizes the CentOS official repository by default, while Nginx requires additional configuration of the EPEL repository or the Nginx official repository.
# Step 1: Install EPEL repository
yum install epel-release -y
# Step 2: Install Nginx again
yum install nginx -y
Using Nginx Official Repository (Latest Version)
If the Nginx version in EPEL is outdated, you can use the Nginx official repository:
Step 1: Add the Nginx official repository
# Create Nginx official repository file
cat > /etc/yum.repos.d/nginx.repo <<EOF
[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
EOF
Step 2: Install Nginx
yum install nginx -y
Nginx will be installed from the official source as the latest stable version.
Manual Download RPM Installation (If YUM Still Fails)
# Step 1: Download Nginx RPM package
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.1-1.el7.ngx.x86_64.rpm
# Step 2: Manual installation
rpm -ivh nginx-1.20.1-1.el7.ngx.x86_64.rpm
# Step 3: Start Nginx
systemctl start nginx
systemctl enable nginx # Set to start on boot