{"id":5337,"date":"2023-10-12T07:20:43","date_gmt":"2023-10-12T07:20:43","guid":{"rendered":"https:\/\/natsav.com\/blog\/?p=5337"},"modified":"2024-07-08T11:12:35","modified_gmt":"2024-07-08T11:12:35","slug":"how-to-install-and-configure-nginx-on-ubuntu","status":"publish","type":"post","link":"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/","title":{"rendered":"How To Install And Configure Nginx on Ubuntu 18.04 LTS"},"content":{"rendered":"\n<p><strong>How To Install And Configure Nginx on Ubuntu 18.04 LTS<\/strong>, phonetically articulated as Engine X, stands as an open-source, highly regarded web server deployed to accommodate websites and applications. It garners widespread acclaim for its proficiency in functioning as a Load Balancer. This guide delineates the procedural steps necessary to install Nginx on the widely-utilized Linux distribution, Ubuntu. Specifically, it outlines the installation process for Ubuntu 18.04 LTS. These instructions should be largely applicable to other Linux systems and various iterations of Ubuntu.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/natsav.com\/blog\/install-git-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Install Git on Ubuntu 20.04<\/a><\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_68_1 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#How_To_Install_And_Configure_Nginx_on_Ubuntu\" title=\"How To Install And Configure Nginx on Ubuntu\">How To Install And Configure Nginx on Ubuntu<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Install_And_Configure_Nginx_on_Ubuntu_%E2%80%93_Latest\" title=\"Install And Configure Nginx on Ubuntu &#8211; Latest\">Install And Configure Nginx on Ubuntu &#8211; Latest<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Certbot_Plugin\" title=\"Certbot Plugin\">Certbot Plugin<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Authenticate_Nginx_Installation\" title=\"Authenticate Nginx Installation\">Authenticate Nginx Installation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Common_Commands\" title=\"Common Commands\">Common Commands<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Disable_Default_Server_Block\" title=\"Disable Default Server Block\">Disable Default Server Block<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Configuring_Individual_Websites\" title=\"Configuring Individual Websites\">Configuring Individual Websites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Hash_Bucket_Memory_Problem\" title=\"Hash Bucket Memory Problem\">Hash Bucket Memory Problem<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/natsav.com\/blog\/how-to-install-and-configure-nginx-on-ubuntu\/#Summary\" title=\"Summary\">Summary<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_To_Install_And_Configure_Nginx_on_Ubuntu\"><\/span>How To Install And Configure Nginx on Ubuntu<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can install Nginx directly from the Ubuntu repositories using the commands as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Refresh packages index<br>sudo apt update<br><br># Install nginx<\/strong><br><strong><br>sudo apt install nginx<br><br># Installation output<\/strong><br><strong><br>Reading package lists&#8230; Done<\/strong><br><strong><br>Building dependency tree<\/strong><br><strong><br>Reading state information&#8230; Done<\/strong><br><strong><br>The following additional packages will be installed:<br>libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common nginx-core<\/strong><br><strong><br>Suggested packages:<\/strong><br><strong><br>fcgiwrap nginx-doc<\/strong><br><strong><br>The following NEW packages will be installed:<\/strong><br><strong><br>libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx nginx-common<br>nginx-core<br>&#8230;<br>&#8230;<br>Setting up nginx-common (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Created symlink \/etc\/systemd\/system\/multi-user.target.wants\/nginx.service \u2192 \/lib\/systemd\/system\/nginx.service.<\/strong><br><strong><br>Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Setting up libnginx-mod-mail (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Setting up libnginx-mod-stream (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Setting up nginx-core (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Setting up nginx (1.14.0-0ubuntu1.7) &#8230;<\/strong><br><strong><br>Processing triggers for man-db (2.8.3-2ubuntu0.1) &#8230;<\/strong><br><strong><br>Processing triggers for ufw (0.36-0ubuntu0.18.04.1) &#8230;<\/strong><br><strong><br>Processing triggers for ureadahead (0.100.0-21) &#8230;<\/strong><br><strong><br>Processing triggers for systemd (237-3ubuntu10.38) &#8230;<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                    <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Now verify the Nginx installation by checking it&#8217;s version as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Nginx Version<\/strong><br><strong><br>nginx -v<br><br># Output<\/strong><br><strong><br>nginx version: nginx\/1.14.0 (Ubuntu)<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                  <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>How To Install And Configure Nginx on Ubuntu. The aforementioned directives will facilitate the installation of the Nginx server along with its requisite dependencies. On my setup, specifically Ubuntu 18.04 LTS, I have installed Nginx version 1.14.0. This version may differ depending on your operating system. Below are the pivotal configurations and directories for Nginx when installed from the Ubuntu repositories.<\/p>\n\n\n\n<ul>\n<li class=\"has-medium-font-size\"><strong>Configurations Directory:<\/strong> <code>\/etc\/nginx\/conf.d<\/code> &#8211; This directory is intended for supplementary server configurations and is initially unoccupied.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Modules Available Directory:<\/strong> <code>\/etc\/nginx\/modules-available<\/code> &#8211; Here, you will find the configuration files for various modules.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Modules Enabled Directory: <\/strong><code>\/etc\/nginx\/modules-enabled<\/code> &#8211; This directory contains symlinks to the module configurations. By default, symlinks for geoip, image filter, xslt filter, mail, and stream modules are present.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Sites Available Directory: <\/strong><code>\/etc\/nginx\/sites-available<\/code> &#8211; This is where server block configurations are stored, including the default server block.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Sites Enabled Directory:<\/strong> <code>\/etc\/nginx\/sites-enabled<\/code> &#8211; This directory holds symlinks to the server blocks located in <code>\/etc\/nginx\/sites-available<\/code>. You can activate or deactivate server blocks by creating or removing these symlinks. The default server block symlink is present here.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Main Configuration:<\/strong> <code>\/etc\/nginx\/nginx.conf<\/code> &#8211; This file contains the primary configuration settings for Nginx. It can be viewed on GitHub.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Default Server Block:<\/strong> <code>\/etc\/nginx\/sites-available\/default<\/code> &#8211; This file represents the default server block and is also available on GitHub.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"400\" src=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-To-Install-And-Configure-Nginx-on-Ubuntu-natsav.com_.webp?resize=799%2C400&#038;ssl=1\" alt=\"\" class=\"wp-image-6714\" srcset=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-To-Install-And-Configure-Nginx-on-Ubuntu-natsav.com_.webp?w=799&amp;ssl=1 799w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-To-Install-And-Configure-Nginx-on-Ubuntu-natsav.com_.webp?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-To-Install-And-Configure-Nginx-on-Ubuntu-natsav.com_.webp?resize=768%2C384&amp;ssl=1 768w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-To-Install-And-Configure-Nginx-on-Ubuntu-natsav.com_.webp?resize=150%2C75&amp;ssl=1 150w\" sizes=\"(max-width: 799px) 100vw, 799px\" data-recalc-dims=\"1\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Install_And_Configure_Nginx_on_Ubuntu_%E2%80%93_Latest\"><\/span>Install And Configure Nginx on Ubuntu &#8211; Latest<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can opt to install the most up-to-date version of Nginx by utilizing the official Nginx repository. To completely remove the current installation, use the commands provided below. Ensure you backup any existing configuration files, especially if you have been using Nginx to host applications.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Backup main configuration<\/strong><br><strong><br>sudo cp \/etc\/nginx\/nginx.conf \/&lt;backups path>\/nginx.conf.bak<br><br># Backup server blocks<br><br># Completely Uninstall Nginx<\/strong><br><strong><br>sudo apt purge nginx nginx-full nginx-common nginx-core<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                  <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Now create the repository source file using the commands as shown below. I have used the nano editor to create and save the file. You can use any editor of your choice.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Create the repository source file<\/strong><br><strong><br>sudo nano \/etc\/apt\/sources.list.d\/nginx.list<br><br># Update the file<\/strong><br><strong><br>deb [arch=amd64] http:\/\/nginx.org\/packages\/mainline\/ubuntu\/ bionic nginx<br>deb-src http:\/\/nginx.org\/packages\/mainline\/ubuntu\/ bionic nginx<br><br># Save and close the editor<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                            <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Save the file using the Nano text editor by pressing\u00a0<strong>CTRL + O<\/strong>, then press\u00a0<strong>Enter<\/strong>\u00a0to write the file. Press\u00a0<strong>CTRL + X<\/strong>\u00a0to close the editor. You can use the below-mentioned content for\u00a0<strong>Ubuntu 19.04<\/strong>\u00a0by replacing\u00a0<strong>bionic<\/strong>\u00a0with\u00a0<strong>disco<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Update the file<br><br>deb [arch=amd64] http:\/\/nginx.org\/packages\/mainline\/ubuntu\/ disco nginx<br><br>deb-src http:\/\/nginx.org\/packages\/mainline\/ubuntu\/ disco nginx<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                               <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Now verify the package integrity using the commands as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Get the signing key<\/strong><br><strong><br>wget http:\/\/nginx.org\/keys\/nginx_signing.key<br><br># Add the key<\/strong><br><strong><br>sudo apt-key add nginx_signing.key<br><br># Output<\/strong><br><strong><br>OK<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                             <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>The above commands confirm the addition of the official Nginx repository to the system. Now refresh the packages index and install the latest Nginx using the commands as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Refresh packages index<\/strong><br><strong><br>sudo apt update<br><br># Install nginx<\/strong><br><strong><br>sudo apt install nginx<br><br># Installation output<\/strong><br><strong><br>Reading package lists&#8230; Done<\/strong><br><strong><br>Building dependency tree<\/strong><br><strong><br>Reading state information&#8230; Done<\/strong><br><strong><br>The following NEW packages will be installed:<\/strong><br><strong><br>nginx<br>&#8230;.<br>&#8230;.<br>Unpacking nginx (1.19.0-1~bionic) &#8230;<\/strong><br><strong><br>Setting up nginx (1.19.0-1~bionic) &#8230;<\/strong><br><strong><br>Created symlink \/etc\/systemd\/system\/multi-user.target.wants\/nginx.service \u2192 <\/strong><br><strong>\/lib\/systemd\/system\/nginx.service.<\/strong><br><strong><br>Processing triggers for man-db (2.8.3-2ubuntu0.1) &#8230;<\/strong><br><strong><br>Processing triggers for ureadahead (0.100.0-21) &#8230;<\/strong><br><strong><br>Processing triggers for systemd (237-3ubuntu10.38) &#8230;<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                          <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Now verify the installation by checking the Nginx version as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Check Nginx Version<\/strong><br><strong><br>nginx -v<br><br># Output<\/strong><br><strong><br>nginx version: nginx\/1.19.0<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Nginx version 1.19.0 was successfully installed on Ubuntu 18.04 LTS. Below are the crucial configuration files and directories for Nginx installations derived from the official Nginx repositories.<\/p>\n\n\n\n<ul>\n<li class=\"has-medium-font-size\"><strong>Configuration Directory<\/strong>: <code>\/etc\/nginx\/conf.d<\/code> &#8211; Contains supplementary server configurations. The default server block resides in this directory.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Modules Directory<\/strong>: This is a symbolic link to <code>\/usr\/lib\/nginx\/modules<\/code>.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Main Configuration File<\/strong>: <code>\/etc\/nginx\/nginx.conf<\/code> &#8211; This file holds the primary configurations for Nginx. You can view it on GitHub.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Default Server Block<\/strong>: <code>\/etc\/nginx\/conf.d\/default.conf<\/code> &#8211; This file represents the default server block and is available for viewing on GitHub.<\/li>\n<\/ul>\n\n\n\n<p>Upon comparing the Nginx installations from the Ubuntu repositories with those from the Nginx repositories, we observe several distinctions. The Nginx version from the Nginx repositories lacks configuration directories such as modules available, modules enabled, sites available, and sites enabled. Additionally, the default server block is configured within the <code><strong>\/etc\/nginx\/conf.d<\/strong><\/code> directory.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Include server block files<\/strong><br><strong><br>sudo nano \/etc\/nginx\/nginx.conf<br><br># Scroll down and make sure that the below mentioned lines are there in the http block<br>include \/etc\/nginx\/conf.d\/*.conf;<\/strong><br><strong><br>include \/etc\/nginx\/sites-enabled\/*;<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                  <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Turn off the server token for production usage.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Update config<\/strong><br><strong><br>sudo nano \/etc\/nginx\/nginx.conf<br><br># Update\/Add server tokens config &#8211; http block<br>server_tokens off;<br><br># Save and exit the editor<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Certbot_Plugin\"><\/span>Certbot Plugin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Optionally, implement the Certbox extension indispensable for procuring Let&#8217;s Encrypt SSL certifications. We can leverage Certbox to fabricate and install SSL certifications, thereby fortifying the website or application with robust security measures.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Install Certbot Plugin<\/strong><br><br><strong><br>sudo apt install python3-certbot-nginx<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                            <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Embark on your journey of mastering Nginx by delving into comprehensive guides on setting up Virtual Hosts or Server Blocks, and on the meticulous installation of Let\u2019s Encrypt SSL certificates on Ubuntu. These resources illuminate the intricate processes behind Server Block configurations and the generation of SSL certificates with Let\u2019s Encrypt.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/natsav.com\/blog\/install-wildcard-ssl-certificate\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Install a Wildcard SSL Certificate on Multiple Servers<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Authenticate_Nginx_Installation\"><\/span>Authenticate Nginx Installation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To ascertain the integrity of the Nginx installation accomplished in the preceding segments, execute the following commands, which serve as a testament to the veracity of your setup:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Check version<\/strong><br><strong><br>nginx -v<br><br># Ubuntu repositories<\/strong><br><strong><br>nginx version: nginx\/1.14.0 (Ubuntu)<br><br># Nginx repository<\/strong><br><strong><br>nginx version: nginx\/1.17.4<br><br># Detailed version details<br>nginx -V<br><br># Output<\/strong><br><strong><br>nginx version: nginx\/1.17.4<\/strong><br><strong><br>built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)<\/strong><br><strong><br>built with OpenSSL 1.1.1 11 Sep 2018<\/strong><br><strong><br>TLS SNI support enabled<br>&#8230;.<br>&#8230;.<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                       <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p><br>Also, check the configuration file and status of Nginx using the command as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Check configuration<br>sudo nginx -t<br><br># Output<br>nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok<br>nginx: configuration file \/etc\/nginx\/nginx.conf test is successful<br><br><br># Check status<br>systemctl status nginx<br><br># It will show status<br>\u25cf nginx.service &#8211; nginx &#8211; high performance web server Loaded: loaded (\/lib\/systemd\/system\/nginx.service; enabled; vendor preset: enabled) Active: inactive (dead) Docs: http:\/\/nginx.org\/en\/docs\/<br>&#8230;.<br>&#8230;.<\/strong><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                     <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>We can see that the Nginx is installed successfully and the configuration is ok.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># Start Nginx<br><br>sudo systemctl <strong>start<\/strong> nginx<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                  <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<p>The quintessential directory for Nginx&#8217;s web files resides at \/usr\/share\/nginx\/html. This repository serves as the locus from which static content is disseminated, facilitated by the default server configuration.<\/p>\n\n\n\n<p>Proceed to launch your preferred web navigator and input http:\/\/localhost, pressing Enter thereafter. For instances involving a remote server, substitute with the corresponding IP address, formatted as http:\/\/xx.xx.xx.xx, where xx.xx.xx.xx symbolizes the server&#8217;s IP. This should unveil the default Nginx landing page, illustrated in Fig 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Common_Commands\"><\/span>Common Commands<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This section lists the commonly used commands to manage the Nginx process. These commands are listed below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># Nginx Status<br>sudo systemctl <strong>status<\/strong> nginx<br><br># Start Nginx<br>sudo systemctl <strong>start<\/strong> nginx<br><br># Check status<br>sudo systemctl <strong>status<\/strong> nginx<br><br># Status Output after starting the server<br>\u25cf nginx.service &#8211; nginx &#8211; high performance web server<br>Loaded: loaded (\/lib\/systemd\/system\/nginx.service; enabled; vendor preset: enabled)<br>Active: active (running) since Wed 2019-10-02 14:08:59 IST; 3s ago<br>Docs: http:\/\/nginx.org\/en\/docs\/<br>Process: 6263 ExecStart=\/usr\/sbin\/nginx -c \/etc\/nginx\/nginx.conf (code=exited, status=0\/SUCCESS)<br>Main PID: 6264 (nginx)<br>Tasks: 2 (limit: 4915)<br>CGroup: \/system.slice\/nginx.service<br>\u251c\u25006264 nginx: master process \/usr\/sbin\/nginx -c \/etc\/nginx\/nginx.conf<br>\u2514\u25006265 nginx: worker process<br>&#8230;<br>&#8230;<br><br># Stop Nginx<br>sudo systemctl <strong>stop<\/strong> nginx<br><br># Reload Nginx<br>sudo systemctl <strong>reload<\/strong> nginx<br><br># Enable Nginx on system boot<br>sudo systemctl <strong>enable<\/strong> nginx<br><br># Disable Nginx on system boot<br>sudo systemctl <strong>disable<\/strong> nginx<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">                                                         <strong>How To Install And Configure Nginx on Ubuntu<\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Disable_Default_Server_Block\"><\/span>Disable Default Server Block<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This step is optional and you can follow it in case you want to disable the default server block which shows the\u00a0<strong>Default Page<\/strong>\u00a0as shown in Fig 1. It can be done as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Disable default configs<br>sudo nano \/etc\/nginx\/nginx.conf<br><br># Scroll down and update the http block<br>&#8230;.<br>&#8230;.<br>keepalive_timeout 65;<br><br>#gzip on;<br><br><strong>#include \/etc\/nginx\/conf.d\/*.conf;<\/strong><br>include \/etc\/nginx\/sites-enabled\/*;<br>}<br><br># Save and exit the editor<br><br># Optional &#8211; Only if sites-available and sites-enabled directories exist<br># Remove default config symlink from sites-enabled<br>sudo rm \/etc\/nginx\/sites-enabled\/default<br><br># Reload Nginx<br>sudo systemctl <strong>reload<\/strong> nginx<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Upon the actuation of an NGINX reload, the default server block is rendered inactive. Should the directory of configurations at \/etc\/nginx\/conf.d be requisitioned for alternative configurations beyond server blocks, the pre-existing default configuration file can be excised.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># <strong>Backup<\/strong> the file &#8211; <strong>\/etc\/nginx\/conf.d\/default.conf<\/strong><br>sudo mkdir \/etc\/nginx\/sites-available<br>sudo mv \/etc\/nginx\/conf.d\/default.conf \/etc\/nginx\/sites-available\/default<br><br># Enable default configs<br>sudo nano \/etc\/nginx\/nginx.conf<br><br># Scroll down and update the http block<br>&#8230;.<br>&#8230;.<br>keepalive_timeout 65;<br><br>#gzip on;<br><br><strong>include \/etc\/nginx\/conf.d\/*.conf;<\/strong><br>include \/etc\/nginx\/sites-enabled\/*;<br>}<br><br># Save and exit the editor<br><br># Remove default server block &#8211; if exists<br>sudo rm \/etc\/nginx\/conf.d\/default.conf<br><br># Optional &#8211; Only if sites-available and sites-enabled directories exist<br># Remove default config symlink from sites-enabled<br>sudo rm \/etc\/nginx\/sites-enabled\/default<br><br># Reload Nginx<br>sudo systemctl <strong>reload<\/strong> nginx<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Thus, we can maintain the configurations within the \/etc\/nginx\/conf.d\/ directory instead of completely disabling it. If you attempt to access the default URL in your browser, an error message will be displayed: &#8220;Unable to connect.&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configuring_Individual_Websites\"><\/span>Configuring Individual Websites<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This section delves into the process of adding configurations for each website independently. Begin by establishing the site configuration using the default settings as outlined below. In the examples provided, the domain name used is example.com. Be sure to substitute it with your own domain.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Create the directories if does not exist<br>sudo mkdir \/etc\/nginx\/sites-available<br>sudo mkdir \/etc\/nginx\/sites-enabled<br><br># Copy the default config<br>sudo cp \/etc\/nginx\/sites-available\/default \/etc\/nginx\/sites-available\/example.com<br># OR<br>sudo cp \/etc\/nginx\/conf.d\/default.conf \/etc\/nginx\/sites-available\/example.com<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Next, modify the configuration to integrate your domain and designate the system path containing your website&#8217;s files. The configuration should resemble the example provided below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Update configuration<br>sudo nano \/etc\/nginx\/sites-available\/example.com<br><br># Content<br>server {<br>listen 80;<br>server_name example.com www.example.com;<br><br>#charset koi8-r;<br>#access_log \/var\/log\/nginx\/host.access.log main;<br><br>location \/ {<br>root \/var\/www\/example.com\/html;<br>index index.html index.htm;<br>}<br><br>#error_page 404 \/404.html;<br><br># redirect server error pages to the static page \/50x.html<br>#<br>error_page 500 502 503 504 \/50x.html;<br>location = \/50x.html {<br>root \/usr\/share\/nginx\/html;<br>}<br><br># proxy the PHP scripts to Apache listening on 127.0.0.1:80<br>#<br>#location ~ \\.php$ {<br># proxy_pass http:\/\/127.0.0.1;<br>#}<br><br># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000<br>#<br>#location ~ \\.php$ {<br># root html;<br># fastcgi_pass 127.0.0.1:9000;<br># fastcgi_index index.php;<br># fastcgi_param SCRIPT_FILENAME \/scripts$fastcgi_script_name;<br># include fastcgi_params;<br>#}<br><br># deny access to .htaccess files, if Apache&#8217;s document root<br># concurs with nginx&#8217;s one<br>#<br>#location ~ \/\\.ht {<br># deny all;<br>#}<br>}<br><br># Save and exit the editor<br><br># Create the site directory and update permissions<br>sudo mkdir \/var\/www\/example.com\/html<br>sudo chmod -R 755 \/var\/www\/example.com<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Use the below-mentioned commands to enable or disable the site configuration.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Enable Site &#8211; Create the symlink<br>sudo ln -s \/etc\/nginx\/sites-available\/example.com \/etc\/nginx\/sites-enabled\/<br><br># Disable Site<br>sudo rm \/etc\/nginx\/sites-enabled\/example.com<br># OR<br>sudo unlink \/etc\/nginx\/sites-enabled\/example.com<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Apply the configuration using the below-mentioned commands.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Check configurations<br>sudo nginx -t<br><br># Reload configuration<br>sudo nginx -s reload<br># OR<br>sudo systemctl reload nginx<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now add the\u00a0<strong>index.html<\/strong>\u00a0file having content as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Add index.html<br>sudo nano \/var\/www\/example.com\/html\/index.html<br><br># Content<br>&lt;!DOCTYPE html><br>&lt;html lang=&#8221;en&#8221;><br>&lt;head><br>&lt;title>My Domain&lt;\/title><br>&lt;\/head><br>&lt;body><br>&lt;h1>Welcome to My Domain.&lt;\/h1><br>&lt;\/body><br>&lt;\/html><br><br># Save and exit the editor<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This is all about configuring the site. You may fine-tune the configuration by adding error pages, SSL certificate, etc based on your site needs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Hash_Bucket_Memory_Problem\"><\/span>Hash Bucket Memory Problem<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You may face the hash bucket memory problem in case you are using server blocks. It can be avoided by updating the configuration file as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Update config<br>sudo nano \/etc\/nginx\/nginx.conf<br><br># Update the file<br><br>&#8230;<br>http {<br>&#8230;<br>server_names_hash_bucket_size 64;<br>&#8230;<br>}<br>&#8230;<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Apply the configuration using the below-mentioned commands.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong># Check configurations<br>sudo nginx -t<br><br># Restart nginx<br>sudo systemctl restart nginx<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now open your website with the URL http:\/\/ekample.com or http:\/\/www.ekample.com. It should display your website. Make sure the domain entry is properly configured for use by your server. The domain should point to your server&#8217;s IP address.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This way we can install and configure Nginx for Ubuntu. The previous section also provides common commands used for Nginx.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To start, please log in to your Ubuntu instance. Next, it&#8217;s important to update the repository to ensure that we have the most recent and secure version. Once the repository is successfully updated, we can proceed. To start, please log in to your Ubuntu instance. Next, it&#8217;s important to update the repository to ensure that we have the most recent and secure version. Once the repository is successfully updated, we can proceed.<\/p>\n","protected":false},"author":1,"featured_media":5346,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[255],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2023\/10\/nginx.png?fit=798%2C478&ssl=1","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts\/5337"}],"collection":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/comments?post=5337"}],"version-history":[{"count":36,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts\/5337\/revisions"}],"predecessor-version":[{"id":6717,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts\/5337\/revisions\/6717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/media\/5346"}],"wp:attachment":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/media?parent=5337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/categories?post=5337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/tags?post=5337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}