Linux常用命令--源代码安装nginx
摘要
-
虽然绝大部分软件都可以通过
rpm或者yum的方式进行安装,但是由于yum中的版本不一定是最新版,或者软件开发商没有将软件放到yum源中,所以也有一些软件需要通过源代码的方式进行安装。 -
本文以源代码安装nginx为例说明如何通过源代码安装软件。
-
本文基于
CentOS8(x86_64)
安装nginx
-
yum 安装
1 | # 安装nginx最小依赖 |
-
源码安装
1 | # 安装nginx最小依赖 |
-
上面就完成了nginx安装,之后我们可以根据需要对配置文件进行修改,并启动nginx
1 | # 假设我们将nginx安装到了自定义安装路径/usr/local/nginx-1.22.1下 |
为nginx添加新的模块
-
nginx安装成功后,发现有一些其他模块没有编译进去,或者想额外添加一些模块,这时候就要重新编译nginx。
-
重新编译之前,需要查看之前安装时的参数
1 | $ nginx -V |
-
configure arguments中就是之前编译安装时配置的参数 -
如添加新的模块
http_gzip_static_module
1 | # 进入nginx源码目录 |
-
安装第三方模块,
configure中需要添加--add-module=module_dir,具体查看第三方模块官网说明即可
nginx编译时可以添加哪些参数
-
以下是通过
yum安装nginx时的编译参数
1 | # 指定nginx安装路径,至少web根目录在该路径下 |
-
其它参数
1 | # 启用ipv6支持 |
nginx编译安装出现的常见错误
-
参照
yum安装nginx的配置进行手工编译
1 | ./configure --prefix=/usr/local/nginx-1.22.1 --user=nginx --group=nginx --with-compat --with-debug \ |
-
./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option.
1 | yum install pcre pcre-devel |
-
./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.
1 | yum install openssl openssl-devel |
-
./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries.
1 | yum install libxml2 libxml2-devel libxslt libxslt-devel |
-
./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
1 | yum install gd gd-devel |
-
./configure: error: perl module ExtUtils::Embed is required
1 | yum install perl-ExtUtils-Embed |
-
./configure: error: the Google perftools module requires the Google perftools library. You can either do not enable the module or install the library
1 | yum install google-perftools google-perftools-devel |
Nginx 配置文件
1 | # 全局参数设置 |
-
日志格式参数说明
| 参数名 | 含义 | 示例值 |
|---|---|---|
| remote_addr | 客户端IP地址 | 192.168.0.1 |
| remote_user | 客户端用户(通常为“-”) | - |
| time_local | 日志记录时间(本地时区) | 02/May/2023:14:43:58 +0800 |
| request | 请求行,如 “GET /index.html …” | “GET / HTTP/1.1” |
| status | HTTP状态码 | 200 |
| body_bytes_sent | 发送给客户端的主体字节数 | 612 |
| http_referer | 来源页面 | “https://domain.com/” |
| http_user_agent | 用户代理信息 | “Mozilla/5.0 (…)” |
| http_x_forwarded_for | 代理转发的原始IP地址链 | 192.168.0.1, 10.0.0.2 |
-
更多变量可参考官方文档:https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
-
tomcat.conf
1 | upstream tomcats { # 定义名为 tomcats 的后端服务器组,用于负载均衡 |
-
redis.conf
1 | upstream redis{ |
-
upstream配置说明
1 | upstream还可以为每个设备设置状态值,这些状态值的含义分别如下: |
一个https配置示例
1 | upstream jenkins { |
配置为系统服务
-
yum安装自带系统服务
1 | # 进入service服务目录 |