> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo First Website with Nginx

> Step by step guide to set up a minimal Hello World website served by NGINX, including virtual host configuration, site files, enabling the site, testing and reloading.

This guide walks through creating a minimal "Hello World" site and serving it with NGINX on a Linux host. You'll:

* Verify NGINX is installed and running
* Create a new site configuration in `/etc/nginx/sites-available`
* Create site content under `/var/www/helloworld`
* Enable the site with a symlink in `/etc/nginx/sites-enabled`
* Test the site locally by sending a `Host` header with `curl`

Important commands and file snippets are provided in the sequence you should run them.

## 1. Verify NGINX is installed and running

Check the service status and start NGINX if it is not running:

```bash theme={null}
# Check service status
sudo systemctl status nginx

# If it's not running, start it
sudo systemctl start nginx
```

Confirm NGINX responds on localhost:

```bash theme={null}
curl localhost
```

You should see the default NGINX welcome page, for example:

```html theme={null}
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
<h1>Welcome to nginx!</h1>
...
</html>
```

## 2. Become root (optional)

You can continue using `sudo` for each command, or switch to root temporarily:

```bash theme={null}
sudo su
# prompt changes to root@host:#
```

## 3. Inspect the NGINX configuration directory

List the NGINX config files and the `sites-available` directory:

```bash theme={null}
cd /etc/nginx
ls -la
ls -la sites-available
```

## 4. Create a new virtual host configuration

Copy the default site configuration to create a new site called `helloworld`:

```bash theme={null}
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/helloworld
```

Edit `/etc/nginx/sites-available/helloworld` and simplify its server block. Remove comments and any IPv6 `listen` line if present. Ensure the server block looks like this:

```nginx theme={null}
server {
    listen 80;

    root /var/www/helloworld;

    index index.html index.htm index.nginx-debian.html;

    server_name helloworld.com;

    location / {
        try_files $uri $uri/ =404;
    }
}
```

Notes:

* `server_name` must match the `Host` header that clients send (we use `helloworld.com` in this example).
* `root` is where NGINX will look for the site files.

Avoid having two server blocks configured with `listen 80 default_server` simultaneously. That causes a duplicate default server error. Keep only one `default_server` or remove the directive from additional site configs.

## 5. Create the site directory and index page

Create the site folder and a simple `index.html`:

```bash theme={null}
mkdir -p /var/www/helloworld
cat > /var/www/helloworld/index.html <<'HTML'
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Hello World</title>
</head>
<body>
  <h1>Hello World!</h1>
</body>
</html>
HTML
```

## 6. Enable the site

Enable the site by creating a symbolic link from `sites-available` to `sites-enabled`:

```bash theme={null}
ln -s /etc/nginx/sites-available/helloworld /etc/nginx/sites-enabled/helloworld
```

## 7. Test NGINX configuration for syntax errors

Run the NGINX config test:

```bash theme={null}
nginx -t
```

If you left `default_server` in more than one server block, you might see an error like:

```bash theme={null}
# nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/helloworld:3
# nginx: configuration file /etc/nginx/nginx.conf test failed
```

Remove `default_server` from extra server blocks and re-run `nginx -t`. A successful test shows:

```bash theme={null}
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
```

## 8. Reload NGINX to apply the new configuration

Reload the running service to apply changes:

```bash theme={null}
# Either with nginx directly
nginx -s reload

# Or via systemd
systemctl reload nginx
```

## 9. Exit root (if applicable)

If you switched to root earlier, return to your normal user:

```bash theme={null}
exit
```

## 10. Test the site locally with curl

Because `helloworld.com` is not necessarily configured in your DNS, test by sending a `Host` header:

```bash theme={null}
# This returns our Hello World page
curl --header "Host: helloworld.com" localhost
# Output:
# <h1>Hello World!</h1>
```

If you pass a `Host` header that doesn't match any `server_name`, NGINX will serve the first enabled configuration (typically the default site):

```bash theme={null}
curl --header "Host: someunknownhost" localhost
# This returns the default "Welcome to nginx!" page
```

When multiple server blocks exist, NGINX tries to select the best match for the `Host` header. If none match, it serves the first enabled configuration (often the default). To avoid accidentally serving the wrong site, remove the default config, or ensure each `server_name` is explicit and unique.

## Quick reference: key files and commands

| Item | Path / Command | Purpose |
| - | - | - |
| Site config | `/etc/nginx/sites-available/helloworld` | Virtual host definition (server block) |
| Enabled site | `/etc/nginx/sites-enabled/helloworld` | Symlink to enable the site |
| Site content root | `/var/www/helloworld` | HTML files served by NGINX |
| Test config | `nginx -t` | Validate NGINX config syntax |
| Reload NGINX | `systemctl reload nginx` | Apply configuration changes |

## Troubleshooting tips

* If `curl localhost` returns the default welcome page after enabling your site, ensure your `Host` header matches the `server_name`, e.g. `curl --header "Host: helloworld.com" localhost`.
* Permissions: NGINX needs read access to files in `/var/www/helloworld`. Adjust ownership or permissions if files are inaccessible.
* Ports/firewall: To expose the site externally, open port 80 in your host firewall or cloud security group.

## Links and references

* NGINX official docs: [https://nginx.org/en/docs/](https://nginx.org/en/docs/)
* Systemd service management: [https://www.freedesktop.org/wiki/Software/systemd/](https://www.freedesktop.org/wiki/Software/systemd/)
* curl manual: [https://curl.se/docs/manpage.html](https://curl.se/docs/manpage.html)

That's it — you now have a minimal NGINX virtual host serving a Hello World page from `/var/www/helloworld`. To make the site available to external users, ensure your DNS points `helloworld.com` to the server and that port 80 is allowed through any firewalls.

> **[Watch Video](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/0de43784-b08d-4ce0-8470-a7541b78fe58/lesson/205db332-da0d-4f3d-8273-225f9566c386)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.