nginx stream 模块编译安装

###  一下为nginx-1.25.3 版本为例,环境 Cent OS 7.6 !

一、下载编辑

在 CentOS 中安装 Nginx 并包含特定模块,你需要从源代码编译 Nginx。以下是具体步骤:

1. 首先,你需要安装一些必要的开发工具和库。在命令行中输入以下命令:

```bash
sudo yum install gcc-c++ pcre pcre-devel zlib zlib-devel make openssl openssl-devel
```

2. 然后,你需要下载 Nginx 的源代码。你可以从 Nginx 的官方网站下载最新版本的源代码,或者直接使用 wget 命令下载:

```bash
wget http://nginx.org/download/nginx-1.25.3.tar.gz
```

3. 解压下载的源代码包:

```bash
tar -zxvf nginx-1.25.3.tar.gz
```

4. 进入源代码目录:

```bash
cd nginx-1.25.3
```

5. 配置编译选项。你可以根据需要选择你要包含的模块。下面的命令包含了你提供的 Nginx 版本中的所有模块:

```bash
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
```

6. 编译和安装 Nginx:

```bash
make
sudo make install
```

7. 最后,你可以使用以下命令来检查 Nginx 是否已经正确安装,以及是否包含了你需要的模块:

```bash
nginx -V
```

这个命令应该会显示出你的 Nginx 版本,以及你在编译时包含的模块。

也可以使用 nginx -v 查看版本号

补充如果:错误提示 nginx: [emerg] getpwnam("nginx") failed ,自行修改

  1. 打开 Nginx 的配置文件。你可以使用 vi 或 nano 等文本编辑器来打开它:
sudo vi /etc/nginx/nginx.conf
  1. 找到 #user nobody; 这一行,移除前面的 # 注释符,并将 nobody 改为 root(当前用户),使其变成 user root(当前用户);

请注意,编译安装 Nginx 需要一定的 Linux 基础知识,并且如果你在编译或安装过程中遇到任何问题,你可能需要根据错误信息来解决。

 

二、添加守护进程

从错误信息来看,系统中似乎没有找到名为 `nginx.service` 的服务单元。这可能是因为你是通过源码编译安装的 Nginx,而不是通过包管理器(如 yum 或 apt)安装的,因此没有自动创建 systemd 服务文件。你可以手动创建一个 systemd 服务文件来管理 Nginx。

以下是一个基本的 Nginx systemd 服务文件示例,你可以将其保存为 `/etc/systemd/system/nginx.service`:

```bash
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
```

你可以根据你的实际情况修改上面的文件路径和参数。

创建服务文件后,你需要重新加载 systemd 的配置,然后启动 Nginx:

```bash
sudo systemctl daemon-reload
sudo systemctl start nginx
```

如果你想让 Nginx 在系统启动时自动运行,你可以使用以下命令:

```bash
sudo systemctl enable nginx
```

请注意,你需要有 root 权限才能执行上述操作。

 

三、启动找不到目录问题解决(目前解决没有client_temp 目录找不到)

从你的 journalctl 输出中,我们可以看到新的错误信息:`mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)`。这表明 Nginx 试图访问 `/var/cache/nginx/client_temp` 目录,但是这个目录不存在。

你可以通过以下命令来创建这个目录,并给 "nginx" 用户和组赋予相应的权限:

```bash
sudo mkdir -p /var/cache/nginx/client_temp
sudo chown -R nginx:nginx /var/cache/nginx
```

这里,`mkdir -p` 命令会创建指定的目录,如果父目录不存在,它也会被创建。`chown -R nginx:nginx` 命令会将 `/var/cache/nginx` 及其所有子目录的所有者和组更改为 "nginx"。

创建目录并更改权限之后,你可以再次尝试启动 Nginx 服务:

```bash
sudo systemctl start nginx.service
```

然后,你可以通过以下命令来检查 Nginx 服务的状态:

```bash
systemctl status nginx.service
```

 

四、重新编译(如果不小心把nginx.conf 配置弄错了可以重新编译)

如果你需要重新编译 Nginx,你可以按照以下步骤进行:

1. 首先,你需要停止运行的 Nginx 服务。你可以使用以下命令:

```bash
sudo systemctl stop nginx
```

或者如果你的系统没有使用 systemd,你可以使用以下命令:

```bash
sudo service nginx stop
```

2. 然后,进入你之前解压 Nginx 源代码的目录,执行 `make clean` 命令来清除之前的编译结果:

```bash
cd nginx-1.25.3
make clean
```

3. 重新执行 `configure` 命令来配置编译选项。你可以修改 `configure` 命令的参数来包含或排除你需要的模块。例如:

```bash
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
```

4. 重新执行 `make` 和 `sudo make install` 命令来编译和安装 Nginx:

```bash
make
sudo make install
```

5. 重新启动 Nginx 服务:

```bash
sudo systemctl start nginx
```

或者如果你的系统没有使用 systemd,你可以使用以下命令:

```bash
sudo service nginx start
```

6. 检查 Nginx 的版本和模块,确保你的修改已经生效:

```bash
nginx -V
```

请注意,重新编译 Nginx 时,你需要确保 Nginx 服务已经停止,否则可能会出现问题。此外,你也需要确保你的编译选项是正确的,否则 Nginx 可能无法正确编译或运行。

 

posted @   Monarsoft  阅读(785)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示