{"id":276,"date":"2015-09-07T16:15:21","date_gmt":"2015-09-07T16:15:21","guid":{"rendered":"http:\/\/onlinelab.info\/?p=276"},"modified":"2015-09-07T16:15:21","modified_gmt":"2015-09-07T16:15:21","slug":"install-nginx-on-centos-7-rhel-7","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/09\/07\/install-nginx-on-centos-7-rhel-7\/","title":{"rendered":"Install Nginx on CentOS 7 \/ RHEL 7"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-7221\" src=\"http:\/\/www.itzgeek.com\/wp-content\/uploads\/2014\/07\/NGINX.jpg\" alt=\"NGINX\" width=\"100\" height=\"80\" title=\"\"><\/p>\n<p><a title=\"Nginx home page\" href=\"http:\/\/nginx.org\/\" target=\"_blank\" rel=\"noopener\">Nginx<\/a> is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer. This article will look at installing Nginx on a CentOS using the \u2018yum\u2019 package manager.<\/p>\n<h3><strong>Install Nginx:<\/strong><\/h3>\n<p>To install Nginx using yum we need to include the Nginx repository, install the Nginx repository.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">rpm -Uvh http:\/\/nginx.org\/packages\/centos\/7\/noarch\/RPMS\/nginx-release-centos-7-0.el7.ngx.noarch.rpm<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Install the Nginx by issuing the following command.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">yum install nginx<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3><strong>Starting Nginx:<\/strong><\/h3>\n<p>Once installed, start the Nginix by using the following command.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">systemctl start nginx.service<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Once started. Navigate the web browser to Nginx server(IP Address or Host Name). The following image shows the default Welcome Screen of Nginx.<\/p>\n<figure id=\"attachment_7225\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7225\" src=\"http:\/\/www.itzgeek.com\/wp-content\/uploads\/2014\/07\/CentOS-7-Nginx-Default-Page.jpg\" alt=\"CentOS 7 - Nginx Default Page\" width=\"640\" height=\"471\" title=\"\"><figcaption class=\"wp-caption-text\">CentOS 7 \u2013 Nginx Default Page<\/figcaption><\/figure>\n<h3><strong>Extras:<\/strong><\/h3>\n<p>Init scripts will help you to start and stop the service. The init scripts for the Nginx is installed while installing Nginx. However, we need to set up Nginx to start automatically if the server is rebooted:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">systemctl enable nginx.<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3><strong>Configuration File:<\/strong><\/h3>\n<p>The name of the default configuration file is nginx.conf, located in <strong>\/etc\/nginx<\/strong> directory. Configuration file is based on directives and parameters, each directive ends with a semi colon. The following shows the directives for worker connection and in which user name Nginx process should run.<\/p>\n<div class=\"command\">user\u00a0 nginx;<br \/>\nworker_processes\u00a0 1;<br \/>\nevents {<br \/>\nworker_connections\u00a0 1024;<br \/>\n}<\/div>\n<h3><strong>Creating a Virtual Server:<\/strong><\/h3>\n<p>We must setup at least one virtual server for Nginx, in order to process the HTTP request by Nginx. When Nginx process the request,\u00a0 it looks for the server directive which is placed in http context. You can add multiple server directives, to define multiple virtual servers.<\/p>\n<p>Default virtual server config can be found under <strong>\/etc\/nginx\/conf.d<\/strong> directory, if you open and see that; first line itself a virtual server for localhost and listening on port 80.<\/p>\n<div class=\"command\">server {<br \/>\nlisten\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 80;<br \/>\nserver_name\u00a0 localhost;<br \/>\n}<\/div>\n<p>You will find the location directive, which will tell the server to look for the static file when the requests comes for the localhost.<\/p>\n<div class=\"command\">location \/ {<br \/>\nroot\u00a0\u00a0 \/usr\/share\/nginx\/html;<br \/>\nindex\u00a0 index.html index.htm;<br \/>\n}<\/div>\n<p>Additionally you can mention the error pages.<\/p>\n<div class=\"command\">error_page\u00a0\u00a0 500 502 503 504\u00a0 \/50x.html;<br \/>\nlocation = \/50x.html {<br \/>\nroot\u00a0\u00a0 \/usr\/share\/nginx\/html;<br \/>\n}<\/div>\n<p>The above is the minimum configuration for a virtual server, you can find the full configuration <a href=\"http:\/\/nginx.com\/resources\/admin-guide\/web-server\/\" target=\"_blank\" rel=\"nofollow noopener\">here<\/a>.<\/p>\n<h3><strong>Example:<\/strong><\/h3>\n<p>The following virtual server will accept the request for server.itzgeek.com, create a configuration file\u00a0 in<strong>\/etc\/nginx\/conf.d<\/strong> or copy the default configuration file.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">cp \/etc\/nginx\/conf.d\/default.conf \/etc\/nginx\/conf.d\/itzgeek.conf<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Edit in such a way that, it receives the request. Minimal configuration will look like below.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">vi \/etc\/nginx\/conf.d\/itzgeek.conf<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"command\">server {<br \/>\nlisten\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 80;<br \/>\nserver_name\u00a0 server.itzgeek.com;<br \/>\nlocation \/ {<br \/>\nroot\u00a0\u00a0 \/usr\/share\/nginx\/html\/itzgeek;<br \/>\nindex\u00a0 index.html index.htm;<br \/>\n}<br \/>\n}<\/div>\n<p>Create root directory.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\">mkdir \/usr\/share\/nginx\/html\/itzgeek<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Create Index.html page.<\/p>\n<div class=\"command\">echo \u201cThis is ITzGeek Home\u201d &gt; \/usr\/share\/nginx\/html\/itzgeek\/index.html<\/div>\n<p>Restart the Nginx service.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"dos\"> systemctl restart nginx.service<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Test with browser, url will be <strong>http:\/\/server.itzgeek.com<\/strong><\/p>\n<figure id=\"attachment_7224\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-7224\" src=\"http:\/\/www.itzgeek.com\/wp-content\/uploads\/2014\/07\/CentOS-7-Nginx-Virtual-Server.jpg\" alt=\"CentOS 7 - Nginx Virtual Server\" width=\"640\" height=\"475\" title=\"\"><figcaption class=\"wp-caption-text\">CentOS 7 \u2013 Nginx Virtual Server<\/figcaption><\/figure>\n<p>That\u2019s all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer. This article will look at installing Nginx on a CentOS using the \u2018yum\u2019 package&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-monitoring"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}