1.Preparation: Download the Nginx Source Package
-
Visit the Nginx Official Website to Obtain the Source Code
Open your browser and visit the Nginx official download page: https://nginx.org/en/download.html, and select the appropriate stable version (for example, nginx-1.24.0, the stable version is usually more suitable for production environments).
-
Download the source package via command line
Log in to the Linux server, navigate to a temporary directory (e.g., /tmp), and use thewget command to download the corresponding version of the source package (using 1.24.0 as an example):
|
cd /tmp wget https://nginx.org/download/nginx-1.24.0.tar.gz |
If the server does not havewget, you can install it first usingyum install -y wget.
2.Install Dependency Environment
Nginx compiles with several system libraries and tools that need to be installed in advance to avoid errors during compilation:
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
-
gcc: C language compiler used for compiling Nginx source code
-
pcre/pcre-devel: Supports regular expressions, Nginx’s URL matching relies on this library
-
zlib/zlib-devel: Used for HTTP data compression functionality
-
openssl/openssl-devel: Supports HTTPS (SSL/TLS) functionality
3.Extract the Source Package and Enter the Directory
After downloading, extract the source package and enter the extracted directory:
# Extracttar.gz package
tar -zxvf nginx-1.24.0.tar.gz
# Enter the extractedNginxsource directory
cd nginx-1.24.0
4.Configure Compilation Options (Customize Features)
Use the./configure command to configure Nginx’s installation path and enabled modules, here is an example of common configurations:
bash ./configure \
–prefix=/usr/local/nginx \ # Specify installation directory (recommended path for easier management)
–with-http_stub_status_module \ # Enable status monitoring module (can view connection counts and other information)
–with-http_ssl_module \ # EnableHTTPS support (essential for deployingSSL certificates)
–with-http_sub_module \ # EnableHTTP content replacement module (can replace response content)
–with-http_gzip_static_module \ # Enable static filegzip compression module (optimizes transmission speed)
–with-stream \ # EnableTCP/UDP proxy module (supports reverse proxy for nonHTTP services, such as databases)
–user=nginx \ # Specify the user to runNginx (must create later for enhanced security)
–group=nginx # Specify the user group to runNginx
After configuration, aMakefile file will be generated for subsequent compilation. If there are errors during configuration, it is usually due to incomplete dependencies; install according to the error prompts.
5.Compile and Install
1).Compile the Source Code Execute themake command to compile the source code into executable files (this process may take a few minutes, depending on server performance):
make
2).Install to the Specified Directory After compilation, executemake install to install Nginx to the previously specified–prefix directory/usr/local/nginx:
make install
6.Create a Running User (Enhance Security)
To avoid security risks of running Nginx as the root user, create a dedicatednginx user and group (if specified during configuration with–user and–group):
groupadd nginx # Create user group
useradd -g nginx -s /sbin/nologin -M nginx # Create user (-s specifies no login,-M does not create home directory)
7.Verify Installation and Familiarize with Directory Structure
After installation, the/usr/local/nginx directory will generate the following key subdirectories:
-
sbin: Stores the Nginx main program (nginx executable file)
-
conf: Stores configuration files (the core configurationnginx.conf is in this directory)
-
html: Default website root directory (stores static pages)
-
logs: Stores log files (access logs, error logs, etc.)
You can verify the installation success by checking the version:
/usr/local/nginx/sbin/nginx -v # Outputting version information indicates successful installation
8.Nginx Common Operation Commands
After entering the Nginx execution directory (sbin), you can execute the following commands:
cd /usr/local/nginx/sbin
# StartNginx
./nginx
# Check if the configuration file syntax is correct (must do after modifying configuration)
./nginx -t
# Reload the configuration file (no need to restart after modifying configuration, smooth effect)
./nginx -s reload
# Safely stopNginx (exit after processing the current request)
./nginx -s quit
# Force stopNginx (terminate immediately, may lose requests)
./nginx -s stop
# ViewNginx process status
ps -ef | grep nginx
9.Set to Start on Boot (Optional)
To avoid having to manually start Nginx after a server reboot, you can configure it to start on boot usingsystemd:
1).Create a service file:
vim /usr/lib/systemd/system/nginx.service
2).Write the following content:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3).Execute the command
systemctl daemon-reload # Reload service configuration
systemctl enable nginx # Set to start on boot
systemctl start nginx # Start the service
By following the above steps, you can complete the source installation of Nginx on a Linux system;
10.nginx.conf Configuration
Regardingnginx configuration,location will be detailed in the next section, stay tuned!