史上最全最详细Nginx源码编译安装的方法步骤

nginx作为现在最热门的web服务应用软件,日常安装及升级经常会使用,源码安装nginx详细教程

nginx作为现在最热门的web服务应用软件,日常安装及升级经常会使用

linux环境下安装nginx步骤

最新及历史版本下载地址: http://nginx.org/en/download.html

下载完成后上传到服务器

[root@haproxy2 opt]# rz -e #上传到服务器
[root@haproxy2 opt]# ll
total 1036
-rw-r--r-- 1 root root 1060580 Apr  1 12:40 nginx-1.19.9.tar.gz
[root@haproxy2 opt]# tar -zxf nginx-1.19.9.tar.gz 
[root@haproxy2 opt]# ll
total 1036
drwxr-xr-x 8 mockbuild mockbuild     158 Mar 30 22:47 nginx-1.19.9
-rw-r--r-- 1 root      root      1060580 Apr  1 12:40 nginx-1.19.9.tar.gz

安装依赖(不安装依赖完法完成安装nginx)

步骤需要服务器可以联网或者有自建yum源

[root@haproxy2 opt]# yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel 
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
 * webtatic: us-east.repo.webtatic.com
base                                                                                                                                                                    | 3.6 kB  00:00:00     
extras                                                                                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                                                                                 | 2.9 kB  00:00:00     
webtatic                                                                                                                                                                | 3.6 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                       | 230 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                      | 6.5 MB  00:00:00     
软件包 gcc-4.8.5-44.el7.x86_64 已安装并且是最新版本
软件包 pcre-8.32-17.el7.x86_64 已安装并且是最新版本
软件包 pcre-devel-8.32-17.el7.x86_64 已安装并且是最新版本
软件包 zlib-1.2.7-19.el7_9.x86_64 已安装并且是最新版本
软件包 zlib-devel-1.2.7-19.el7_9.x86_64 已安装并且是最新版本
软件包 1:openssl-1.0.2k-21.el7_9.x86_64 已安装并且是最新版本
软件包 1:openssl-devel-1.0.2k-21.el7_9.x86_64 已安装并且是最新版本
无须任何处理

配置nginx安装路径及需要的模块(如果不需要额外模块则去掉add-module)

[root@localhost opt]# cd nginx-1.19.9
[root@localhost nginx-1.19.9]# ./configure --prefix=/opt/nginx/ --add-module=/opt/nginx-module-vts
checking for OS
 + Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
checking for gcc -pipe switch ... found
checking for getaddrinfo() ... found
configuring additional modules
adding module in /opt/nginx-module-vts
 + ngx_http_vhost_traffic_status_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/opt/nginx/"
  nginx binary file: "/opt/nginx//sbin/nginx"
  nginx modules path: "/opt/nginx//modules"
  nginx configuration prefix: "/opt/nginx//conf"
  nginx configuration file: "/opt/nginx//conf/nginx.conf"
  nginx pid file: "/opt/nginx//logs/nginx.pid"
  nginx error log file: "/opt/nginx//logs/error.log"
  nginx http access log file: "/opt/nginx//logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@localhost nginx-1.19.9]# 

把nginx编译成二进制文件

[root@localhost nginx-1.19.9]# make -j 4

把编译好的nginx文件安装到上面指定(--prefix)的目录

[root@localhost nginx-1.19.9]# make install

查看nginx版本

[root@localhost nginx-1.19.9]# cd /opt/nginx/sbin
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.19.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/opt/nginx/ --add-module=/opt/nginx-module-vts

启动nginx

[root@localhost sbin]# /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

测试页面 http://192.168.93.131/

测试nginx配置文件是否正确

[root@localhost sbin]# /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# 

停止nginx

[root@localhost sbin]# /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -s stop

重启nginx

[root@localhost sbin]# /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -s reload
到此nginx安装完成
本此安装附带了nginx-module-vts模块,模块使用方法

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/145.html

发表评论

登录后才能评论