virtual host nginx ubuntu 22.04

12

Jun

Create virtual host in nginx ubuntu 22.04

Ready to take your Nginx skills to the next level? In this tutorial, we’ll guide you through the process of setting up virtual hosts on Ubuntu using Nginx. With virtual hosts, you can host multiple websites or applications on a single server, each with its own domain or subdomain.

Creating Your Web Content

Let’s start by creating a directory to store our web content and then create a simple HTML file for our site:

cd /var/www
sudo mkdir tutorial
cd tutorial
sudo nano index.html

Paste the following HTML code into the file:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello, Nginx!</title>
</head>
<body>
    <h1>Hello World, Nginx!</h1>
</body>
</html>

Configuring Nginx Virtual Host

Next, let’s create a configuration file for our virtual host. Navigate to the Nginx sites-enabled directory and create a new configuration file:

cd /etc/nginx/sites-enabled
sudo nano tutorial

Paste the following configuration into the file:

server {
    listen 80;
    listen [::]:80;

    server_name tutorial.localhost;

    root /var/www/tutorial;
    index index.html;

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

Restarting Nginx

Once the configuration is saved, restart Nginx to apply the changes:

sudo service nginx restart

Testing Your Virtual Host

Now, open your web browser and navigate to:

http://tutorial.localhost

You should see your web page with the title “Hello, Nginx!” and a heading “Hello World, Nginx!”.

Conclusion

Congratulations! You’ve successfully set up a virtual host with Nginx on your Ubuntu server. You can now host multiple websites or applications on the same server using different domain names.

If you encounter any issues or have any questions, feel free to ask in the comments below. Happy hosting with Nginx!

Leave a Reply

Your email address will not be published. Required fields are marked *

RELATED

Posts