{"id":1078,"date":"2013-03-28T20:09:04","date_gmt":"2013-03-28T20:09:04","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=584"},"modified":"2013-03-28T20:09:04","modified_gmt":"2013-03-28T20:09:04","slug":"centos-install-nginx-and-php-fpm-using-yum","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/centos-install-nginx-and-php-fpm-using-yum\/","title":{"rendered":"CentOS – Install Nginx And PHP-FPM using yum"},"content":{"rendered":"
<\/p>\n
In this article, I\u2019m 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<\/p>\n
<\/p>\n
\nyum remove httpd* php*\n<\/pre>\n1.Enabling Additional Repositories<\/strong><\/p>\n
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:<\/p>\n
\nyum install yum-priorities -y\nrpm -Uvh http:\/\/rpms.famillecollet.com\/enterprise\/remi-release-6.rpm\nrpm -Uvh http:\/\/download.fedoraproject.org\/pub\/epel\/6\/i386\/epel-release-6-8.noarch.rpm\n<\/pre>\n2.Installing Nginx<\/strong><\/p>\n
Type the following command<\/p>\n
\nyum install nginx\n<\/pre>\nIf you want to run nginx by default when the system boots, type the following command<\/p>\n
\nchkconfig --level 345 nginx on\n<\/pre>\nTo start Nginx for the first time, type the following command<\/p>\n
\n\/etc\/init.d\/nginx start\n<\/pre>\n3.Installing PHP-FPM<\/strong><\/p>\n
Type the following command<\/p>\n
\nyum --enablerepo=remi install php php-fpm\n<\/pre>\nIf you want to run php-fpm by default when the system boots, type the following command<\/p>\n
\n\nchkconfig --level 345 php-fpm on\n\n<\/pre>\nPHP was installed with only core modules. It\u2019s very likely that additional modules will be desired, such as MySQL, XML, GD, etc. Type the following command<\/p>\n
\n\nyum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt\n\n<\/pre>\nTo start PHP-FPM for the first time, type the following command<\/p>\n
\n\n\/etc\/init.d\/php-fpm restart\n\n<\/pre>\n4.Configure PHP-FPM and Nginx working together<\/strong><\/p>\n
The configuration file for Nginx is located at \/etc\/nginx\/nginx.conf. To edit nginx.conf type the following command<\/p>\n
\n\nvi \/etc\/nginx\/nginx.conf\n\n<\/pre>\nUncomment and edit as follows<\/p>\n
\n...\nlocation \/ {\n\troot \/usr\/share\/nginx\/html;\n\tindex index.html index.htm index.php;\n}\n...\nlocation ~ \\.php$ {\n\troot html;\n\tfastcgi_pass 127.0.0.1:9000;\n\tfastcgi_index index.php;\n\tfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n\tinclude fastcgi_params;\n}\n...\n<\/pre>\nRestart Nginx to reload new configuration, enter<\/p>\n
\n\n\/etc\/init.d\/nginx reload\n\n<\/pre>\nNow create the following PHP file in the document root<\/p>\n
\n\nvi \/usr\/share\/nginx\/html\/info.php\n\n<\/pre>\nNow you can access your first nginx host at http:\/\/localhost to see result of php code you put in
\n5. Nginx Virtual Hosting Configuration<\/strong>
\nYour sample setup
\nIP: 192.168.1.1
\nDomain: yoursite.loc
\nHosted at: \/home\/www\/yoursite.loc<\/p>\nType the following command to create user called www<\/p>\n
\n\nuseradd www\n\n<\/pre>\nCreate necessary directories<\/p>\n
\n\nmkdir -p \/home\/www\/yoursite.loc\/public_html\nmkdir -p \/home\/www\/yoursite.loc\/log\nchown -R www.www \/home\/www\/\nchmod 755 \/home\/www\/\n\n<\/pre>\nCreating virtual host config file<\/p>\n
\n\ncd \/etc\/nginx\/conf.d\/\ncp virtual.conf www.conf\n\n<\/pre>\nOpen www.conf, enter<\/p>\n
\n\nvi \/etc\/nginx\/conf.d\/www.conf\n\n<\/pre>\nAppend configuration as follows:<\/p>\n
\nserver {\n\tserver_name yoursite.loc;\n\troot \/home\/www\/yoursite.loc\/public_html;\n\taccess_log \/home\/www\/yoursite.loc\/log\/yoursite.loc-access.log;\n\terror_log \/home\/www\/yoursite.loc\/log\/yoursite.loc-error.log;\n\n\tlocation \/ {\n\t\tindex index.html index.htm index.php;\n\t}\n\tlocation ~ \\.php$ {\n\t\tinclude \/etc\/nginx\/fastcgi_params;\n\t\tfastcgi_pass 127.0.0.1:9000;\n\t\tfastcgi_index index.php;\n\t\tfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n\t}\n}\n<\/pre>\nYou can also check the current status as well as the configuration syntax:<\/p>\n
\n\n\/etc\/init.d\/nginx configtest\n\n<\/pre>\nSample outputs<\/p>\n
\n\nthe configuration file \/etc\/nginx\/nginx.conf syntax is ok\nconfiguration file \/etc\/nginx\/nginx.conf test is successful\n\n<\/pre>\nNow edit \/etc\/php-fpm.d\/www.conf to change the users who own the php-fpm processes to www, enter<\/p>\n
\n\nvi \/etc\/php-fpm.d\/www.conf\n\n<\/pre>\nFind \u201cgroup of processes\u201d and edit as follows:<\/p>\n
\n\n; Unix user\/group of processes\n; Note: The user is mandatory. If the group is not set, the default user's group\n;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 will be used.\n; RPM: apache Choosed to be able to access some dir as httpd\nuser = www\n; RPM: Keep a group allowed to write in log dir.\ngroup = www\n\n<\/pre>\nFinally restart nginx<\/p>\n
\n\n\/etc\/init.d\/nginx restart\n\/etc\/init.d\/php-fpm restart\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"In this article, I\u2019m 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…<\/p>\n