Ubuntu安装Nginx全过程(在线安装&源码编译安装)

 更新时间:2025年03月05日 11:42:44   作者:fkjavaer  
介绍了在Ubuntu 20.04上安装Nginx的两种方式:apt安装和源码编译安装,apt安装简单,但模块有限;源码编译安装可以自定义模块,更灵活

1.安装环境

  • ubuntu 20.04
  • nginx1.18.0

2.安装方式

2.1 apt安装

sudo apt update
sudo apt install nginx

1)查看版本

nginx -v
# 版本
nginx version: nginx/1.18.0 (Ubuntu)

2)查看安装版本及详情

nginx -V

#版本及安装详情
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-lUTckl/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

可以发现,使用在线安装方式,为我们指定了一些安装参数,

例如:--prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf,并为我们安装了一些module,

例如:--with-http_ssl_module,这就是我们服务器部署常用的https模块。

2.2 源码编译安装

1)删除nginx

由于通过apt方式安装了nginx,因此需要先将其卸载掉。加上--purge删除已安装的软件包,并删除配置文件。

sudo apt --purge remove nginx

2)删除相关依赖

虽然在第一步删除nginx时,会提示使用sudo apt autoremove

注意:使用该命令会出现一些无法预知的错误,切记。

sudo apt --purge remove fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core

2.2.1 下载源码

下载地址:nginx: download

2.2.2 安装

1)解压缩

tar zxvf nginx-1.18.0.tar.gz

2)安装编译相关模块

sudo apt install gcc
sudo apt install make

3)设置配置

cd /home/stone/nginx-1.18.0
sudo ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid

会提示报错需要pcre,zlib模块

4)安装相关依赖

sudo apt install libpcre3-dev
sudo apt install zlib1g-dev

5)编译&安装

sudo make
sudo make install

6)启动

cd /usr/local/nginx
sudo ./nginx

7)查看进程

ps -ef|grep nginx

root       39949       1  0 12:54 ?        00:00:00 nginx: master process ./nginx
nobody     39950   39949  0 12:54 ?        00:00:00 nginx: worker process

8)查看默认安装模块

我们先使用nginx -V查看,发现其只返回了我们配置的参数,并不像apt安装方式时,会返回安装了哪些模块。

cd /usr/local/nginx
./nginx -V

# 输出结果
nginx version: nginx/1.18.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid

这时,我们需要去编译的文件夹去找,可以看到编译安装的方式,安装的模块比apt安装方式时还要多。

cd /home/stone/nginx-1.18.0/auto
cat options | grep "YES"

# 输出结果
HTTP=YES
HTTP_CACHE=YES
HTTP_CHARSET=YES
HTTP_GZIP=YES
HTTP_SSI=YES
HTTP_ACCESS=YES
HTTP_AUTH_BASIC=YES
HTTP_MIRROR=YES
HTTP_USERID=YES
HTTP_AUTOINDEX=YES
HTTP_GEO=YES
HTTP_MAP=YES
HTTP_SPLIT_CLIENTS=YES
HTTP_REFERER=YES
HTTP_REWRITE=YES
HTTP_PROXY=YES
HTTP_FASTCGI=YES
HTTP_UWSGI=YES
HTTP_SCGI=YES
HTTP_GRPC=YES
HTTP_MEMCACHED=YES
HTTP_LIMIT_CONN=YES
HTTP_LIMIT_REQ=YES
HTTP_EMPTY_GIF=YES
HTTP_BROWSER=YES
HTTP_UPSTREAM_HASH=YES
HTTP_UPSTREAM_IP_HASH=YES
HTTP_UPSTREAM_LEAST_CONN=YES
HTTP_UPSTREAM_RANDOM=YES
HTTP_UPSTREAM_KEEPALIVE=YES
HTTP_UPSTREAM_ZONE=YES
MAIL_POP3=YES
MAIL_IMAP=YES
MAIL_SMTP=YES
STREAM_LIMIT_CONN=YES
STREAM_ACCESS=YES
STREAM_GEO=YES
STREAM_MAP=YES
STREAM_SPLIT_CLIENTS=YES
STREAM_RETURN=YES
STREAM_UPSTREAM_HASH=YES
STREAM_UPSTREAM_LEAST_CONN=YES
STREAM_UPSTREAM_RANDOM=YES
STREAM_UPSTREAM_ZONE=YES
        --with-select_module)            EVENT_SELECT=YES           ;;
        --with-poll_module)              EVENT_POLL=YES             ;;
        --with-threads)                  USE_THREADS=YES            ;;
        --with-file-aio)                 NGX_FILE_AIO=YES           ;;
        --with-http_ssl_module)          HTTP_SSL=YES               ;;
        --with-http_v2_module)           HTTP_V2=YES                ;;
        --with-http_realip_module)       HTTP_REALIP=YES            ;;
        --with-http_addition_module)     HTTP_ADDITION=YES          ;;
        --with-http_xslt_module)         HTTP_XSLT=YES              ;;
        --with-http_image_filter_module) HTTP_IMAGE_FILTER=YES      ;;
        --with-http_geoip_module)        HTTP_GEOIP=YES             ;;
        --with-http_sub_module)          HTTP_SUB=YES               ;;
        --with-http_dav_module)          HTTP_DAV=YES               ;;
        --with-http_flv_module)          HTTP_FLV=YES               ;;
        --with-http_mp4_module)          HTTP_MP4=YES               ;;
        --with-http_gunzip_module)       HTTP_GUNZIP=YES            ;;
        --with-http_gzip_static_module)  HTTP_GZIP_STATIC=YES       ;;
        --with-http_auth_request_module) HTTP_AUTH_REQUEST=YES      ;;
        --with-http_random_index_module) HTTP_RANDOM_INDEX=YES      ;;
        --with-http_secure_link_module)  HTTP_SECURE_LINK=YES       ;;
        --with-http_degradation_module)  HTTP_DEGRADATION=YES       ;;
        --with-http_slice_module)        HTTP_SLICE=YES             ;;
        --with-http_perl_module)         HTTP_PERL=YES              ;;
        --with-http_stub_status_module)  HTTP_STUB_STATUS=YES       ;;
        --with-mail)                     MAIL=YES                   ;;
        --with-mail_ssl_module)          MAIL_SSL=YES               ;;
            MAIL=YES
            MAIL_SSL=YES
        --with-stream)                   STREAM=YES                 ;;
        --with-stream_ssl_module)        STREAM_SSL=YES             ;;
        --with-stream_realip_module)     STREAM_REALIP=YES          ;;
        --with-stream_geoip_module)      STREAM_GEOIP=YES           ;;
                                         STREAM_SSL_PREREAD=YES     ;;
        --with-google_perftools_module)  NGX_GOOGLE_PERFTOOLS=YES   ;;
        --with-cpp_test_module)          NGX_CPP_TEST=YES           ;;
        --with-compat)                   NGX_COMPAT=YES             ;;
        --with-debug)                    NGX_DEBUG=YES              ;;
        --with-pcre)                     USE_PCRE=YES               ;;
        --with-pcre-jit)                 PCRE_JIT=YES               ;;
        --with-libatomic)                NGX_LIBATOMIC=YES          ;;
        --test-build-devpoll)            NGX_TEST_BUILD_DEVPOLL=YES ;;
        --test-build-eventport)          NGX_TEST_BUILD_EVENTPORT=YES ;;
        --test-build-epoll)              NGX_TEST_BUILD_EPOLL=YES   ;;
        --test-build-solaris-sendfilev)  NGX_TEST_BUILD_SOLARIS_SENDFILEV=YES ;;

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • nginx学习总结五(nginx反向代理)

    nginx学习总结五(nginx反向代理)

    Nginx代理与负载均衡配置与优化技巧,方便需要的朋友
    2012-11-11
  • Nginx 安装配置及常用命令详解

    Nginx 安装配置及常用命令详解

    本文介绍了Nginx的安装、配置和启动等步骤,详细介绍了Nginx的配置文件结构和常用命令,感兴趣的朋友跟随小编一起看看吧
    2026-04-04
  • Nginx中autoindex的具体使用

    Nginx中autoindex的具体使用

    autoindex是Nginx配置的一个指令,它可以控制Nginx是否允许在浏览器中显示一个目录的内容,本文主要介绍了Nginx中autoindex的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-06-06
  • nginx 平滑重启的实现方法

    nginx 平滑重启的实现方法

    这篇文章主要介绍了nginx 平滑重启的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Nginx服务器中配置GeoIP模块来拦截指定国家IP

    Nginx服务器中配置GeoIP模块来拦截指定国家IP

    Nginx中自带GeoIP模块可以屏蔽指定IP的请求,只不过默认没有被编译,打开以后我们只要再下载一个IP规则就行,Nginx服务器中配置GeoIP模块来拦截指定国家IP
    2016-06-06
  • Nginx 处理超长 Cookie 导致的 400 错误

    Nginx 处理超长 Cookie 导致的 400 错误

    本文主要介绍了Nginx 处理超长 Cookie 导致的 400 错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-04-04
  • Nginx 禁止IP访问 只允许域名访问

    Nginx 禁止IP访问 只允许域名访问

    今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案
    2016-09-09
  • nginx中配置https的详细步骤及配置SSL/TLS证书的完整流程

    nginx中配置https的详细步骤及配置SSL/TLS证书的完整流程

    本文将详细介绍如何在 Nginx 服务器上配置 SSL/TLS 证书,包括获取证书、配置 Nginx、强化安全性等完整流程,无论您是使用 Let’s Encrypt 的免费证书还是自签名证书,都能在本指南中找到对应的配置方法,感兴趣的朋友跟随小编一起看看吧
    2025-10-10
  • 教你nginx跳转配置的四种方式

    教你nginx跳转配置的四种方式

    现如今随着应用服务的增多,服务可能部署在不同的服务器上,下面这篇文章主要给大家介绍了关于nginx跳转配置的四种方式,需要的朋友可以参考下
    2022-07-07
  • Nginx配置详解(推荐)

    Nginx配置详解(推荐)

    Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能
    2017-05-05

最新评论