0
0
Nginxdevops~5 mins

Nginx installation - Commands & Configuration

Choose your learning style9 modes available
Introduction
Nginx is a web server that helps deliver websites and applications quickly. Installing Nginx sets up the software so your computer can serve web pages to users.
When you want to host a simple website on your own server or computer.
When you need a reverse proxy to forward requests to other servers.
When you want to serve static files like images and HTML pages efficiently.
When you want to load balance traffic between multiple backend servers.
When you want to improve website speed and handle many users at once.
Commands
This command updates the list of available software and versions on your system. It ensures you install the latest Nginx version.
Terminal
sudo apt update
Expected OutputExpected
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Fetched 215 kB in 1s (234 kB/s) Reading package lists... Done
This command installs Nginx on your system. The -y flag automatically confirms the installation so it proceeds without asking you.
Terminal
sudo apt install nginx -y
Expected OutputExpected
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: nginx-common nginx-core Suggested packages: fcgiwrap nginx-doc The following NEW packages will be installed: nginx nginx-common nginx-core 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 1,234 kB of archives. After this operation, 5,678 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 nginx-common all 1.18.0-0ubuntu1 [1,000 kB] Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 nginx-core amd64 1.18.0-0ubuntu1 [150 kB] Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 nginx amd64 1.18.0-0ubuntu1 [84.0 kB] Fetched 1,234 kB in 2s (617 kB/s) Selecting previously unselected package nginx-common. (Reading database ... 123456 files and directories currently installed.) Preparing to unpack .../nginx-common_1.18.0-0ubuntu1_all.deb ... Unpacking nginx-common (1.18.0-0ubuntu1) ... Selecting previously unselected package nginx-core. Preparing to unpack .../nginx-core_1.18.0-0ubuntu1_amd64.deb ... Unpacking nginx-core (1.18.0-0ubuntu1) ... Selecting previously unselected package nginx. Preparing to unpack .../nginx_1.18.0-0ubuntu1_amd64.deb ... Unpacking nginx (1.18.0-0ubuntu1) ... Setting up nginx-common (1.18.0-0ubuntu1) ... Setting up nginx-core (1.18.0-0ubuntu1) ... Setting up nginx (1.18.0-0ubuntu1) ... Processing triggers for systemd (245.4-4ubuntu3.13) ... Processing triggers for man-db (2.9.1-1) ...
-y - Automatically confirm installation prompts
This command starts the Nginx service so it begins running and can serve web pages.
Terminal
sudo systemctl start nginx
Expected OutputExpected
No output (command runs silently)
This command checks if Nginx is running properly and shows its current status.
Terminal
sudo systemctl status nginx
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-14 10:00:00 UTC; 10s ago Docs: man:nginx(8) Main PID: 1234 (nginx) Tasks: 3 (limit: 1137) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─1235 nginx: worker process └─1236 nginx: worker process Jun 14 10:00:00 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
This command sends a simple request to the local Nginx server to check if it responds with the default web page headers.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx/1.18.0 (Ubuntu) Date: Fri, 14 Jun 2024 10:00:10 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 12 May 2020 15:00:00 GMT Connection: keep-alive ETag: "5eb3-5c1a5a3f4f400" Accept-Ranges: bytes
Key Concept

If you remember nothing else from this pattern, remember: installing Nginx requires updating your system, installing the package, starting the service, and verifying it runs.

Common Mistakes
Skipping 'sudo apt update' before installing Nginx
Your system might install an outdated version or fail to find the package.
Always run 'sudo apt update' first to refresh package lists.
Not starting the Nginx service after installation
Nginx will not run, so your web server won't respond to requests.
Run 'sudo systemctl start nginx' to launch the service.
Not checking the status of Nginx after starting it
You might miss errors or failures that prevent Nginx from running.
Use 'sudo systemctl status nginx' to confirm it is active and running.
Summary
Update your system package list with 'sudo apt update' before installing software.
Install Nginx using 'sudo apt install nginx -y' to get the latest stable version.
Start the Nginx service with 'sudo systemctl start nginx' so it can serve web pages.
Check Nginx status using 'sudo systemctl status nginx' to ensure it is running.
Test the server response locally with 'curl -I http://localhost' to confirm installation.