CentOS – Install Nginx And PHP-FPM using yum

centos6

In this article, I’m going to show you how to install Nginx with PHP-FPM via yum on CentOS (actually, steps are similar on another operating sytems). Before starting to install Nginx and PHP-FPM, you must uninstall all previous Apache and PHP related RPMs installed on your system or you’ll have to disable Apache or httpd on your system. Login as root and type the following command

yum remove httpd* php*

1.Enabling Additional Repositories

By default, php-fpm is not available from the official CentOS repositories, but from the Remi RPM repository which itself depends on the EPEL repository; we can enable both repositories as follows:

yum install yum-priorities -y
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

2.Installing Nginx

Type the following command

yum install nginx

If you want to run nginx by default when the system boots, type the following command

chkconfig --level 345 nginx on

To start Nginx for the first time, type the following command

/etc/init.d/nginx start

3.Installing PHP-FPM

Type the following command

yum --enablerepo=remi install php php-fpm

If you want to run php-fpm by default when the system boots, type the following command


chkconfig --level 345 php-fpm on

PHP was installed with only core modules. It’s very likely that additional modules will be desired, such as MySQL, XML, GD, etc. Type the following command


yum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt

To start PHP-FPM for the first time, type the following command


/etc/init.d/php-fpm restart

4.Configure PHP-FPM and Nginx working together

The configuration file for Nginx is located at /etc/nginx/nginx.conf. To edit nginx.conf type the following command


vi /etc/nginx/nginx.conf

Uncomment and edit as follows

...
location / {
	root   /usr/share/nginx/html;
	index  index.html index.htm index.php;
}
...
location ~ \.php$ {
	root           html;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	include        fastcgi_params;
}
...

Restart Nginx to reload new configuration, enter


/etc/init.d/nginx reload

Now create the following PHP file in the document root


vi /usr/share/nginx/html/info.php

Now you can access your first nginx host at http://localhost to see result of php code you put in
5. Nginx Virtual Hosting Configuration
Your sample setup
IP: 192.168.1.1
Domain: yoursite.loc
Hosted at: /home/www/yoursite.loc

Type the following command to create user called www


useradd www

Create necessary directories


mkdir -p /home/www/yoursite.loc/public_html
mkdir -p /home/www/yoursite.loc/log
chown -R www.www /home/www/
chmod 755 /home/www/

Creating virtual host config file


cd /etc/nginx/conf.d/
cp virtual.conf www.conf

Open www.conf, enter


vi /etc/nginx/conf.d/www.conf

Append configuration as follows:

server {
	server_name  yoursite.loc;
	root /home/www/yoursite.loc/public_html;
	access_log /home/www/yoursite.loc/log/yoursite.loc-access.log;
	error_log /home/www/yoursite.loc/log/yoursite.loc-error.log;

	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 $document_root$fastcgi_script_name;
	}
}

You can also check the current status as well as the configuration syntax:


/etc/init.d/nginx configtest

Sample outputs


the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful

Now edit /etc/php-fpm.d/www.conf to change the users who own the php-fpm processes to www, enter


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

Find “group of processes” and edit as follows:


; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = www
; RPM: Keep a group allowed to write in log dir.
group = www

Finally restart nginx


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

Leave a Comment