{"id":1026,"date":"2018-03-16T17:15:20","date_gmt":"2018-03-16T17:15:20","guid":{"rendered":"https:\/\/www.newdelhihosting.co.in\/blog\/?p=1026"},"modified":"2018-03-17T11:10:25","modified_gmt":"2018-03-17T11:10:25","slug":"install-and-config-nginx-in-centos-7","status":"publish","type":"post","link":"https:\/\/www.newdelhihosting.co.in\/blog\/install-and-config-nginx-in-centos-7\/","title":{"rendered":"How to install and config Nginx in CentOS 7"},"content":{"rendered":"<p><strong>NGINX\u00a0<\/strong>is a free, open-source and powerful HTTP web server and reverse proxy with an event-driven architecture.<\/p>\n<p>It is a high performance web server software. It is a much more flexible and lightweight program than Apache HTTP Server.<\/p>\n<p>It also works as a reverse proxy, standard mail and TCP\/UDP proxy server, and can additionally be configured as a load balancer. It is powering many sites on the web; well known for its high-performance, stability and feature-rich set.<\/p>\n<p>In this article, we will explain how to install, configure and manage Nginx HTTP web server on a CentOS 7 server using command line.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>System Requirements:<\/strong><\/span><\/p>\n<ul>\n<li>CentOS 7.x server with minimal installation.<\/li>\n<li>root credentials.<\/li>\n<\/ul>\n<h4><span style=\"text-decoration: underline;\"><strong>Install Nginx Web Server<\/strong><\/span><\/h4>\n<p><strong>Step 1:\u00a0<\/strong>Let update our server and packages to latest version.<\/p>\n<p>[root@newdelhihosting ~]#\u00a0yum -y update<\/p>\n<p>Updating the server may takes, more time. Once the update completes reboot the server<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:\u00a0<\/strong>Now, we go with install Nginx HTTP server from the <a href=\"https:\/\/www.newdelhihosting.co.in\/blog\/enable-epel-repository-in-centos-7\/\">EPEL repository<\/a> using the YUM package manager<\/p>\n<pre>[root@newdelhihosting ~]# yum install epel-release\r\n[root@newdelhihosting ~]# yum install nginx<\/pre>\n<pre>=================================================================================\r\n Package                        Arch      Version             Repository  Size\r\n=================================================================================\r\nInstalling:\r\n nginx                        x86_64    1:1.12.2-1.el7       epel        529 k\r\nInstalling for dependencies:\r\n fontconfig                   x86_64    2.10.95-11.el7       base        229 k\r\n fontpackages-filesystem      noarch    1.44-8.el7           base        9.9 k\r\n freetype                     x86_64    24.11-15.el7         base        392 k\r\n gd                           x86_64    2.0.35-26.el7        base        146 k\r\n gperftools-libs              x86_64    2.4-8.el7            base        272 k\r\n libX11                       x86_64    2.6.5-1.el7          base        606 k\r\n libX11-common                noarch    1.6.5-1.el7          base        164 k\r\n libXau                       x86_64    1.0.8-2.1.el7        base        29 k\r\n libXpm                       x86_64    3.5.12-1.el7         base        55 k\r\n libjpeg-turbo                x86_64    1.2.90-5.el7         base        134 k\r\n libpng                       x86_64    2:1.5.13-7.el7_2     base        213 k\r\n libunwind                    x86_64    2:1.2-2.el7          base        57 k\r\n libxcb                       x86_64    1.12-1.el7           base        211 k\r\n libxslt                      x86_64    1.1.28-5.el7         base        242 k\r\n lyx-fonts                    noarch    2.2.3-1.el7          epel        159 k\r\n nginx-all-modules            noarch    1:1.12.2-1.el7       epel        16 k\r\n nginx-filesystem             noarch    1:1.12.2-1.el7       epel        17 k\r\n nginx-mod-http-geoip         x86_64    1:1.12.2-1.el7       epel        23 k\r\n nginx-mod-http-image-filter  x86_64    1:1.12.2-1.el7       epel        26 k\r\n nginx-mod-http-perl          x86_64    1:1.12.2-1.el7       epel        35 k\r\n nginx-mod-http-xslt-filter   x86_64    1:1.12.2-1.el7       epel        25 k\r\n nginx-mod-mail               x86_64    1:1.12.2-1.el7       epel        53 k\r\n nginx-mod-stream             x86_64    1:1.12.2-1.el7       epel        76 k\r\nTransaction Summary\r\n============================================\r\nInstall  1 Package (+23 Dependent packages)\r\n\r\nTotal download size: 3.6 M\r\nInstalled size: 11 M\r\nIs this ok [y\/d\/N]: y<\/pre>\n<pre>Dependency Installed:\r\n  fontconfig.x86_64 0:2.10.95-11.el7                  fontpackages-filesystem.noarch 0:1.44-8.el7\r\n  freetype.x86_64 0:2.4.11-15.el7                     gd.x86_64 0:2.0.35-26.el7\r\n  gperftools-libs.x86_64 0:2.4-8.el7                  libX11.x86_64 0:1.6.5-1.el7\r\n  libX11-common.noarch 0:1.6.5-1.el7                  libXau.x86_64 0:1.0.8-2.1.el7\r\n  libXpm.x86_64 0:3.5.12-1.el7                        libjpeg-turbo.x86_64 0:1.2.90-5.el7\r\n  libpng.x86_64 2:1.5.13-7.el7_2                      libunwind.x86_64 2:1.2-2.el7\r\n  libxcb.x86_64 0:1.12-1.el7                          libxslt.x86_64 0:1.1.28-5.el7\r\n  lyx-fonts.noarch 0:2.2.3-1.el7                      nginx-all-modules.noarch 1:1.12.2-1.el7\r\n  nginx-filesystem.noarch 1:1.12.2-1.el7              nginx-mod-http-geoip.x86_64 1:1.12.2-1.el7\r\n  nginx-mod-http-image-filter.x86_64 1:1.12.2-1.el7   nginx-mod-http-perl.x86_64 1:1.12.2-1.el7\r\n  nginx-mod-http-xslt-filter.x86_64 1:1.12.2-1.el7    nginx-mod-mail.x86_64 1:1.12.2-1.el7\r\n  nginx-mod-stream.x86_64 1:1.12.2-1.el7\r\nComplete!\r\n[root@newdelhihosting ~]#\r\n<\/pre>\n<p>Now, Nginx is installed on the server.<\/p>\n<h4><span style=\"text-decoration: underline;\"><strong>Manage Nginx HTTP Server on CentOS 7<\/strong><\/span><\/h4>\n<p><strong>Step 3:<\/strong> Once Nginx web server installed, we can start it first time and enable it to start automatically at system boot.<\/p>\n<pre>[root@newdelhihosting ~]# systemctl start nginx\r\n[root@newdelhihosting ~]# systemctl enable nginx\r\n[root@newdelhihosting ~]# systemctl status nginx\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre>[root@newdelhihosting ~]# systemctl status nginx\r\n\u25cf nginx.service - The nginx HTTP and reverse proxy server\r\nLoaded: loaded (\/usr\/lib\/systemd\/system\/nginx.service; enabled; vendor preset: disabled)\r\nActive: active (running) since Sat 2018-03-16 08:28:09 EET; 10s ago\r\nMain PID: 29730 (nginx)\r\nCGroup: \/system.slice\/nginx.service\r\n\u251c\u250029730 nginx: master process \/usr\/sbin\/nginx\r\n\u251c\u250029731 nginx: worker process\r\n\u2514\u250029732 nginx: worker process\r\n\r\nMar 17 08:28:09 newdelhihosting.co.in systemd[1]: Starting The nginx HTTP and reverse proxy server...\r\nMar 17 08:28:09 newdelhihosting.co.in nginx[29724]: nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\r\nMar 17 08:28:09 newdelhihosting.co.in nginx[29724]: nginx: configuration file \/etc\/nginx\/nginx.conf test is successful\r\nMar 17 08:28:09 newdelhihosting.co.in systemd[1]: Started The nginx HTTP and reverse proxy server.\r\n[root@newdelhihosting ~]#<\/pre>\n<p>&nbsp;<\/p>\n<h4><span style=\"text-decoration: underline;\"><strong>Configure firewalld to allow Nginx and itsTraffic<\/strong><\/span><\/h4>\n<p><strong>Step 4:\u00a0<\/strong>Enable firewalld and start the service.<\/p>\n<pre>[root@newdelhihosting ~]# systemctl enable firewalld\r\n[root@newdelhihosting ~]# systemctl status firewalld<\/pre>\n<p>By default, CentOS 7 built-in firewall is set to block Nginx traffic. To allow web traffic on Nginx, update the system firewall rules to permit inbound packets on HTTP and HTTPS using the commands below.<\/p>\n<pre>[root@newdelhihosting ~]# firewall-cmd --zone=public --permanent --add-service=http\r\n[root@newdelhihosting ~]# firewall-cmd --zone=public --permanent --add-service=https\r\n[root@newdelhihosting ~]# firewall-cmd --reload<\/pre>\n<p>&nbsp;<\/p>\n<h4><span style=\"text-decoration: underline;\"><strong>Test Nginx Server on CentOS 7<\/strong><\/span><\/h4>\n<p><strong>Step 5:\u00a0<\/strong>Now, verify Nginx server by going to the following URL, a default nginx page will be shown.<\/p>\n<pre>http:\/\/domain_name.tld or IP_address<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1038\" src=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx-1.jpg\" alt=\"\" width=\"814\" height=\"461\" srcset=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx-1.jpg 814w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx-1-300x170.jpg 300w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx-1-768x435.jpg 768w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx-1-250x142.jpg 250w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/p>\n<h4><span style=\"text-decoration: underline;\"><strong>Creating Virtual Hosts in Nginx<\/strong><\/span><\/h4>\n<p>A virtual host is a website that is served by Nginx in a single VPS or physical server. However, in the Nginx docs you will find the term &#8220;server blocks&#8221; instead, but they are basically the same thing called by different names.<\/p>\n<p>The first step to set up virtual hosts is to create one or more server blocks in the main configuration file (\/etc\/nginx\/nginx.conf) or inside \/etc\/nginx\/sites-available.<\/p>\n<p>Although the name of the configuration files in this directory(sites-available) can be set to whatever you want, it is an idea to use the name of the domains, and in addition we chose to add the .conf extension to indicate that these are configuration files.<\/p>\n<p>These server blocks can be relatively complex, but in their basic form they consist of the following content:<\/p>\n<p>In <code>\/etc\/nginx\/sites-available\/newdelhosting.com.conf<\/code>:<\/p>\n<ol>\n<li>First, we need to set up our directories where the server blocks will live.<\/li>\n<\/ol>\n<pre>[root@newdelhihosting ~]# mkdir \/etc\/nginx\/sites-available\r\n[root@newdelhihosting ~]# mkdir \/etc\/nginx\/sites-enabled<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Now we need to tell NGINX to use look at those directories for the server blocks. Open the global NGINX configuration file in the text editor of your choice. We will use nano<\/p>\n<pre>[root@newdelhihosting ~]# nano \/etc\/nginx\/nginx.conf<\/pre>\n<p>Now, Add the below lines at end of http section and save the file<\/p>\n<p><code>include \/etc\/nginx\/sites-enabled\/*.conf;<br \/>\nserver_names_hash_bucket_size 64;<\/code><\/p>\n<p>As below<\/p>\n<pre># Load modular configuration files from the \/etc\/nginx\/conf.d directory.\r\n# See http:\/\/nginx.org\/en\/docs\/ngx_core_module.html#include\r\n# for more information.\r\ninclude \/etc\/nginx\/conf.d\/*.conf;\r\ninclude \/etc\/nginx\/sites-enabled\/*.conf;\r\nserver_names_hash_bucket_size 64;\r\n<\/pre>\n<p>Fine. Now, NGINX can recognize the server block.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Configuring the actual NGINX server blocks<\/strong><\/span><\/p>\n<p><strong>Step 3:\u00a0<\/strong>Create a new file specifically for the server block for your site. The line below will do this<\/p>\n<pre>[root@newdelhihosting ~]# \/etc\/nginx\/sites-available\/newdelhihosting.com.conf<\/pre>\n<p>and, add the below lines with your actual domain name<\/p>\n<pre>server {\r\nlisten\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 80;\r\nserver_name\u00a0 newdelhihosting.com www.newdelhihosting.com;\r\nlocation \/ {\r\nroot\u00a0\u00a0 \/var\/www\/newdelhihosting.com\/public_html;\r\nindex\u00a0 index.html index.htm;\r\ntry_files $uri $uri\/ =404;\r\n}\r\nerror_page\u00a0\u00a0 500 502 503 504\u00a0 \/50x.html;\r\nlocation = \/50x.html {\r\nroot\u00a0\u00a0 html;\r\n}\r\n}\r\n<\/pre>\n<p><strong>Step 4:\u00a0<\/strong> Now, we need to create a symbolic link between sites-available and sites-enabled using the below command.<\/p>\n<pre># ln -s \/etc\/nginx\/sites-available\/newdelhihosting.com.conf \/etc\/nginx\/sites-enabled\/newdelhihosting.com.conf<\/pre>\n<p>Next, restart Nginx<\/p>\n<pre>[root@newdelhihosting ~]# nginx -t &amp;&amp; systemctl start nginx<\/pre>\n<pre>[root@newdelhihosting ~]# nginx -t &amp;&amp; systemctl start nginx\r\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\r\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful\r\n[root@newdelhihosting ~]#<\/pre>\n<p>You\u2019re done! Provided your DNS and\/or hosts file is pointed for your domain, you should now be able to go to the domain in a web browser and see the test HTML page we created earlier.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1040\" src=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx1.jpg\" alt=\"\" width=\"749\" height=\"327\" srcset=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx1.jpg 669w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx1-300x131.jpg 300w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2018\/03\/Nginx1-250x109.jpg 250w\" sizes=\"(max-width: 749px) 100vw, 749px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NGINX\u00a0is a free, open-source and powerful HTTP web server and reverse proxy with an event-driven architecture. It is a high performance web server software. It is a much more flexible and lightweight program than Apache HTTP Server. It also works as a reverse proxy, standard mail and TCP\/UDP proxy server, and can additionally be configured&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[24,26],"tags":[],"class_list":["post-1026","post","type-post","status-publish","format-standard","hentry","category-centos","category-web-server"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts\/1026"}],"collection":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1026"}],"version-history":[{"count":6,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts\/1026\/revisions"}],"predecessor-version":[{"id":1043,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts\/1026\/revisions\/1043"}],"wp:attachment":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}