Posts

How to Install PHP in nginx

Prerequisites

Install nginx web server

Step 1: Install PHP & PHP-FPM

Install packages php & php-fpm package on server using epel repository.

# yum --enablerepo=epel -y install php php-mbstring php-pear php-fpm

Step 2: Configure PHP-FPM

Modify user and group name in php-fpm configuration file.

# vi /etc/php-fpm.d/www.conf

user = nginx
group = nginx

Step 3: Start Service

Start php-fpm service.

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

Starting php-fpm: [ OK ]
# chkconfig php-fpm on

Step 4: Configure default.conf file

Modify default.conf file.

# vi /etc/nginx/conf.d/default.conf
    # add follows in a "server" section

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;
    }

Step 5: Restart Service

Restart nginx server to execute the latest configurations.

# /etc/rc.d/init.d/nginx restart

Stopping nginx: [ OK ]
Starting nginx: [ OK ]

Step 6: Create a PHP Page

Create a PHP test page and save the file.

# echo "" > /usr/share/nginx/html/info.php

Open Your favorite browser and hit the URL: http://Your-IP-Address/info.php
phpinfo

How to Configure Virtual Host in NGINX Web Server

Step 1: Prerequisite

Step 2: Add Domains in nginx Configuration File

To add multiple domains in nginx web server edit /usr/share/nginx/virtual.host file and create virtual hosts.

# vi /etc/nginx/conf.d/virtual.conf
# add at the last line

server {
    listen       80;
    server_name  www.a.com;

    location / {
        root   /usr/share/nginx/a.com;
        index  index.html index.htm;
    }
}

Step 3: Restart nginx server

# mkdir /usr/share/nginx/a.com
# /etc/rc.d/init.d/nginx restart

Stopping nginx: [ OK ]
Starting nginx: [ OK ]

Step 3: Create a Web page

# vi /usr/share/nginx/a.com/index.html

<html>
<body>
<div style="width: 100%; font-weight: bold; text-align: center;">
Nginx Test Page
</div>
</body>
</html>

How to install Nginx on RHEL/CentOS

Step 1: Install Nginx

Nginx is well known web server. Download and install nginx repos:

For RedHat

# wget http://nginx.org/packages/rhel/6/noarch/RPMS/nginx-release-rhel-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-rhel-6-0.el6.ngx.noarch.rpm

For CentOS

# wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
# yum --enablerepo=epel -y install nginx

Step 2: Configure Nginx

Edit the nginx.conf file to configure Nginx.

# vi /etc/nginx/nginx.conf
worker_processes 2
gzip on;
# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.org
# egrep -v "^ *#|^ *$" /etc/nginx/conf.d/default.conf.org > /etc/nginx/conf.d/default.conf

Edit default.conf file and enter the server name

# vi /etc/nginx/conf.d/default.conf
server_name www.example-server

Step 3: Start nginx Server

After configuration has made, restart nginx service.

# service nginx start 
OR
#/etc/rc.d/init.d/nginx start
Starting nginx: [ OK ]
# chkconfig nginx on

Step 4: Open Page on Browser

Hit the Server IP on your favorite browser, a default page of nginx appears:

nginx-homepage