Explained – How do I host a website using Nginx?

I have purchased a cloud server with DigitalOcean / AWS / Azure / Google Cloud and now I want to host my website on it. But since it doesn’t have any GUI, how do I do that?

Note – This tutorial assumes you are working with an Ubuntu Server (any version).

Installing Nginx – the right way!

Step 1  – Add Nginx Repository

This is one of the very first questions thrown around by linux administrators that have just begun. In this tutorial, I will walk you through setting up nginx.

You would see quite a few tutorial diving straight into “install nginx” as the first step. Do NOT follow that advise, as nginx installed from the operating system’s repository is usually very old and you would end up with missing features or security issues.
Follow these commands to add Nginx’s official repository to your server. Here is the link to Official Nginx Repository information. 

  • Create the repository file
    sudo vim /etc/apt/sources.list.d/nginx.list
  • Copy paste the following text into the file
    deb http://nginx.org/packages/ubuntu/ xenial nginx
    deb-src http://nginx.org/packages/ubuntu/ xenial nginx
    Here, “xenial” represents Ubuntu-16.04. You need to replace “xenial” with “trusty” if you are using Ubuntu-14.04.

Step 2 – Add Nginx’s public key and install nginx

- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62
- sudo apt-get update
- sudo apt-get install -y nginx
"ABF5BD827BD9BF62" is the Nginx public key that you can find here.

Congratulations! You have the latest version of Nginx now installed on your server, from Nginx’s repository and not that of the operating system’s.

Overview of Nginx config files –

The first important nginx configuration file we’re going to look at is called “nginx.conf”. This file dictates the behavior of nginx. The file can be found at “/etc/nginx/nginx.conf”. Ofcourse you would need sudo access to edit the file.

sudo vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

Let’s break it down to understand each line.

  • user nginx : The nginx process would be run by user “nginx”.
  • worker_processes : Its the number of processes that would handle worker_connections. For example, if you have only one worker_process, in this case you would be able to handle 1024 connections. If you set worker_process to 2, the server would be able to handle 1024 x 2 = 2048 concurrently active connections
  • error_log : Path to store the error logs of this particular application. It is customizable.
  • pid : Linux pid that would be used.
  • http : Block defining standard nginx configuration including the location of the access logs of the application.
  • include : This is the path where nginx would look for configuration files of the websites / applications that you set up. It is customizable.

Let’s set up our first website’s configuration file!

For the first website, we can simply edit the “default.conf” nginx configuration file and replace that with the location of our own website / application. 

cd /etc/nginx/conf.d/
sudo mv default.conf default.old

Create a new default.conf with the following code.
sudo vim default.conf

server {
    listen       80;
    server_name  npsoni.com www.npsoni.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www/npsoni.com/html;
        index  index.html index.htm;

    #error_page  404              /404.html;

This is the most basic nginx file, which is executing the following tasks.

  1. Listening to incoming requests on port 80, which is the default HTTP port.
  2. Defining the server name, in my case its “npsoni.com” or “www.npsoni.com”, you can replace that with your domain ame.
  3. We have commented out “access_log”, but you can uncomment it and define any specific path if you want to have separate access logs for each one of your websites.
  4. Location is defining the default location of your website files. This path would be served via HTTP when a visitor hits the “server_name”.
  5. A few more commented lines in case you want ot define a global 404 page.

Sweet, that should be enough to setup your first static website using nginx! You can create more “.conf” files in “/etc/nginx/conf.d” if you want to run more than 1 website on the same server from different “location”.

Next steps would be to –

  • Enable PHP in nginx website configuration to serve PHP applications such as PHP
  • Enabel SSL in nginx website configuration file to serve your website over HTTPS
  • Learn how to use “round-robin” approach to use nginx as a load-balancer for your website / application.

Published by Nishant

With over 9 years in the industry, initially started as a Linux administrator and transitioned into DevOps Engineer. I work with deployment and infrastructure automation as well as application release management processes.

Leave a comment

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

Hooman? *