Reference document:

Note:

  • The ubuntu version is 18.04 lts, if you don’t sure your linux version, you can view the version with lsb_release -a. Then you can replace bionic with correct version(also called codename).
  1. sudo touch /etc/apt/source.list.d/nginx.list
  2. sudo vi /etc/apt/source.list.d/nginx.list
    1
    2
    
    deb [arch=amd64] http://nginx.org/packages/ubuntu/ bionic nginx
    deb-src http://nginx.org/packages/ubuntu/ bionic nginx
    
  3. [arch=amd64] means just available for amd64
  4. download the nginx key from Nginx Key
  5. sudo apt add nginx_signing.key, then you can delete this file
  6. sudo apt update
  7. sudo apt install nginx
  8. sudo service nginx start
  9. the default config directory is /etc/nginx
  10. example for http config file:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    server {
        listen       80;
        server_name  example.com;
    
        #charset koi8-r;
        access_log  /var/log/nginx/host.access.log  main;
        error_log  /var/log/nginx/host.error.log  warn;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    
  11. example for https config file:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    
    server {
        listen       443 ssl http2;
        server_name  example.com;
    
        charset utf-8;
        access_log  /var/log/nginx/host.access.log  main;
        error_log  /var/log/nginx/host.error.log  warn;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        ssl_certificate ~/path/fullchain.pem;
        ssl_certificate_key ~/path/privkey.pem;
        ssl_trusted_certificate ~/path/chain.pem;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
  12. example for redirect config file:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    server {
        listen       80;
        server_name  example.com;
    
        access_log  /var/log/nginx/host.access.log  main;
        error_log  /var/log/nginx/host.error.log  warn;
    
        return 301 https://example.com$request_uri;
    }
    
  13. you can execute nginx -t for test config file.