当前位置:首页>Linux>Linux离线安装Nginx

Linux离线安装Nginx

  • 2026-09-08 16:59:31
Linux离线安装Nginx

1.卸载yum安装的Nginx

sudo systemctl stop nginx
sudo systemctl disable nginx
sudo yum remove nginx
sudo rm -rf /etc/nginx/ /var/log/nginx/ /usr/share/nginx/
sudo yum autoremove

2.离线安装nginx至指定目录/usr/local/nginx

2.1下载依赖包

https://nginx.org/en/download.html
nginx-1.28.2.tar.gz

https://www.openssl.org/source/openssl-3.6.1.tar.gz
openssl-3.6.1.tar.gz(如果需要https)

https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
pcre-8.45.tar.gz

https://zlib.net/fossils/zlib-1.3.2.tar.gz
zlib-1.3.2.tar.gz

2.2创建临时目录,并将依赖包上传至此

mkdir -p /tmp/nginx-offline-install

2.3解压所有压缩包

tar -zxvf nginx-1.28.2.tar.gz
tar -zxvf pcre-8.45.tar.gz 
tar -zxvf zlib-1.3.2.tar.gz 
tar -zxvf openssl-3.6.1.tar.gz

2.4编译

cd pcre-8.45
./configure --prefix=/usr/local/pcre
make && make install


同理编译zlib
cd ../zlib-1.3.2
./configure --prefix=/usr/local/zlib
make && sudo make install

如需https,同理编译openssl
cd ../openssl-3.6.1
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
make && sudo make install


编译nginx(如果不需要https,去掉倒数第四行)
./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/usr/local/nginx/conf/nginx.conf \
  --error-log-path=/usr/local/nginx/logs/error.log \
  --http-log-path=/usr/local/nginx/logs/access.log \
  --pid-path=/usr/local/nginx/logs/nginx.pid \
  --lock-path=/usr/local/nginx/logs/nginx.lock \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_stub_status_module \
  --with-pcre=/tmp/nginx-offline-install/pcre-8.45 \
  --with-zlib=/tmp/nginx-offline-install/zlib-1.3.2 \
  --with-openssl=/tmp/nginx-offline-install/openssl-3.6.1 \
  --with-threads \
  --user=nginx \
  --group=nginx

3.服务启停

创建 systemd 服务文件:
vim /etc/systemd/system/nginx.service

粘贴下内容:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.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=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3.1首次启动服务:
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx

nginx启停:
systemctl start nginx
systemctl stop nginx

4.最终结构:

安装根目录:/usr/local/nginx
配置文件:/usr/local/nginx/conf/nginx.conf
可执行文件:/usr/local/nginx/sbin/nginx
日志目录:/usr/local/nginx/logs/
Web根目录:/usr/local/nginx/html/
运行用户:nginx

最新文章

随机文章