nginx离线安装

安装指南:https://blog.csdn.net/jshxk/article/details/138518870
官方文档:https://nginx.org/en/docs/

一、前置技术

gcc、perl以及相关的依赖openssl、pcre2、zlib
nginx 下载地址:https://nginx.org/en/download.html
zlib 下载地址:https://www.zlib.net/fossils/
pcre 下载地址:https://sourceforge.net/projects/pcre/files/pcre/8.44/
openssl 下载地址:https://openssl-library.org/source/old/1.0.2/

(一) gcc安装

  1. 验证是否安装gcc
gcc -v
  1. 如果终端打印gcc相关版本信息,说明已经安装了gcc,否则需要安装gcc
    image

  2. 下载gcc安装包,下载地址:https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/

  3. 上传安装包至服务器/opt/rpms/gcc/目录中

  4. 进入安装包目录,一次性安装所有包,执行命令:rpm -ih *.rpm --nodeps --force

(二) perl安装

  1. 验证是否安装perl
perl -v
  1. 如果终端打印perl相关版本信息,说明已经安装了perl,否则需要安装perl
    image

  2. 下载地址:https://www.perl.org/get.html#unix_like
    image

  3. 上传至/opt/sources目录中,并执行以下命令

# 解压
tar -zxvf /opt/sources/perl-5.40.1.tar.gz -C /opt
cd /opt/perl-5.40.1
./Configure -des
make && make install

(三) 其他相关依赖

1. openssl下载地址:https://openssl-library.org/source/

image

2. pcre2下载地址:https://github.com/PCRE2Project/pcre2/releases

image

3. zlib下载地址【注意:zlib版本需要1.1.3+】:https://www.zlib.net/

image

4. 解压openssl、pcre2 以及 zlib:

tar -zxvf /opt/sources/openssl-3.2.4.tar.gz -C /opt
tar -zxvf /opt/sources/pcre2-10.42.tar.gz -C /opt
tar -zxvf /opt/sources/zlib-1.3.tar.gz -C /opt

二、正式安装nginx

(一) nginx下载

下载地址:https://nginx.org/en/download.html
image

(二) 解压并编译

将安装包上传至/opt/sources目录中,执行以下命令:

  tar -zxvf /opt/sources/nginx-1.24.0.tar.gz -C /opt
cd /opt/nginx-1.24.0
 
# prefix:指定安装目录,为了方便使用,这里选择/usr/local/nginx
# with-pcre:pcre2的根目录(上述解压的pcre2-5.38.2.tar.gz)
# with-openssl:openssl的根目录(上述解压的openssl-3.2.0.tar.gz)
# with-zlib:zlib的根目录(上述解压的zlib-1.3.tar.gz)
./configure --with-http_ssl_module --prefix=/usr/local/nginx
--with-pcre=/opt/pcre2-10.42 
--with-openssl=/opt/openssl-3.2.0 
--with-zlib=/opt/zlib-1.3
make && make install
# 基本安装步骤
# 1.进入解压的nginx目录
./config

# 2.编译
make

# 3.安装
make install 

# 4.查看安装地址
whereis nginx

# 5.进入安装目录
cd /usr/local/nginx

# 6.进入sbin文件夹,执行nginx命令
cd sbin 
./nginx

(三) 修改配置文件

NGINX_HOME代表 nginx 的安装目录,也就是上述构建 nginx 时指定的 prefix 参数。
默认情况下,配置文件位于NGINX_HOME/conf/nginx.conf。

########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
 
    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.80.101:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 163.28.7.63; #允许的ip           
        } 
    }
}

(四) 启动nginx

# 默认配置文件启动
nginx
# 指定配置文件启动
nginx -c /etc/nginx.conf

(五) 停止nginx

进入目录cd /usr/local/nginx/sbin

# 立即停止
nginx -s stop
# 完成当前工作后停止
nginx -s quit

使用更改后的配置文件重新启动nginx

nginx -s reload
# 指定配置文件
nginx -s reload -c /etc/nginx.conf

检查nginx配置文件是否正确

nginx -t
# 指定配置文件
nginx -t -c /etc/nginx.conf

(六) 详细配置示例

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes  1;
# 事件区块开始
events {
#单个进程最大连接数(最大连接数=连接数*进程数)
#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。
    worker_connections  1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#include:导入外部文件mime.types,将所有types提取为文件,然后导入到nginx配置文件中
    include       mime.types;
#默认文件类型
    default_type  application/octet-stream;
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile        on;
#长连接超时时间,单位是秒
    keepalive_timeout  65;
# 第一个Server区块开始,表示一个独立的虚拟主机站点
    server {
# 提供服务的端口,默认80
        listen       80;
# 提供服务的域名主机名
        server_name  localhost;
#对 "/" 启用反向代理,第一个location区块开始
        location / {
            root   html;  #服务默认启动目录
            index  index.html index.htm; # 默认的首页文件,多个用空格分开
        }
# 错误页面路由
        error_page   500 502 503 504  /50x.html; # 出现对应的http状态码时,使用50x.html回应客户
        location = /50x.html { # location区块开始,访问50x.html
            root   html;  # 指定对应的站点目录为html
        }
    }
}  
  • proxy_read_timeout 300s
  • proxy_send_timeout 300s

三、nginx配置数据库连接

配置stream和http同级

stream {
    upstream mysql {
        server 192.168.0.2:3306;
    }
 
    server {
        listen 3306;
        proxy_pass mysql;
    }
}

测试方法

mysql -h 127.0.0.1 -P 3306 -u root -p
posted @ 2026-03-25 15:35  小朱学编程啊  阅读(2)  评论(0)    收藏  举报