> For the complete documentation index, see [llms.txt](https://courses.learnwithprayag.com/linux/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://courses.learnwithprayag.com/linux/web-server/lab-5.2-web-server-serve-html-page-using-nginx.md).

# Lab 5.2: Web Server - Serve html page using Nginx

Check if port 80 is already in use on host

In short we are checking if any service is running on port 80

In previous exercise we have apache running on port 80

Check using netstat if port 80 is in use and by which service, using netstat and lsof

```
prayag@devops-vm:~$ netstat -tuln | grep 80
tcp6       0      0 :::80                 :::*                    LISTEN
prayag@devops-vm:~$
prayag@devops-vm:~$ sudo lsof -Pni :80
COMMAND     PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
apache2 2363264     root    4u  IPv6 6043078      0t0  TCP *:80 (LISTEN)
apache2 2363269 www-data    4u  IPv6 6043078      0t0  TCP *:80 (LISTEN)
apache2 2363270 www-data    4u  IPv6 6043078      0t0  TCP *:80 (LISTEN)
prayag@devops-vm:~$
```

Stop apache service&#x20;

```
prayag@devops-vm:~$ sudo systemctl stop apache2
prayag@devops-vm:~$ sudo systemctl disable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable apache2
Removed /etc/systemd/system/multi-user.target.wants/apache2.service.
```

Remove content from /var/www/html as nginx also servers content from /var/www/html on Ubuntu

```
prayag@devops-vm:$sudo cd /var/www/html
prayag@devops-vm:/var/www/html$ sudo rm -fr index.html index.html.orig
```

Again check if port 80 is in use. Blank output indicates nothing is running

```
prayag@devops-vm:~$ sudo lsof -Pni :80
prayag@devops-vm:~$ netstat -tuln | grep 80
prayag@devops-vm:~$
```

We can proceed with the installation of Nginx web server.

Update repo

```
prayag@devops-vm:~$ sudo apt-get update
Ign:1 https://pkg.jenkins.io/debian-stable binary/ InRelease
Hit:2 https://pkg.jenkins.io/debian-stable binary/ Release
Hit:4 http://archive.ubuntu.com/ubuntu focal InRelease
Get:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Fetched 336 kB in 1s (234 kB/s)
Reading package lists... Done
prayag@devops-vm:~$
```

**Install nginx on Ubuntu 20**

```
prayag@devops-vm:~$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libgd3 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc
The following NEW packages will be installed:
  libgd3 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx nginx-common nginx-core
0 upgraded, 8 newly installed, 0 to remove and 60 not upgraded.
Need to get 723 kB of archives.
After this operation, 2,604 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libgd3 amd64 2.2.5-5.2ubuntu2.1 [118 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 nginx-common all 1.18.0-0ubuntu1.4 [37.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libnginx-mod-http-image-filter amd64 1.18.0-0ubuntu1.4 [14.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libnginx-mod-http-xslt-filter amd64 1.18.0-0ubuntu1.4 [13.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libnginx-mod-mail amd64 1.18.0-0ubuntu1.4 [42.9 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libnginx-mod-stream amd64 1.18.0-0ubuntu1.4 [67.4 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 nginx-core amd64 1.18.0-0ubuntu1.4 [425 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 nginx all 1.18.0-0ubuntu1.4 [3,620 B]
Fetched 723 kB in 4s (174 kB/s)
Preconfiguring packages ...

```

Check nginx status using systemctl

```
prayag@devops-vm:~$ sudo systemctl status nginx
● 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 2023-09-08 11:23:33 UTC; 1min 30s ago
       Docs: man:nginx(8)
   Main PID: 2407100 (nginx)
      Tasks: 3 (limit: 9443)
     Memory: 5.1M
     CGroup: /system.slice/nginx.service
             ├─2407100 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─2407101 nginx: worker process
             └─2407102 nginx: worker process

Sep 08 11:23:32 devops-vm.example.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 08 11:23:33 devops-vm.example.com systemd[1]: Started A high performance web server and a reverse proxy server.
prayag@devops-vm:~$

```

Check if its running on port 80. tcp6 means its running on both ipv4 and ipv6

```
prayag@devops-vm:~$ netstat -tuln | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
prayag@devops-vm:~$

```

Test locally if default web page is served using curl.&#x20;

```
prayag@devops-vm:~$ curl 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>
prayag@devops-vm:~$ 
```

You can also check on your hosts IP using curl

```
prayag@devops-vm:~$ curl 192.168.200.46
<!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>
prayag@devops-vm:~$

```

Check on host IP from browser in this case <http://192.168.1.11>

<figure><img src="/files/NDh16LsGuVuAuWGGK7OT" alt=""><figcaption></figcaption></figure>

Next, we will server our own simple index.html page instead of default one.

By default Nginx default page (index.nginx-debian.html) is locate at **/var/www/html**

**We will rename** index.nginx-debian.html as index.nginx-debian.html.orig and create new index.html page with contetns "This is web page on Nginx"

```
prayag@devops-vm:~$ cd /var/www/html/
prayag@devops-vm:/var/www/html$ ls
index.nginx-debian.html

prayag@devops-vm:/var/www/html$ sudo mv index.nginx-debian.html index.nginx-debian.html.orig
prayag@devops-vm:/var/www/html$ sudo vim index.html
prayag@devops-vm:/var/www/html$ cat index.html
This is web page on Nginx
prayag@devops-vm:/var/www/html$ ls
index.html  index.nginx-debian.html.orig

prayag@devops-vm:/var/www/html$ sudo systemctl restart nginx
prayag@devops-vm:/var/www/html$ sudo systemctl status nginx
● 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 2023-09-08 11:46:24 UTC; 1min 56s ago
       Docs: man:nginx(8)
    Process: 2441648 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 2441660 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2441667 (nginx)
      Tasks: 3 (limit: 9443)
     Memory: 3.4M
     CGroup: /system.slice/nginx.service
             ├─2441667 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─2441668 nginx: worker process
             └─2441669 nginx: worker process

Sep 08 11:46:24 devops-vm.example.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 08 11:46:24 devops-vm.example.com systemd[1]: Started A high performance web server and a reverse proxy server.

prayag@devops-vm:/var/www/html$ netstat -tuln | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
prayag@devops-vm:/var/www/html$

prayag@devops-vm:/var/www/html$ curl localhost
This is web page on Nginx
prayag@devops-vm:/var/www/html$ curl 192.168.200.46
This is web page on Nginx
prayag@devops-vm:/var/www/html$

```

Check on browser using host ip, in this case 192.168.1.11

![](/files/mbEbuDCb2dEy3dZR6NvI)
