Nginx 开源版编译安装教程

  • Nginx 开源版编译安装教程已关闭评论
  • 193 次浏览
  • A+
所属分类:linux技术
摘要

本文示例在虚拟机 Rocky Linux 8.5 系统中编译安装 Nginx 开源版软件。若提示“./configure: error: C compiler cc is not found”表示缺少 C 编译器 cc 依赖:


本文示例在虚拟机 Rocky Linux 8.5 系统中编译安装 Nginx 开源版软件。

1. 下载并解压

# dnf install -y wget wget https://nginx.org/download/nginx-1.21.6.tar.gz tar -zxvf nginx-1.21.6.tar.gz 

2. 依赖检查和编译安装

2.1 配置检查依赖库并创建 Makefile

cd nginx-1.21.6 # --prefix 参数配置表示将 make install 将会把 nginx 安装到该目录中去 ./configure --prefix=/usr/local/nginx 

若提示“./configure: error: C compiler cc is not found”表示缺少 C 编译器 cc 依赖:

Nginx 开源版编译安装教程

安装缺少的 C 编译器 cc 依赖:

dnf install -y gcc 

安装成功之后,如果没有再缺少其他的依赖的话,configure 应该是可以继续进行的了,我们再次检查一下:

./configure --prefix=/usr/local/nginx 

还是缺少 PCRE 依赖库:

Nginx 开源版编译安装教程

安装缺少的 PCRE 依赖库:

dnf install -y pcre pcre-devel 

安装成功之后,再次检查一下:

./configure --prefix=/usr/local/nginx 

依然缺少 zlib 依赖库:

Nginx 开源版编译安装教程

安装缺少的 zlib 依赖库:

dnf install -y zlib zlib-devel 

安装成功之后,再次检查一下:

./configure --prefix=/usr/local/nginx 

不再缺少依赖,配置检查依赖库并创建 Makefile 成功,配置结果如下:

Nginx 开源版编译安装教程

2.2 编译安装

make make install 

2.3 删除源码安装包(可选)

cd .. rm -rf nginx-1.21.6 nginx-1.21.6.tar.gz 

3. 关闭防火墙

3.1 关闭防火墙

systemctl stop firewalld.service 

3.2 禁止开机自启

systemctl disable firewalld.service 

3.3 放行端口

如果不想关闭防火墙的话,可以考虑放行端口:

firewall-cmd --zone=public --add-port=80/tcp --permanent 

然后重启防火墙,让它生效:

firewall-cmd --reload 

4. 启动 Nginx

4.1 启动 Nginx

cd /usr/local/nginx/sbin/ ./nginx 

打开浏览器,地址栏输入 http://<虚拟机 IP>,查看 nginx 是否启动成功:

Nginx 开源版编译安装教程

出现以上“Welcome to nginx!”页面时,即提示 nginx 已启动成功!

4.2 常用相关命令

./nginx           # 启动 ./nginx -s stop   # 快速停止 ./nginx -s quit   # 优雅关闭退出,在退出前先完成已经接受的连接请求 ./nginx -s reload # 重新加载配置 

5. 安装成系统服务

5.1 创建 Nginx 系统服务脚本

vim /usr/lib/systemd/system/nginx.service 

i 输入以下内容:

[Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target  [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecQuit=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true  [Install] WantedBy=multi-user.target 

:wq 保存退出。

5.2 重新加载系统服务

systemctl daemon-reload 

5.3 启动 Nginx 系统服务

先停掉前面 4.1 小节中手动启动的 Nginx 进程:

# cd /usr/local/nginx/sbin ./nginx -s stop 

启动 Nginx 系统服务:

systemctl start nginx.service 

查看启动状态:

systemctl status nginx.service 

Nginx 开源版编译安装教程

查看 Nginx 进程:

ps -ef | grep nginx 

Nginx 开源版编译安装教程

5.4 设置开机自启

systemctl enable nginx.service