Saturday, February 23, 2019

Nginx - Host a static website

Greetings!

Let's play with nginx and serve a static website. ;)

Step 1: Install nginx


sudo apt install nginx


Step 2: Update hosts file to add our site name


sudo vim /etc/hosts
127.0.0.1 nginx-site.com


Step 3: Unlink default site


cd /etc/nginx/sites-enabled
sudo unlink default


Step 4: Create folder for the site


cd /var/www
sudo mkdir nginx-site.com
sudo chmod o+w nginx-site.com
touch nginx-site.com/index.html
echo "coming soon" > nginx-site.com/index.html


Step 5: Configure nginx

Now we need to nginx where to find our site.

  • sites-available - contains configurations for available sites
  • sites-enabled - contains links to configurations in sites-available. Nginx uses this location to read and run.


sudo vim /etc/nginx/sites-available/nginx-site.com

server {

        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/nginx-site.com;

        index index.html;

        server_name nginx-site.com www.nginx-site.com;

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


Create symbolic link


sudo ln -s /etc/nginx/sites-available/nginx-site.com /etc/nginx/sites-enabled/nginx-site.com


Test the configuration and restart nginx

sudo nginx -t
sudo systemctl restart nginx


Visit our website in the brower
nginx-site.com

No comments:

Post a Comment