{"id":146702,"date":"2020-12-29T22:44:14","date_gmt":"2020-12-29T22:44:14","guid":{"rendered":"https:\/\/onet.com.vn\/how-to-install-and-configure-a-nginx-server-for-the-first-time.html"},"modified":"2020-12-29T22:44:14","modified_gmt":"2020-12-29T22:44:14","slug":"how-to-install-and-configure-a-nginx-server-for-the-first-time","status":"publish","type":"post","link":"https:\/\/onet.com.vn\/how-to-install-and-configure-a-nginx-server-for-the-first-time\/","title":{"rendered":"How to Install, And Configure a Nginx Server For the First Time"},"content":{"rendered":"\n
Nginx is one of the popular web servers, and is used as a proxy server, reverse proxy server, load balancer. It\u2019s a popular alternative to the aging Apache web server, as it\u2019s designed keeping resource intensive applications in mind. It is event driven, asynchronous and non-blocking, and therefore it frequently beats Apache in terms of performance. Nginx is often used in large web servers to which millions of users simultaneously connect to access resources.<\/p>\n

Being asynchronous, ability to handle millions of users without slowing down the server makes it the number one choice in many enterprises to deploy their systems. This guide demonstrates how to install, and configure Nginx web servers with ease. The guide uses Ubuntu 18.04 as the version because it\u2019s LTS; hence it has a long-term support which is required in a production environment. Installing and configuring a Nginx web server are relatively easy, but it involves a number of steps.<\/p>\n

Installation<\/h2>\n

These instructions were written for Ubuntu 18.04 LTS version, and thus it should not be used in a different Linux flavour unless the same commands work over there as well. It\u2019s encouraged to install Nginx in a regular user account with sudo permission in order to the mitigate security risk. However, this article doesn\u2019t demonstrate how to create a user account as it\u2019s out of its scope.<\/p>\n

    \n
  1. Prior to install Nginx, update the local package information, then update the packages with the following commands. It makes sure latest version of Nginx is retrieved from the repository (server) when Nginx install command is used. Dist-upgrade command intelligently handles dependencies to prevent incompatibility problems among different packages.<\/li>\n<\/ol>\n
    \n
    apt-get update<\/span> &&<\/span> apt-get dist-upgrade<\/span><\/div>\n<\/div>\n
      \n
    1. Install Nginx with the following command<\/li>\n<\/ol>\n
      \n
      apt-get install<\/span> nginx<\/div>\n<\/div>\n
        \n
      1. The installation only requires 3 main commands to be used, then Nginx is installed in the server. Since in this guide, Nginx is used as a web server, the index.html is created as soon as Nginx is installed, and it can be accessed through the external IP address of the server.<\/li>\n<\/ol>\n

        http:\/\/IPAddress<\/a><\/p>\n

        <\/a><\/p>\n

          \n
        1. Even though it\u2019s installed, it\u2019s important to make sure Nginx service automatically starts its own if in case the server is restarted for some reason. It can be done as following.<\/li>\n<\/ol>\n
          \n
          sudo<\/span> systemctl enable<\/span> nginx<\/div>\n<\/div>\n
            \n
          1. Use the following two commands to adjust the file system permissions. The first command assigns currently logged in user\u2019s name to the file\u2019s permission. If it\u2019s root, then it\u2019s root, if it\u2019s a custom name, then its name. With the second command the file\u2019s permission is set. Since permission for \u201call users\u201d is set to R, the file can be read by anyone, which is recommended for publicly accessible files. W standards for write permission, which is required for owner to make changes to the file, and It comes handy when a file is modified through a script while being in the server, such as on the WordPress dashboard.<\/li>\n<\/ol>\n
            \n
            sudo<\/span> chown<\/span> -R<\/span> $USER<\/span>:$USER<\/span> \/<\/span>var\/<\/span>www\/<\/span>html
            sudo<\/span> chmod<\/span> -R<\/span> 755<\/span> \/<\/span>var\/<\/span>www\/<\/span>example.com<\/div>\n<\/div>\n

            <\/a><\/p>\n

            Configuration<\/h3>\n

            Installing Nginx is simple as described above, but configuration requires more effort, and it also depends on the requirements and environment of the server. This guide demonstrates how to configure a nginx web server for one domain, how to adjust basic settings, how to set up SSL\/TLS, which is required by Google to improve the rank of the web site, and finally what commands are involved in setting up a Nginx server.<\/p>\n

              \n
            1. Use the following command to open Nginx default file via nano editor. Default file is automatically created when Nginx is installed at first time, and defines the configuration for a web server. This configuration contains a server block which is dedicated for one domain name, and processes the requests to its domain as per the rules within its boundary. Nano editor is just a console editor which helps in opening text files with ease. It\u2019s highly recommended to use a better editor like Notepad++ with NppFTP extension as it\u2019s quite user friendly compared to a console text editor. <\/a><\/li>\n<\/ol>\n
              \n
              nano<\/span> \/<\/span>etc\/<\/span>nginx\/<\/span>sites-available\/<\/span>default<\/div>\n<\/div>\n

              The configuration file contains a few important lines as seen in the following code snippet.<\/p>\n