您的位置:首页 > 技术中心 > 运维 >

一文教你怎么在Debian上编译安装Nginx(步骤详解)

时间:2022-09-07 11:13

Debian系统上如何编译安装Nginx?下面本篇文章带大家详解下Debian系统上编译安装Nginx的方法,希望对大家有所帮助!

Nginx

Nginx是一款轻量级的 HTTP 服务器,时常用于服务端的反向代理和负载均衡。

手动编译安装Nginx比较复杂,但是平时一般使用最多。原因:

  • 便于管理 编译安装的Nginx,其安装地址可控,如果需要卸载,执行反编译即可。
  • 模块可控 Nginx有其丰富的模块库,如:ngx-fancyindex。使用Docker或软件包管理器安装的Nginx,模块有时不方便载入。

下次给大家分享,怎么安装模块~~~

环境准备

本次安装Nginx,是在Debian发行版本的Linux上安装,如果是CentOS发行版本Linux,需要注意:

  • 编译安装时,需要自行安装:gccpcrezlib以及openssl

另外,如果你觉得本文的安装方法过于技术型。其实,也可以试试宝塔面板的一键操作。

本次教程使用一台Debian10 x64服务器:

1.png

安装gcc编译器

首先,我们需要安装gcc编译器用于make编译,Debian可以通过安装build-essential来安装GCC编译器:

apt install -y build-essential

2.png

安装正则库

正则库很关键,我们使用Nginx,在配置文件内location进行目录匹配,就需要正则库。Debian安装正则库,可以:

apt install -y libpcre3 libpcre3-dev

3.png

安装zlib库

当然,Nginx编译过程和Http相应过程还需要gzip格式的压缩,所以我们还需要安装zlib库用于对HTTP包的内容做gzip格式的压缩,可以这样安装:

apt install -y zlib1g-dev

4.png

安装OpenSSL库

最后,现在SSL协议很重要,Chrome等主流浏览器,都开始默认相应HTTPS了,所以OpenSSL编译环境也很重要:

apt install -y openssl libssl-dev

5.png

依赖都安装完成,就可以下载源码来编译了。

下载Nginx源码

接下来,我们下载Nginx源码,我们进入Nginx官网:http://nginx.org/en/download.html

下载最新的stable稳定版本:

6.png

在Debian上使用wget下载:

# 下载源码
wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 解压源码
tar -xf nginx-1.20.2.tar.gz
# 进入源代码内
cd cd nginx-1.20.2

7.png

配置和编译

接下来就是make环节了,编译时候的参数可以参考官方Nginx文档:http://nginx.org/en/docs/configure.html

我自己编译Nginx时候,选择的参数一般是:

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module

其中:

  • --prefix:Nginx主要安装路径,后续Nginx子目录依照这个变量展开
  • --user:设置Nginx进程启动时,所属的用户
  • --group:设置Nginx进程启动时,所属的用户组

8.png

如果没有问题,会提示信息:

Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx"
  nginx configuration file: "/usr/local/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

没有报错信息就可以编译了:

make

9.png

接下来就是安装了。

安装

首先是安装,很简单:

make install

10.png

11.png

我们再创建systemctl守护,管理Nginx:

vim /usr/lib/systemd/system/nginx.service

12.png

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

13.png

具体使用

如果你是按我的方法编译,那么,需要注意。

  • /usr/local/nginx:为Nginx编译安装的地址。
  • /usr/local/nginx/nginx.conf:Nginx默认配置文件。

同时,我们使用systemctl对Nginx进行管理:

  • systemctl start nginx:启动Nginx服务。
  • systemctl reload nginx:Nginx配置重载。
  • systemctl stop nginx:停止Nginx服务。

更多systemctl操作,可以看这篇教程:《Linux系统服务神器:systemctl的配置与使用》

https://juejin.cn/post/7059029634922315812

最后,我们写个HelloWorld

编辑配置文件:

14.png

指向目录/www

15.png

cd /
mkdir /www
cd www
vim index.html

16.png

重载Nginx配置:

systemctl reload nginx

浏览器访问成功:

17.png

卸载

最后,如何卸载Nginx呢?其实更简单:

# 停止Nginx服务
systemctl stop nginx
# 删除Nginx服务
rm -rf /usr/lib/systemd/system/nginx.service
# 重载配置
systemctl daemon-reload
# 删除Nginx编译文件
rm -rf nginx

这样就卸载完成了。

END

其实呢?个人是喜欢编译安装Nginx。

Nginx确实是个Web服务器神器呢~~~

推荐教程:nginx教程

以上就是一文教你怎么在Debian上编译安装Nginx(步骤详解)的详细内容,更多请关注gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游