Skip to content

Install nginx#

The nginx web server is incredibly popular. It's used by tens of thousands of businesses and millions of end users on a daily basis. Let's install it: sudo apt install nginx

 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
32
33
34
35
36
37
superman@develop:~$ sudo apt install nginx
[sudo] password for superman:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfwupdplugin1
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 879 kB of archives.
After this operation, 3,117 kB of additional disk space will be used.
Get:1 http://nginx.org/packages/ubuntu focal/nginx amd64 nginx amd64 1.20.2-1~focal [879 kB]
Fetched 879 kB in 3s (281 kB/s)
Selecting previously unselected package nginx.
(Reading database ... 108487 files and directories currently installed.)
Preparing to unpack .../nginx_1.20.2-1~focal_amd64.deb ...
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* https://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* https://nginx.com/products/

----------------------------------------------------------------------
Unpacking nginx (1.20.2-1~focal) ...
Setting up nginx (1.20.2-1~focal) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.15) ...

Note

If you remembered from the previous section, we talked about managing software and one thing we did was add the nginx Apt repository to our server. If you removed this, then the above installation of nginx will still work, you just might have a different version. That's OK, because what version you're running is basically irrelevant at this point in time.

My command installed nginx-1.20.2-1~focal, or version 1.20.2-1. Let's see if the nginx service is running: curl -sI localhost/

1
2
superman@develop:~$ curl http://localhost/
curl: (7) Failed to connect to localhost port 80: Connection refused

Connection refused. Because we know that localhost talks to our local system on IP address 127.0.0.1, and http:// means connect via HTTP or TCP port 80, our curl command will attempt to connect to the local system's web server, which we just installed: nginx.

But it's not running. Let's fix that: sudo systemctl start nginx and also run our curl command again:

 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
superman@develop:~$ sudo systemctl start nginx
superman@develop:~$ curl http://localhost/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

A result! That's real HTML that nginx is returning to us. So now the nginx process is running and is listening for TCP connections on port 80. You just installed some software and started a service - well done.

The command line tool we used to manage this service was systemctl, which is part of systemd.

Note

Some Linux distributions, though not many, use an older "init" system called initd, SysVinit, and others. An "init" system is used to "initualise" a Linux system when you power up the machine. You might see initd still around, but we're covering systemd because it's not become the default among the major distributions.