Hosting a Website on NGINX in a Virtual Machine

This guide will help you configure NGINX to host your website on a VM and access it from your browser using custom domain names.


1. Create NGINX Configuration

Create a configuration file for your site in the sites-available directory:

sudo nano /etc/nginx/sites-available/mywebsite.conf

Paste your NGINX configuration:

server {
    listen 80;
    server_name ghost2.mywebsite.com www.ghost2.mywebsite.com mywebsite.com www.mywebsite.com;

    # Redirect all HTTP requests to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name ghost2.mywebsite.com www.ghost2.mywebsite.com;

    ssl_certificate /etc/ssl/certs/selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/selfsigned.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

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

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

Note: Replace /var/www/ghost2 with your website’s root directory and ensure your SSL certificate paths are correct.


2. Enable the Site

Create a symbolic link to sites-enabled so NGINX loads this configuration:

sudo ln -s /etc/nginx/sites-available/mywebsite.conf /etc/nginx/sites-enabled/


3. Test NGINX Configuration

Check that the configuration syntax is correct:

sudo nginx -t

If it outputs:

syntax is ok
test is successful

you can proceed.