How to install Nginx, PHP 5.3.10 and PHP-FPM on CentOS 5.7 — 6.2

Getting started

Add necessary repositories:

CentOS 5.x i386:

sudo rpm -ivh http://mirror.yandex.ru/epel/5/i386/epel-release-5-4.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

CentOS 5.x x86_64:
sudo rpm -ivh http://mirror.yandex.ru/epel/5/x86_64/epel-release-5-4.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

CentOS 6.x i386:
sudo rpm -ivh http://mirror.yandex.ru/epel/6/i386/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

CentOS 6.x x86_64:
sudo rpm -ivh http://mirror.yandex.ru/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Create /etc/yum.repos.d/nginx.repo file and add:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Save the file.

Installing Nginx, PHP 5.3.10 and PHP-FPM

To install Nginx, PHP 5.3.10 and PHP-FPM run:

yum install nginx php php-fpm php-common

Install some PHP modules: PEAR (php-pear), PDO (php-pdo), MySQL (php-mysql), Memcache (php-pecl-memcache), Memcached (php-pecl-memcached), GD (php-gd), XML (php-xml), MBString (php-mbstring), MCrypt (php-mcrypt)

yum --enablerepo=remi install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Enable Nginx and PHP-FPM. You can do it this way:

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

or this way:

service nginx start
service php-fpm start

To start Nginx on boot:

chkconfig --add nginx
chkconfig --levels 235 nginx on

To start PHP-FPM on boot:

chkconfig --add php-fpm
chkconfig --levels 235 php-fpm on

Configuring Nginx

Creating folders for our website and logs:

mkdir -p /home/web/www/howtounix.info/public_html
mkdir /home/web/www/howtounix.info/logs
chown -R nginx:nginx /home/web/www/howtounix.info

Create folders for virtual hosts:

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Add to /etc/nginx/nginx.conf file after "include /etc/nginx/conf.d/*.conf":

include /etc/nginx/sites-enabled/*;

Add virtual host. In /etc/nginx/sites-available/ directory create the file named as your website. In our case it's howtounix.info, add:

server {
    server_name howtounix.info;
    access_log /home/web/www/howtounix.info/logs/access.log;
    error_log /home/web/www/howtounix.info/logs/error.log;
    root /home/web/www/howtounix.info/public_html;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/web/www/howtounix.info/public_html$fastcgi_script_name;
    }
}

Create a symlink to the virtual host and restart Nginx:

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/howtounix.info

service nginx restart

Add the virtual host to /etc/host file:

127.0.0.1               localhost howtounix.info
It will be the same pattern for an external IP address.

Check on Nginx and PHP-FPM. Create index.php file in /home/web/www/howtounix.info/public_html/ directory and add:

<?php 
    phpinfo();
?>

Navigate your website in a browser to check how it works.






  1. Mike
    2012-09-19 10:19:10
    In this example...
    
    mkdir /home/web/howtounix.info/logs
    must be...
    mkdir /home/web/www/howtounix.info/logs
    
    and
    
    include /etc/nginx/sites-enabled/*;
    must be...
    include /etc/nginx/websites-enabled/*;
    
  1. unixowl
    2012-09-19 11:57:11
    Thanks for finding it out! Fixed.
  1. Bg
    2012-12-31 06:12:28
    Hello!
    
    I am configuring my VPS with these same settings -- my Drupal 7 sites is taking a lot of CPU's like 69% of 2.4GHz ..
    
    Do you have any suggestion?
    
    Thanks a lot
  1. donny
    2012-12-31 14:54:23
    hi, php-fpm & remi did not found. how to install them?
  1. Sergey
    2012-12-31 15:44:03
    donny, what version of CentOS do you have?
  1. Sergey
    2012-12-31 15:54:57
  1. vndeveloper
    2014-04-07 16:14:33
    The first command misses "--enablerepo=remi" option. It should be:
    
    yum --enablerepo=remi install nginx php php-fpm php-common

Got a comment?

captcha =

Categories

  1. System (20)
    1. FreeBSD (5)
    2. Linux (9)
  2. Email (2)
  3. DNS (2)
  4. Databases (1)
  5. WebServer (27)
 
Copyright © 2012-2015 HowToUnix - *nix Howtos and Tutorials
All Rights Reserved.