SoFunction
Updated on 2025-05-21

Interpretation of Nginx1.28.0 source code compilation and installation and systemd management methods under Ubuntu

1. Environment and dependency preparation

To ensure smooth compilation, we first update the system and install the necessary compilation tools and libraries:

sudo apt update
sudo apt install -y build-essential \
                    libpcre3 libpcre3-dev \
                    zlib1g zlib1g-dev \
                    libssl-dev \
                    wget
  • build-essential:supplygccmakeBasic compilation tools
  • libpcre3 / libpcre3-dev: Support regular matching (such asrewriteModule)
  • zlib1g / zlib1g-dev: Provides gzip compression function
  • libssl-dev: Enable HTTPS/SSL support
  • wget: Used to download source code packages

2. Download and decompress the Nginx source code

  • Switch to the user's home directory (or other working directory)
  • Download and decompress the source code package
cd ~
wget /download/nginx-1.28.
tar zxvf nginx-1.28.
cd nginx-1.28.0

If you have already packaged the source codenginx-1.28.Place it in the local directory and execute it the sametar zxvfAnd enter the unzipped directory.

3. Configure compilation options

use./configureThe script specifies the installation path and required modules for the compilation process.

In this example, common functions such as HTTP, SSL, HTTP/2, gzip, status monitoring, asynchronous I/O, threading, and Stream module are enabled:

./configure \
  --prefix=/usr/local/nginx \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module \
  --with-http_realip_module \
  --with-threads \
  --with-file-aio \
  --with-stream \
  --with-stream_ssl_module \
  --with-stream_realip_module
  • --prefix: Specify the installation directory
  • --with-http_stub_status_module: Open the run status page (can be used for monitoring)
  • The remaining modules can be added and deleted according to actual needs. implement./configure --helpAll available options are available.

4. Compilation and installation

  • Compilation: According to machine performance, the execution time is usually around a few minutes
  • Install: Copy the compiled results to the specified directory
make
sudo make install
  • After compilation is completed, the executable file is located in/usr/local/nginx/sbin/nginx
  • Default main configuration file:/usr/local/nginx/conf/
  • Log Directory:/usr/local/nginx/logs/

5. Create a systemd service unit

In order to facilitate the self-start and unified system management, it is recommended to create a new systemd service file:

sudo tee /etc/systemd/system/ > /dev/null << 'EOF'
[Unit]
Description=NGINX HTTP and reverse proxy server
After=

[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
PIDFile=/usr/local/nginx/logs/
PrivateTmp=true

[Install]
WantedBy=
EOF

Then execute:

sudo systemctl daemon-reload
sudo systemctl enable nginx

6. Startup, reload and status check

  • Start Nginx
sudo systemctl start nginx
  • Check the running status
sudo systemctl status nginx
  • Smooth reload configuration(Reviseback)
sudo systemctl reload nginx
  • Stop Nginx
sudo systemctl stop nginx

7. Firewall settings and access verification

If the system has UFW firewall enabled, release the HTTP/HTTPS port:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

If you access the server IP or bound domain name in the browser, if the default Nginx welcome page appears, it means that the installation and deployment are successful.

8. Common troubleshooting

Port occupied

sudo lsof -i:80

If other services are occupied, the Nginx listening port needs to be stopped or modified.

Configuration file syntax error

/usr/local/nginx/sbin/nginx -t

Check and correct the error before overloading.

Log viewing

  • Access log:/usr/local/nginx/logs/
  • Error log:/usr/local/nginx/logs/

Summarize

This article introduces in detail the entire process of compiling and installing Nginx 1.28.0 from source code on Ubuntu, covering dependency environment preparation, source code download and decompression, configuration and compilation options, make installation, systemd service management and common troubleshooting methods.

In this way, you can flexibly customize Nginx functions according to business needs and better integrate them into the production operation and maintenance system.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.