• 技术文章 >运维 >Nginx

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

    青灯夜游青灯夜游2022-02-17 11:14:42转载202
    Debian系统上如何编译安装Nginx?下面本篇文章带大家详解下Debian系统上编译安装Nginx的方法,希望对大家有所帮助!

    Nginx

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

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

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

    环境准备

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

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

    本次教程使用一台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

    其中:

    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

    具体使用

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

    同时,我们使用systemctl对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(步骤详解)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:掘金社区,如有侵犯,请联系admin@php.cn删除
    专题推荐:Debian Nginx
    上一篇:浅析nginx的缓存和清理(代码分享) 下一篇:nginx报错502怎么办?解决方案分享
    PHP编程就业班

    相关文章推荐

    • laravel在nginx中能配置到已有站点路径吗?• 图文讲解nginx+phpstorm+xdebug环境的配置方法• CentOS8怎么安装最新版Nginx• 记录Nginx怎么配置TP5.1及所遇问题• docker-composer快速构建nginx+php环境• 解析docker怎么搭建lnmp环境(php7.4 + nginx )• 带你搞懂怎么基于Docker安装Nginx搭建静态服务器

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网