How to Configure NGINX in CentOS/RHEL

NGINX Plus is a high performance web server and proxy server. Nginx doesn’t relay on threads to handle requests. The Nginx also read data from disk and load it into the cache and expire it.

PHP-FPM is a php module. It’s known as FastCGI Process Manager. The PHP-FPM is usually used to allow Nginx to process PHP files.

nginx_opt

Step 1: First install below repository on server.

CentOS/RHEL 6, 32 Bit (i686):
# rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm

CentOS/RHEL 6, 32 Bit (i386):
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

CentOS/RHEL 6, 64 Bit (x86_64):
# rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

CentOS/RHEL 6, 64 Bit x86_64):
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Step 2: Use following command to install Nginx and php-fpm packages:

# yum install nginx –enablerepo=remi
# yum install php-fpm –enablerepo=remi

Step 3: After install the ngnix and php-fpm packages need to change user and group using php-fpm configuration file:

# vim /etc/php-fpm.d/www.conf
Default entry in configuration file:
User = apache
Group = apache
Change entry as below:
User = nginx
Group = nginx

Step 4: Nginx provides default virtual host file, which we can use as a template to create new virtual host files for other domains.

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/techoism.com.conf

Copy below code and save the file:

server {
        listen 80;
        server_name techoism.com;
        root   /usr/local/apache2;
        index index.php index.html;
        location / {
        	     try_files $uri $uri/ @handler;
       		    }
        location @handler {
           	fastcgi_pass 127.0.0.1:9000;
            	fastcgi_param SCRIPT_FILENAME /usr/local/apache2/index.php;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_NAME /index.php;
        	          }
   	location ~ .php$ {
               	try_files $uri @handler;
               	fastcgi_pass    127.0.0.1:9000;
               	fastcgi_index   index.php;
                fastcgi_param SCRIPT_FILENAME /usr/local/apache2$fastcgi_script_name;
                include fastcgi_params;
        		}
}

Step 5: After creating virtual host configuration file we need to start nginx and php-fpm service using following command.

# /etc/init.d/nginx start
# /etc/init.d/php-fpm start

and also enable auto start on system reboot.

# chkconfig ngnix on
# chkconfig php-fpm on

Some other information related with Nginx:

  • Default document Root of Nginx webserver is below:
  • /usr/share/nginx/html
    
  • Nginx configuration file:
  • /etc/nginx/nginx.conf
    
  • Php-fpm configuration file:
  • /etc/php-fpm.conf
    /etc/php-fpm.d/www.conf
    
  • Default nginx virtual host file:
  • /etc/nginx/conf.d/default.conf
    
  • Default nginx access and error log file:
  • /var/log/nginx/access.log
    /var/log/nginx/error.log
    

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    The reCAPTCHA verification period has expired. Please reload the page.