Host a Static Website with Nginx on Ubuntu: A Step-by-Step Guide

Hosting a static website on your own server is an efficient and cost-effective way to get your content online. Nginx, a powerful and lightweight web server, is an excellent choice for serving static websites. When paired with Ubuntu, a popular Linux distribution, setting up your website becomes straightforward. In this guide, we’ll walk you through the steps to host a static website using Nginx on Ubuntu.

Why Choose Nginx and Ubuntu for Hosting?
Nginx is renowned for its high performance, scalability, and low resource usage, making it perfect for static websites. Combined with Ubuntu’s user-friendliness and widespread support, you have a reliable platform for your hosting needs.

Steps to Host a Static Website with Nginx on Ubuntu
To host a static website with Nginx on Ubuntu, start by installing Nginx. Update your system and install Nginx using the following commands:

sudo apt update
sudo apt install nginx

Verify that the installation was successful by checking its status:

sudo systemctl status nginx

Next, prepare your static website files. Create a directory to store these files:

sudo mkdir -p /var/www/static-site

Copy your website files (HTML, CSS, JavaScript) into this directory:

sudo cp -r /path/to/your/website/* /var/www/static-site/

Ensure proper permissions are set for the website directory:

sudo chown -R www-data:www-data /var/www/static-site
sudo chmod -R 755 /var/www/static-site

Configure Nginx to serve your static website by creating a new configuration file:

sudo nano /etc/nginx/sites-available/static-site

Add the following server block configuration to the file:

server {
    listen 80;
    server_name yourdomain.com;

    root /var/www/static-site;
    index index.html;

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

Enable the configuration by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/static-site /etc/nginx/sites-enabled/

Test the configuration for errors:

sudo nginx -t

Reload Nginx to apply the changes:

sudo systemctl reload nginx

To view your website, open your browser and navigate to http://yourdomain.com or http://<your-server-ip>. If you want to use a custom domain, update your DNS records to point to your server’s IP address and ensure the server_name in the Nginx configuration matches your domain.

For additional security, secure your website with HTTPS by installing an SSL certificate using Let’s Encrypt. Run the following commands:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

Follow the prompts to obtain and install the certificate. This ensures your website is accessible via HTTPS.

Hosting a static website with Nginx on Ubuntu is a simple yet powerful way to get your content online. By following the steps outlined in this guide, you can ensure a reliable and efficient hosting setup. Whether you’re showcasing a portfolio, sharing information, or creating a personal blog, this combination of Nginx and Ubuntu provides a solid foundation.

Have you tried hosting a static website using Nginx on Ubuntu? Share your thoughts or questions in the comments below !

Leave a Comment