SoFunction
Updated on 2025-04-11

Ubuntu installs Nginx full process (online installation & source code compilation and installation)

1. Installation environment

  • ubuntu 20.04
  • nginx1.18.0

2. Installation method

2.1 apt installation

sudo apt update
sudo apt install nginx

1) View version

nginx -v
# Versionnginx version: nginx/1.18.0 (Ubuntu)

2) View the installation version and details

nginx -V

# Version and installation detailsnginx 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/ --http-log-path=/var/log/nginx/ --error-log-path=/var/log/nginx/ --lock-path=/var/lock/ --pid-path=/run/ --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

It can be found that using the online installation method, some installation parameters were specified for us.

For example:--prefix=/usr/share/nginx --conf-path=/etc/nginx/, and installed some modules for us,

For example:--with-http_ssl_module, this is the commonly used https module for our server deployment.

2.2 Source code compilation and installation

1) Delete nginx

Since nginx is installed through apt, it needs to be uninstalled first. Plus--purgeDelete installed packages and delete configuration files.

sudo apt --purge remove nginx

2) Delete related dependencies

Although in the first step, it will be prompted to usesudo apt autoremove

Note: Using this command will cause some unpredictable errors, remember.

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 Download the source code

Download address:nginx: download

2.2.2 Installation

1) Decompression

tar zxvf nginx-1.18.

2) Install and compile relevant modules

sudo apt install gcc
sudo apt install make

3) Setting up configuration

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

It will prompt an errorpcre,zlibModule

4) Installation-related dependencies

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

5) Compile & Install

sudo make
sudo make install

6) Start

cd /usr/local/nginx
sudo ./nginx

7) View the process

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) Check the default installation module

Let's use it firstnginx -VAfter checking, I found that it only returns the parameters we configured, and it does not return which modules were installed when installing apt.

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

# Output resultnginx 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/ --pid-path=/usr/local/nginx/

At this time, we need to search for the compiled folder, and we can see the compiled and installed method, and there are more modules installed than when installing apt.

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

# Output resultHTTP=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 ;;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.