Simple Way To Host Website on Nginx Web Server

In this tutorial, we'll explain how to host website on Nginx web server. NGINX configuration options are known as "directives" these are arranged into groups, known interchangeably as blocks or contexts. Configuration file plays important role for access the website.

Configuration files inform Nginx server to find website files in specific location as well as we can write some custom activity like restrict file extension, SSL configuration, PHP configuration, security headers, and many more. We can redirect certain path. We need to mention server_name in the configuration file so that it will understand Nginx and process it for that specific domain name.

Host Website on Nginx Web Server

Create configuration file

We need to create configuration file in /etc/nginx/conf.d/. You can name the configuration file anything you want but giving website name is highly recommended. Use your favorite editor.

sudo vi /etc/nginx/conf.d/ Note: Replace with your domain name.

Add following lines:

server {                                                                                                                                                                                                           
    listen 80;                                                                                                                                                                                                     
    server_name <Server IP or Domain Name>;                                                                                                                                                                                     
    index index.html index.php;                                                                                                                                                                                    
    root /usr/share/nginx/example;        

    # logging
    access_log /var/log/nginx/access.log combined buffer=512k flush=1m;
    error_log /var/log/nginx/error.log warn;                                                                                                                                                         

Here, In server_name you have to mention your domain name, it is the crucial part. Because it informs the Nginx about the domain configuration. Replace /usr/share/nginx/example with your domain path. This is website files path. For Ubuntu based server it is /var/www/

Save and exit the file.

Test and restart Nginx

To reflect the change, we need to restart the Nginx server. Before that we can test the configuration first and restart the service. This part is crucial because, if we restart the Nginx service directly and if we get an error, Nginx service will stop and running websites get down. So testing before restarting is highly recommended. Execute following command to test the configuration: nginx -t If you get any error, find the line number and check in configuration file. Next, restart the Nginx service using following command: systemctl restart nginx

That's it, navigate to your browser and access the website using your server IP or domain name.

We have seen how to host website on Nginx web server.