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编译器:

  1. apt install -y build-essential

2.png

安装正则库

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

  1. apt install -y libpcre3 libpcre3-dev

3.png

安装zlib库

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

  1. apt install -y zlib1g-dev

4.png

安装OpenSSL库

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

  1. apt install -y openssl libssl-dev

5.png

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

下载Nginx源码

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

下载最新的stable稳定版本:

6.png

在Debian上使用wget下载:

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

7.png

配置和编译

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

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

  1. ./configure \
  2. –prefix=/usr/local/nginx \
  3. –user=www \
  4. –group=www \
  5. –sbin-path=/usr/local/nginx/sbin/nginx \
  6. –conf-path=/usr/local/nginx/nginx.conf \
  7. –error-log-path=/var/log/nginx/error.log \
  8. –http-log-path=/var/log/nginx/access.log \
  9. –pid-path=/var/run/nginx.pid \
  10. –lock-path=/var/run/nginx.lock \
  11. –http-client-body-temp-path=/var/cache/nginx/client_temp \
  12. –http-proxy-temp-path=/var/cache/nginx/proxy_temp \
  13. –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
  14. –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
  15. –http-scgi-temp-path=/var/cache/nginx/scgi_temp \
  16. –with-file-aio \
  17. –with-threads \
  18. –with-http_addition_module \
  19. –with-http_auth_request_module \
  20. –with-http_dav_module \
  21. –with-http_flv_module \
  22. –with-http_gunzip_module \
  23. –with-http_gzip_static_module \
  24. –with-http_mp4_module \
  25. –with-http_random_index_module \
  26. –with-http_realip_module \
  27. –with-http_secure_link_module \
  28. –with-http_slice_module \
  29. –with-http_ssl_module \
  30. –with-http_stub_status_module \
  31. –with-http_sub_module \
  32. –with-http_v2_module \
  33. –with-mail \
  34. –with-mail_ssl_module \
  35. –with-stream \
  36. –with-stream_realip_module \
  37. –with-stream_ssl_module \
  38. –with-stream_ssl_preread_module

其中:

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

8.png

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

  1. Configuration summary
  2. + using threads
  3. + using system PCRE library
  4. + using system OpenSSL library
  5. + using system zlib library
  6. nginx path prefix: “/usr/local/nginx”
  7. nginx binary file: “/usr/local/nginx/sbin/nginx”
  8. nginx modules path: “/usr/local/nginx/modules”
  9. nginx configuration prefix: “/usr/local/nginx”
  10. nginx configuration file: “/usr/local/nginx/nginx.conf”
  11. nginx pid file: “/var/run/nginx.pid”
  12. nginx error log file: “/var/log/nginx/error.log”
  13. nginx http access log file: “/var/log/nginx/access.log”
  14. nginx http client request body temporary files: “/var/cache/nginx/client_temp”
  15. nginx http proxy temporary files: “/var/cache/nginx/proxy_temp”
  16. nginx http fastcgi temporary files: “/var/cache/nginx/fastcgi_temp”
  17. nginx http uwsgi temporary files: “/var/cache/nginx/uwsgi_temp”
  18. nginx http scgi temporary files: “/var/cache/nginx/scgi_temp”

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

  1. make

9.png

接下来就是安装了。

安装

首先是安装,很简单:

  1. make install

10.png

11.png

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

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

12.png

  1. [Unit]
  2. Description=nginx
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/local/nginx/sbin/nginx
  7. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  8. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  9. PrivateTmp=true
  10. [Install]
  11. 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

  1. cd /
  2. mkdir /www
  3. cd www
  4. vim index.html

16.png

重载Nginx配置:

  1. systemctl reload nginx

浏览器访问成功:

17.png

卸载

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

  1. # 停止Nginx服务
  2. systemctl stop nginx
  3. # 删除Nginx服务
  4. rm -rf /usr/lib/systemd/system/nginx.service
  5. # 重载配置
  6. systemctl daemon-reload
  7. # 删除Nginx编译文件
  8. rm -rf nginx

这样就卸载完成了。

END

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

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

推荐教程:nginx教程

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