天天看點

【Linux】Ubuntu安裝Nginx(線上安裝&源碼編譯安裝)

作者:Java狂人

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 ;;