要在 Ubuntu 上安装 nginx 1.26.2,可以通过源码编译的方式进行安装。以下是详细的步骤:

1. 更新系统包

首先,确保你的系统是最新的:

sudo apt update
sudo apt upgrade

2. 安装依赖包

Nginx 编译需要一些依赖包,首先安装这些包:

sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev

这些包包括了编译和构建所需的工具和库。

3. 下载 Nginx 源码

从 Nginx 官方下载 1.26.2 版本的源码包:

wget http://nginx.org/download/nginx-1.26.2.tar.gz

4. 解压源码包

下载完成后,解压 Nginx 源码包:

tar -zxvf nginx-1.26.2.tar.gz
cd nginx-1.26.2

5. 配置编译选项

运行 ./configure 来配置 Nginx 编译选项。你可以根据需求选择不同的模块和选项。常见的选项有:

./configure --prefix=/etc/nginx --with-http_ssl_module --with-http_v2_module --with-stream
  • --prefix=/etc/nginx:设置 Nginx 的安装路径。
  • --with-http_ssl_module:启用 SSL 模块。
  • --with-http_v2_module:启用 HTTP/2 模块。
  • --with-stream:启用流式处理模块。

如果需要其他模块,可以根据需要添加更多的选项。可以通过 ./configure --help 查看所有可用的配置选项。

6. 编译和安装

配置完成后,开始编译和安装 Nginx:

make
sudo make install

这一步可能会花费一些时间。

7. 启动 Nginx

安装完成后,你可以通过以下命令启动 Nginx:

sudo /etc/nginx/sbin/nginx

8. 验证 Nginx 是否安装成功

打开浏览器,访问 http://localhost/,如果看到 Nginx 默认欢迎页面,则表示安装成功。

9. 配置 Nginx 自启动(可选)

如果你希望 Nginx 开机自动启动,可以使用 systemd 来配置:

sudo nano /etc/systemd/system/nginx.service

在文件中添加以下内容:

[Unit]
Description=The nginx web server
After=network.target

[Service]
ExecStart=/etc/nginx/sbin/nginx
ExecReload=/etc/nginx/sbin/nginx -s reload
ExecStop=/etc/nginx/sbin/nginx -s stop
PIDFile=/etc/nginx/logs/nginx.pid
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存并退出。然后启用并启动 Nginx 服务:

sudo systemctl enable nginx
sudo systemctl start nginx

10. 管理 Nginx 服务

  • 启动:sudo systemctl start nginx
  • 停止:sudo systemctl stop nginx
  • 重启:sudo systemctl restart nginx
  • 查看状态:sudo systemctl status nginx

11. 配置防火墙

如果你的服务器启用了 UFW 防火墙,允许 HTTP 和 HTTPS 流量:

sudo ufw allow 'Nginx Full'

以上就是在 Ubuntu 上从源码安装 Nginx 1.26.2 的完整步骤。如果在安装过程中有任何问题,请随时提问!

更多推荐