How to install Nginx on ubuntu 18.04

How to install Nginx on ubuntu 18.04

So, let’s start the article for how to install Nginx on ubuntu 18.04

Prerequisites
– Ubuntu 18.04 with SSH access.

NOTE: I’m not responsible for any type of data loss.

How to install Nginx on ubuntu 18.04 Prashant Suthar

Update your package for Ubuntu.

 sudo apt-get update 

 

 sudo apt-get upgrade 

Now Install Nginx

 sudo apt install nginx 

before using Nginx we need to setup basic firewall to access server.

 sudo ufw app list 

You got these in terminal

Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

If you do not able HTTPS then do this.

Now let’s check the step for Nginx is properly active/started or not.

 systemctl status nginx 

You will get below type message.
its successfully working.

 nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-08-21 08:15:12 CDT; 3min 57s ago
     Docs: man:nginx(8)
 Main PID: 1196 (nginx)
    Tasks: 5 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─1196 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─1197 nginx: worker process
           ├─1198 nginx: worker process
           ├─1199 nginx: worker process
           └─1200 nginx: worker process

Or you can hit by your IP address.

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Now Let’s set up folder based website like EXAMPLE1DEMO.COM, EXAMPLEDEMO.COM for multi host website on same VPS.

If you don’t want to host multiple site on sever then you had done. Just go to /var/www/html and check it.

You have to just give permission to folder which step is after below step.

sudo mkdir -p /var/www/exampledemo.com/html

Now give permission and ownership to the folder.
NOTE : $USER is as sample you have to replace as per your username and all.

 sudo chown -R $USER:$USER /var/www/exampledemo.com/html 

 

 sudo chmod -R 755 /var/www/exampledemo.com/html 

Now Let’s create a sample file for testing using nano command ( its used in linux ubuntu only. )

 cd  /var/www/exampledemo.com/html 

 

 nano /var/www/exampledemo.com/html/index.html 

Now we need to create/bind as virtual host with exampledemo.com

 cd /etc/nginx/sites-available/ 
 sudo nano exampledemo.com 

paste this text / change as per your domain name and executions.

server {
        listen 80;
        listen [::]:80;

        root /var/www/exampledemo.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name exampledemo.com www.exampledemo.com;

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

after this we need to enable this site.

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

To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file:

 sudo /etc/nginx/nginx.conf 

now find server_names_hash_bucket_size and remove hash before this line.

Save and close it.

Let’s test this environment.

 sudo nginx -t 

If there is no any problem just restart service.

 sudo systemctl restart nginx 

Here is few file path for Ngnix server.

/etc/nginx : nginx configuration file.
/etc/nginx/nginx.conf : main nginx config file. you have to copy and modified it. if you need to extend or modify.
/etc/nginx/sites-available/ : active site/block on website.
/etc/nginx/sites-enabled/ : pre-site active block of website.

Server logs:
/var/log/nginx/access.log, /var/log/nginx/error.log

I hope you like this article. Keep visiting my website for more upcoming articles. If you need any help with How to install nginx on ubuntu 18.04  How to fetch data from MsSQL in NodeJS you can contact me. You can ask me questions in comments also. You can connect me on social media as well links are below in the footer section. Keep connected. Happy Coding.

Prashant Sutharhttps://prashantsuthar.com
My self Prashant Suthar, I had experience worked with NodeJS, Core PHP, WordPress, CodeIgniter, Shopify, Prestashop, Opencart and many frameworks. I had some basic knowledge about server setup and maintenance. I also worked with third parties APIs like Twillio audio,video, SMS API, FB messenger API and many more. I am working as team lead & sr. developer in Ahmedabad, GJ, IN.

Comments

Similar Articles