How to install and configure Nginx in Redhat/CentOS 7

0
904

NGINX (pronounced Engine ex) was released for production in 2004 and is rapidly becoming a popular alternative to the traditional Apache web server suite. It features an event-driven design, which can make better use of today’s computer hardware than Apache’s process-driven design. Because of this, NGINX is often seen as the “faster” alternative to Apache, being able to handle a higher load of concurrent connections while using less resources. There are many comparisons out there between Apache and NGINX; we’ll leave the debate up to the community. But here are a few pointers that outline the key reasons to choose Apache versus NGINX. At the end of the day, the choice of the web server platform is entirely dependent on what you’re doing with the server.

To add the RHEL7 EPEL repository, open terminal and use the following command:

# yum install epel-release

Or To add NGINX yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below:

CentOS:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1

RHEL:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7/x86_64/
gpgcheck=0
enabled=1

Install Nginx using yum,

# yum install nginx

Once installed successfully, then start the service and enable the service to start at boot.

# systemctl start nginx

# systemctl enable nginx

Now you can check your webpage in your browser using your IP/domain. Below is sample test page.

nginx test page

Configuration:

Default configuration folder for nginx in /etc/nginx and nginx.conf is the default configuration file. Configuration file is based on directives and parameters, each directive ends with a semi colon. Here will see directives for worker connection default user name Nginx process should run;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Creating a Virtual Server:

Now will setup a virtual server for nginx, in order to process the HTTP request by Nginx. When Nginx process the request,  it looks for the server directive which is placed in http context. You can add multiple server directives, to define multiple virtual servers.

Default virtual server configuration path is /etc/nginx/conf.d directory, in nginx.conf you can find the port and server name section, which include the default configuration.

include /etc/nginx/conf.d/*.conf;
server {
       listen       80 default_server;
       listen       [::]:80 default_server;
       server_name _;
       root         /usr/share/nginx/html;
       location / {
     }
}

You can find default directive location a root, where server looks for the static file when the requests came to your website/web server.

Creating Virtual host;

Here steps to create virtual host, configuration we can refer from nginx.conf file and here is sample configuration domain nginx.foxutech.com,

# vi /etc/nginx/conf.d/foxutech.conf
server {
     listen       80;
     server_name nginx.foxutech.com;
     location / {
     root   /usr/share/nginx/html/foxutech;
     index index.html index.htm;
     }
}

Create the root directory to upload a file to it.

# mkdir /usr/share/nginx/html/foxutech

Create Index.html page.

# echo “This is Foxutech test page” > /usr/share/nginx/html/foxutech/index.html

Restart the Nginx service to replect change to display.

# systemctl restart nginx.service

Test with browser, url will be http://nginx.foxutech.com.

Tips:  If you want to run ONE site at lightning speed on an advanced configuration, NGINX is probably the server for you. If you want to run MANY sites with easy configuration and flexibility, Apache is still better option.

NO COMMENTS