Install nginx on ubuntu
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 replacebionic
with correct version(also called codename).
sudo touch /etc/apt/source.list.d/nginx.list
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
[arch=amd64]
means just available for amd64- download the nginx key from Nginx Key
sudo apt add nginx_signing.key
, then you can delete this filesudo apt update
sudo apt install nginx
sudo service nginx start
- the default config directory is
/etc/nginx
- 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; } }
- 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; #} }
- 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; }
- you can execute
nginx -t
for test config file.
Author Linfeng
LastMod 2023-09-01