ubuntu安装nginx最新稳定版 1.30.3

📅 发布时间:2026/7/12 18:43:53
ubuntu安装nginx最新稳定版 1.30.3 从源码编译安装可以自定义编译参数或添加第三方模块灵活性高。1. 安装编译依赖sudo apt update sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev -y # ubuntu26.04如果找不到libpcre3-dev # 下载 PCRE 源码 cd /usr/local/src # 下载地址1 sudo wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz sudo tar -xzf pcre-8.45.tar.gz # 下载地址2如果地址1不可用请尝试地址2 sudo wget http://mirror.dotsrc.org/tinycorelinux/14.x/x86_64/tcz/src/pcre/pcre-8.45.tar.bz2 tar -jxvf pcre-8.45.tar.bz22. 下载并解压源码cd /usr/local sudo wget https://nginx.org/download/nginx-1.30.3.tar.gz sudo tar -zxvf nginx-1.30.3.tar.gz cd nginx-1.30.33. 配置、编译与安装# 配置编译选项 sudo ./configure \ --prefix/usr/local/nginx \ --userwww \ --groupwww \ --with-http_ssl_module \ --with-http_stub_status_module # 如果找不到libpcre3-dev使用以下配置 ./configure --prefix/usr/local/nginx \ --userwww \ --groupwww \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-pcre/usr/local/src/pcre-8.45 # 编译并安装 sudo make sudo make install4. 管理Nginx服务# 创建www用户 sudo adduser --system --group www # 赋予www用户可写权限 chown www:www /usr/local/nginx/logs/ # 启动Nginx sudo /usr/local/nginx/sbin/nginx # 重载配置 sudo /usr/local/nginx/sbin/nginx -s reload # 停止Nginx sudo /usr/local/nginx/sbin/nginx -s stop5.启动后通过浏览器或curl访问测试页面curl -I http://localhost后续配置1.配置防火墙如果启用了ufw需要放行 HTTP 和 HTTPS 流量。# 开放80 HTTP sudo ufw allow 80/tcp # 开放443 HTTPS sudo ufw allow 443/tcp # 或 sudo ufw allow http sudo ufw allow https2.设置开机自启a. 创建服务文件/etc/systemd/system/nginx.service。b. 写入以下内容[Unit] DescriptionThe NGINX HTTP and reverse proxy server Afternetwork.target remote-fs.target nss-lookup.target [Service] Typeforking PIDFile/usr/local/nginx/logs/nginx.pid ExecStartPre/usr/local/nginx/sbin/nginx -t ExecStart/usr/local/nginx/sbin/nginx ExecReload/usr/local/nginx/sbin/nginx -s reload ExecStop/usr/local/nginx/sbin/nginx -s stop PrivateTmptrue #Nginx在异常退出后自动重启 Restarton-failure RestartSec5 [Install] WantedBymulti-user.targetc. 重新加载 systemd 配置sudo systemctl daemon-reloadd.测试启动与配置检查# 检查配置语法systemd 的 ExecStartPre 也会做但手动验证更安心 sudo /usr/local/nginx/sbin/nginx -t # 使用 systemctl 启动 Nginx sudo systemctl start nginx # 如果启动失败可以通过以下命令查看详细错误 sudo systemctl status nginx # 查看服务状态和错误信息 sudo journalctl -u nginx -f # 实时查看 Nginx 的 systemd 日志e.设置开机自启# 确认服务能正常启动后启用开机自启 sudo systemctl enable nginxf.排错小贴士PID 文件路径不匹配如果 Nginx 的nginx.conf中使用了不同的 PID 文件路径例如/var/run/nginx.pid请在 service 文件中修改PIDFile为对应路径。权限问题确保 Nginx 二进制文件/usr/local/nginx/sbin/nginx和日志目录/usr/local/nginx/logs/对运行用户www可读/可写。你可以在nginx.conf中配置user指令。端口绑定如果 Nginx 监听 80 或 443 端口确保这些端口未被占用如 Apache且防火墙已放行。日常管理命令配置好 systemd 服务后你可以像管理其他系统服务一样管理 Nginx操作命令启动sudo systemctl start nginx停止sudo systemctl stop nginx重启先停再启sudo systemctl restart nginx重载配置不停机sudo systemctl reload nginx查看状态sudo systemctl status nginx开机自启sudo systemctl enable nginx取消开机自启sudo systemctl disable nginx