Linux - Nginx离线安装(安装包的方式安装Nginx)

最近在处理一批服务器,服务器是纯内网访问,不能联网的。 所以只能走安装包的方式安装。不得不说 Nginx 是我安装这么多个服务以来最麻烦的,真挺奇怪的,这么大一个产品,居然都没有一键安装包。话不多说,开搞:

1,下载安装包

Nginx要下载的包贼多:

  • Nginx本身
  • PCRE2(用于正则表达式的库,Nginx 需要它来处理正则表达式相关的功能)
  • Zlib (一个通用的数据压缩库,Nginx 使用它来处理压缩功能)
  • OpenSSL (一个强大的加密库,Nginx 使用它来处理 SSL/TLS 加密功能)

Nginx

选择稳定版就好:
nginx: downloadhttps://nginx.org/en/download.html

PCRE2

下最新的就行:

https://github.com/PCRE2Project/pcre2/releaseshttps://github.com/PCRE2Project/pcre2/releases

Zlib

官方有提供了一个直接下载最新包的地址:

https://zlib.net/current/zlib.tar.gzhttps://zlib.net/current/zlib.tar.gz

OpenSSL

也是随便选个就行了:
https://sourceforge.net/projects/openssl-project.mirror/files/https://sourceforge.net/projects/openssl-project.mirror/files/

然后把这些文件都上传到服务器上,建议跟我一样,上传到 /opt/nginx目录下

2,安装依赖库

解压依赖包

cd /opt/nginx
tar -zxvf pcre2-10.44.tar.gz
tar -zxvf zlib-1.3.1.tar.gz
tar -zxvf openssl-3.4.2.tar.gz

编译安装 PCRE

cd /opt/nginx/pcre2-10.44
./configure
make
sudo make install

编译安装 Zlib

cd /opt/nginx/zlib-1.3.1
./configure
make
sudo make install

编译安装 OpenSSL

cd /opt/nginx/openssl-3.4.2
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
make
sudo make install

这个OpenSSL要安装很久,得等等。

3,编译安装 Nginx

解压 Nginx 源码包

cd /opt/nginx
tar -zxvf nginx-1.26.3.tar.gz
cd nginx-1.26.3

配置编译参数

./configure \
  --prefix=/usr/local/nginx \
  --with-pcre=/opt/nginx/pcre2-10.44 \
  --with-zlib=/opt/nginx/zlib-1.3.1 \
  --with-openssl=/opt/nginx/openssl-3.4.2 \
  --with-http_ssl_module

编译并安装

make
sudo make install

4,配置 Nginx 服务

建立连接

ln -s  /usr/local/nginx/sbin/nginx /usr/bin

配置文件在:/usr/local/nginx/conf

配置 Systemd 服务

创建服务文件:

sudo vi /etc/systemd/system/nginx.service

添加以下内容:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

启用并启动服务:

sudo systemctl enable nginx
sudo systemctl start nginx

5,测试nginx是否启动

curl http://127.0.0.1

显示welcome to nginx 就是OK了。

6,异常情况

1. 403问题

可以检查一下nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf

把user 改成root。(配置文件第一行)

但是这个有风险,慎改。

然后刷新一下配置文件:

nginx -s reload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值