ubuntu24.04 编译安装 Nginx 1.26.2

写在前面

如题,本文将给出基于ubuntu24.04系统通过编译安装 Nginx 1.26.2 , 并将其配置为系统服务的完整方法。

安装步骤

1. gcc 安装

cd ~
apt update
apt install build-essential libpcre3-dev libssl-dev
gcc --version

2. prce 安装

版本查询

wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.44/pcre2-10.44.tar.gz
tar -zxvf pcre2-10.44.tar.gz
cd pcre2-10.44/
./configure
make
make install

3. zlib 安装

版本查询

cd ~
wget https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
tar -zxvf zlib-1.3.1.tar.gz
cd zlib-1.3.1/
./configure
make
make install

4. openssl安装

版本查询

cd ~
wget https://github.com/openssl/openssl/releases/download/openssl-3.3.1/openssl-3.3.1.tar.gz
tar -zxvf openssl-3.3.1.tar.gz
cd openssl-3.3.1/
./config --prefix=/usr
make

5. nginx 1.26.2(当前最新版) 安装

版本查询
编译模块查询

cd ~
wget https://nginx.org/download/nginx-1.26.2.tar.gz
tar -zxvf nginx-1.26.2.tar.gz
cd nginx-1.26.2/

./configure \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--with-pcre=../pcre2-10.44 \
--with-zlib=../zlib-1.3.1 \
--with-openssl=../openssl-3.3.1 \
--with-stream \
--with-stream_ssl_preread_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module

make
make install

6. 软连接

软连接用于解决可以执行 nginx -v 命令

ln -s /usr/local/nginx/nginx /usr/local/sbin/nginx

7. 配置为系统服务

执行命令vim /etc/systemd/system/nginx.service 在文件中写入以下内容并保存:

[Unit]
Description = nginx daemon

[Service]
ExecStart = /usr/local/nginx/nginx
ExecStop = /usr/local/nginx/nginx -s stop
ExecReload = /usr/local/nginx/nginx -s reload
Restart = always
Type = forking

[Install]
WantedBy = multi-user.target  

8. 测试

测试命令

root@it-nginx:~# systemctl daemon-reload
root@it-nginx:~# systemctl status nginx.service 
root@it-nginx:~# systemctl enable nginx.service
root@it-nginx:~# systemctl restart nginx.service
root@it-nginx:~# systemctl stop nginx.service   
root@it-nginx:~# nginx -V

命令执行记录

root@it-nginx:~# systemctl daemon-reload
root@it-nginx:~# systemctl status nginx.service 
● nginx.service - nginx daemon
     Loaded: loaded (/etc/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Thu 2024-08-29 06:04:10 UTC; 12min ago
   Main PID: 58062 (nginx)
      Tasks: 2 (limit: 9488)
     Memory: 1.6M (peak: 1.8M)
        CPU: 10ms
     CGroup: /system.slice/nginx.service
             ├─58062 "nginx: master process /usr/local/nginx/nginx"
             └─58063 "nginx: worker process"

Aug 29 06:04:10 it-nginx systemd[1]: Starting nginx.service - nginx daemon...
Aug 29 06:04:10 it-nginx systemd[1]: Started nginx.service - nginx daemon.
root@it-nginx:~# systemctl stop nginx.service 
root@it-nginx:~# systemctl status nginx.service 
○ nginx.service - nginx daemon
     Loaded: loaded (/etc/systemd/system/nginx.service; enabled; preset: enabled)
     Active: inactive (dead) since Thu 2024-08-29 06:16:49 UTC; 3s ago
   Duration: 12min 38.999s
    Process: 58175 ExecStop=/usr/local/nginx/nginx -s stop (code=exited, status=0/SUCCESS)
   Main PID: 58062 (code=exited, status=0/SUCCESS)
        CPU: 22ms

Aug 29 06:04:10 it-nginx systemd[1]: Starting nginx.service - nginx daemon...
Aug 29 06:04:10 it-nginx systemd[1]: Started nginx.service - nginx daemon.
Aug 29 06:16:49 it-nginx systemd[1]: Stopping nginx.service - nginx daemon...
Aug 29 06:16:49 it-nginx systemd[1]: nginx.service: Deactivated successfully.
Aug 29 06:16:49 it-nginx systemd[1]: Stopped nginx.service - nginx daemon.
root@it-nginx:~# systemctl start nginx.service 
root@it-nginx:~# systemctl status nginx.service 
● nginx.service - nginx daemon
     Loaded: loaded (/etc/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Thu 2024-08-29 06:17:01 UTC; 1s ago
    Process: 58186 ExecStart=/usr/local/nginx/nginx (code=exited, status=0/SUCCESS)
   Main PID: 58187 (nginx)
      Tasks: 2 (limit: 9488)
     Memory: 1.6M (peak: 1.9M)
        CPU: 11ms
     CGroup: /system.slice/nginx.service
             ├─58187 "nginx: master process /usr/local/nginx/nginx"
             └─58188 "nginx: worker process"

Aug 29 06:17:01 it-nginx systemd[1]: Starting nginx.service - nginx daemon...
Aug 29 06:17:01 it-nginx systemd[1]: Started nginx.service - nginx daemon.
root@it-nginx:~# nginx -V
nginx version: nginx/1.26.2
built by gcc 13.2.0 (Ubuntu 13.2.0-23ubuntu4) 
built with OpenSSL 3.3.1 4 Jun 2024
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre=../pcre2-10.44 --with-zlib=../zlib-1.3.1 --with-openssl=../openssl-3.3.1 --with-stream --with-stream_ssl_preread_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值