{"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":"

\"centos6\"<\/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>\n

1.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>\n

2.Installing Nginx<\/strong><\/p>\n

Type the following command<\/p>\n

\nyum install nginx\n<\/pre>\n

If you want to run nginx by default when the system boots, type the following command<\/p>\n

\nchkconfig --level 345 nginx on\n<\/pre>\n

To start Nginx for the first time, type the following command<\/p>\n

\n\/etc\/init.d\/nginx start\n<\/pre>\n

3.Installing PHP-FPM<\/strong><\/p>\n

Type the following command<\/p>\n

\nyum --enablerepo=remi install php php-fpm\n<\/pre>\n

If 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>\n

PHP 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>\n

To start PHP-FPM for the first time, type the following command<\/p>\n

\n\n\/etc\/init.d\/php-fpm restart\n\n<\/pre>\n

4.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>\n

Uncomment 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>\n

Restart Nginx to reload new configuration, enter<\/p>\n

\n\n\/etc\/init.d\/nginx reload\n\n<\/pre>\n

Now create the following PHP file in the document root<\/p>\n

\n\nvi \/usr\/share\/nginx\/html\/info.php\n\n<\/pre>\n

Now 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>\n

Type the following command to create user called www<\/p>\n

\n\nuseradd www\n\n<\/pre>\n

Create 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>\n

Creating virtual host config file<\/p>\n

\n\ncd \/etc\/nginx\/conf.d\/\ncp virtual.conf www.conf\n\n<\/pre>\n

Open www.conf, enter<\/p>\n

\n\nvi \/etc\/nginx\/conf.d\/www.conf\n\n<\/pre>\n

Append 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>\n

You can also check the current status as well as the configuration syntax:<\/p>\n

\n\n\/etc\/init.d\/nginx configtest\n\n<\/pre>\n

Sample 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>\n

Now 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>\n

Find \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>\n

Finally 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

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,66],"tags":[49,55,64],"aioseo_notices":[],"views":5,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/1078"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=1078"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/1078\/revisions"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=1078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=1078"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=1078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}