Introduction to Nginx: Installation and Configuration Methods
What is Nginx?
Nginx is an open-source, high-performance HTTP server and reverse proxy server (middleware) developed by Igor Sysoev, commonly used for load balancing.
Key features and advantages of Nginx include:
- High Performance: Capable of handling a large number of concurrent connections, excelling in high-concurrency scenarios.
- Lightweight: Low resource consumption and high operational efficiency.
- Reverse Proxy: Can forward client requests to multiple backend servers, enabling load balancing and improving system availability and scalability.
- Static and Dynamic Separation: Effectively separates static resources (e.g., images, CSS, JavaScript files) from dynamic content (e.g., PHP, Python scripts), enhancing server performance.
- Flexible Configuration: Supports a wide range of configuration options to meet various complex business requirements.
- Caching Functionality: Can cache frequently accessed content, reducing server load and improving response speed.
- High Stability: Widely tested and validated, offering excellent stability and reliability.
Due to its outstanding performance and rich feature set, Nginx is extensively used in the internet industry and has become a crucial tool for building efficient and reliable network services.
Installing Nginx
2.1 YUM Installation
According to the Nginx official website, you can first create an nginx.repo file in the /etc/yum.repos.d directory to define the Nginx repository. (If your server has the EPEL repository installed, you can directly use YUM for installation.)
vim /etc/yum.repos.d/nginx.repo
Add the following content:
[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
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
Install Nginx:
yum install nginx -y
Start Nginx:
systemctl start nginx
Enable Nginx to start on boot:
systemctl enable nginx
2.2 Compilation Installation
Access a new web server (this compilation installation is performed on the web2 server).
Create a /nginx directory and download the source code package into it.
mkdir /nginx
cd /nginx
curl -O https://nginx.org/download/nginx-1.26.1.tar.gz
Extract the source code package:
tar xf nginx-1.26.1.tar.gz
configure is a script provided by Nginx for compilation configuration, primarily used to collect user-specified configurations and parameters.
For example, users can specify the installation path, enable or disable certain modules (features), and customize the Nginx software.
| Module | Function |
|---|---|
--prefix=PATH |
Specify installation path |
--user=USER |
Specify the username for starting worker processes |
--group=GROUP |
Specify the user group for starting worker processes |
--with-http_ssl_module |
Enable SSL functionality (not enabled by default) |
--with-http_v2_module |
Enable HTTP/2 module support |
--with-http_v3_module |
Enable HTTP/3 module support |
--with-threads |
Support multi-threading functionality |
--with-stream |
Support reverse proxy functionality |
--with-http_stub_status_module |
Status statistics functionality |
Resolve dependency software packages:
yum install gcc pcre-devel openssl-devel -y
Configuraiton:
cd nginx-1.26.1
./configure --prefix=/usr/local/nginx-custom --user=webuser --group=webgroup --with-http_ssl_module --with-http_v2_module --with-threads --with-stream --with-http_stub_status_module
Start 2 processes for compilation (choose based on your CPU core count; select the number of processes equal to the number of cores):
make -j 2
Install the compiled binaries and configuration files to the /usr/local/nginx-custom directory:
make install
Create the user webuser:
useradd webuser -s /sbin/nologin
Modify the PATH environment variable to include the Nginx installation directory for easy access:
echo 'PATH=/usr/local/nginx-custom/sbin/:$PATH' >> /etc/profile
source /etc/profile
Start Nginx (ensure the firewall is disabled and SELinux is stopped):
/usr/local/nginx-custom/sbin/nginx
For Nginx installed via compilation, the following commands can be used:
| Command | Purpose |
|---|---|
nginx -s stop |
Stop Nginx |
nginx -s reload |
Reload Nginx configuration |
To streamline the compilation installation process, you can encapsulate the above steps into a shell script for future use.
vim install_nginx.sh
Add the following content:
#!/bin/bash
# Create directory for Nginx source code
mkdir -p /nginx
# Navigate to directory and download Nginx source package
cd /nginx
curl -O https://nginx.org/download/nginx-1.26.1.tar.gz
# Extract source package
tar xf nginx-1.26.1.tar.gz
cd nginx-1.26.1
# Resolve dependency packages
yum install gcc pcre-devel openssl-devel -y
# Configure
./configure --prefix=/usr/local/nginx-custom --user=webuser --group=webgroup --with-http_ssl_module --with-http_v2_module --with-threads --with-stream --with-http_stub_status_module
# Compile using 2 processes
make -j 2
# Install
make install
# Create user webuser
useradd webuser -s /sbin/nologin
# Update PATH environment variable
echo 'PATH=/usr/local/nginx-custom/sbin/:$PATH' >> /etc/profile
source /etc/profile
# Start Nginx
/usr/local/nginx-custom/sbin/nginx
# Disable firewall and set it not to start on boot
systemctl stop firewalld
systemctl disable firewalld
# Temporarily and permanently disable SELinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/sysconfig/selinux
Make the script executable and run it:
chmod +x install_nginx.sh
./install_nginx.sh
2.3 Enabling Auto-start for Compiled Nginx
Method 1: Modify /etc/rc.local
Add /usr/local/nginx-custom/sbin/nginx to the end of the file.
Grant executable permission to the file:
chmod +x /etc/rc.d/rc.local
Method 2: Create an nginx.service file for systemctl management
cd /usr/lib/systemd/system
vim nginx.service
Add the following content:
[Unit]
Description=nginx - high performance web server
[Service]
Type=forking
PIDFile=/usr/local/nginx-custom/logs/nginx.pid
ExecStart=/usr/local/nginx-custom/sbin/nginx -c /usr/local/nginx-custom/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat $PIDFile)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat $PIDFile)"
[Install]
WantedBy=multi-user.target
Reload the systemd configuration:
systemctl daemon-reload
Stop any previously started Nginx processes:
killall nginx
Start Nginx via systemctl:
systemctl start nginx
Enable Nginx to start on boot:
systemctl enable nginx
Differences between YUM installation and compilation installation:
-
YUM Installation (
yum install nginx)- Configuration files:
/etc/nginx - Executable Nginx binary:
/usr/sbin/nginx - Web directory:
/usr/share/nginx/html
- Configuration files:
-
Compilation Installation
- Configuration files:
/usr/local/nginx-custom/conf - Executable Nginx binary:
/usr/local/nginx-custom/sbin - Web directory:
/usr/local/nginx-custom/html
- Configuration files: