{"id":4054,"date":"2012-10-11T21:04:30","date_gmt":"2012-10-11T13:04:30","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=4054"},"modified":"2023-04-28T09:48:58","modified_gmt":"2023-04-28T09:48:58","slug":"how-to-setup-nginx-with-php-fastcgi-on-centos-6-2centos-6-3-vps-server","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-setup-nginx-with-php-fastcgi-on-centos-6-2centos-6-3-vps-server\/","title":{"rendered":"How to Setup Nginx With PHP-FastCGI on CentOS 6.2\/CentOS 6.3 VPS Server"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2012\/04\/nginx-150x150.png\" alt=\"\" title=\"nginx\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-2750 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2012\/04\/nginx-150x150.png 150w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2012\/04\/nginx-100x100.png 100w\" data-sizes=\"(max-width: 150px) 100vw, 150px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 150px; --smush-placeholder-aspect-ratio: 150\/150;\" \/>At the previous post, i have setup Nginx as reverse proxy to apache web server. This post will covers the steps how to setup nginx web server to use PHP-FastCGI for dynamic content. Nginx (pronounced \u201cEngine-X\u201d) is a free, open-source HTTP Web server and one of the alternative to Apache http server. It\u2019s a high performance edge web server with the lowest memory footprint and the key features to build modern and efficient web infrastructure. FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs. FastCGI provides better scalability and performance.<\/p>\n<p>1. Prepared yum repository for nginx :<\/p>\n<pre>\n[root@centos63 ~]# vi \/etc\/yum.repos.d\/nginx.repo\n<\/pre>\n<pre>\n[nginx]\nname=nginx repo\nbaseurl=http:\/\/nginx.org\/packages\/centos\/$releasever\/$basearch\/\ngpgcheck=0\nenabled=1\n<\/pre>\n<p>2. Install Nginx :<\/p>\n<pre>\n[root@centos63 ~]# yum install nginx -y\n<\/pre>\n<p>3. Install required php packages :<\/p>\n<pre>\n[root@centos63 ~]# yum install php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy -y\n<\/pre>\n<p>Examples of Software Installed and the Dependency:<\/p>\n<pre>\nInstalled:\n  php-eaccelerator.i686 1:0.9.6.1-1.el6   php-imap.i686 0:5.3.3-14.el6_3      php-ldap.i686 0:5.3.3-14.el6_3\n  php-magickwand.i686 0:1.0.9-1.el6       php-magpierss.noarch 0:0.72-6.el6   php-mssql.i686 0:5.3.3-1.el6\n  php-odbc.i686 0:5.3.3-14.el6_3          php-pear.noarch 1:1.9.4-4.el6       php-shout.i686 0:0.9.2-6.el6\n  php-snmp.i686 0:5.3.3-14.el6_3          php-soap.i686 0:5.3.3-14.el6_3      php-tidy.i686 0:5.3.3-14.el6_3\n  php-xml.i686 0:5.3.3-14.el6_3           php-xmlrpc.i686 0:5.3.3-14.el6_3\n\nDependency Installed:\n  ConsoleKit.i686 0:0.4.1-3.el6                        ConsoleKit-libs.i686 0:0.4.1-3.el6\n  GConf2.i686 0:2.28.0-6.el6                           ImageMagick.i686 0:6.5.4.7-6.el6_2\n  ORBit2.i686 0:2.14.17-3.1.el6                        atk.i686 0:1.28.0-2.el6\n  avahi-libs.i686 0:0.6.25-11.el6                      cairo.i686 0:1.8.8-3.1.el6\n  cups-libs.i686 1:1.4.2-48.el6_3.1                    dbus.i686 1:1.2.24-7.el6_3\n  eggdbus.i686 0:0.6-3.el6                             fontconfig.i686 0:2.8.0-3.el6\n  freetds.i686 0:0.91-2.el6                            ghostscript.i686 0:8.70-14.el6_3.1\n  ghostscript-fonts.noarch 0:5.50-23.1.el6             gtk2.i686 0:2.18.9-10.el6\n  hicolor-icon-theme.noarch 0:0.11-1.1.el6             jasper-libs.i686 0:1.900.1-15.el6_1.1\n  lcms-libs.i686 0:1.19-1.el6                          libICE.i686 0:1.0.6-1.el6\n  libIDL.i686 0:0.8.13-2.1.el6                         libSM.i686 0:1.1.0-7.1.el6\n  libXcomposite.i686 0:0.4.1-2.el6                     libXcursor.i686 0:1.1.10-2.el6\n  libXdamage.i686 0:1.1.2-1.el6                        libXext.i686 0:1.1-3.el6\n  libXfixes.i686 0:4.0.4-1.el6                         libXfont.i686 0:1.4.1-2.el6_1\n  libXft.i686 0:2.1.13-4.1.el6                         libXi.i686 0:1.3-3.el6\n  libXinerama.i686 0:1.1-1.el6                         libXrandr.i686 0:1.3.0-4.el6\n  libXrender.i686 0:0.9.5-1.el6                        libXt.i686 0:1.0.7-1.el6\n  libc-client.i686 0:2007e-11.el6                      libcroco.i686 0:0.6.2-5.el6\n  libfontenc.i686 0:1.0.5-2.el6                        libgsf.i686 0:1.14.15-5.el6\n  libogg.i686 2:1.1.4-2.1.el6                          librsvg2.i686 0:2.26.0-5.el6_1.1.0.1.centos\n  libshout.i686 0:2.2.2-5.1.el6                        libthai.i686 0:0.1.12-3.el6\n  libtheora.i686 1:1.1.0-2.el6                         libtidy.i686 0:0.99.0-19.20070615.1.el6\n  libtiff.i686 0:3.9.4-6.el6_3                         libvorbis.i686 1:1.2.3-4.el6_2.1\n  libwmf-lite.i686 0:0.2.8.4-22.el6.centos             lm_sensors-libs.i686 0:3.1.1-10.el6\n  net-snmp.i686 1:5.5-41.el6_3.1                       net-snmp-libs.i686 1:5.5-41.el6_3.1\n  pango.i686 0:1.28.1-3.el6_0.5.1.centos               pixman.i686 0:0.18.4-1.el6_0.1\n  polkit.i686 0:0.96-2.el6_0.1                         sgml-common.noarch 0:0.6.3-32.el6\n  speex.i686 0:1.2-0.12.rc1.1.el6                      unixODBC.i686 0:2.2.14-11.el6\n  urw-fonts.noarch 0:2.4-10.el6                        xorg-x11-font-utils.i686 1:7.2-11.el6\n\nDependency Updated:\n  dbus-libs.i686 1:1.2.24-7.el6_3\n\nComplete!\n<\/pre>\n<p>4. <a href=\"https:\/\/webhostinggeeks.com\/howto\/how-to-configure-epel-repository-on-centos-6-3\/\">Configure EPEL Repository on CentOS 6.3<\/a> and install spawn-fcgi :<\/p>\n<pre>\n[root@centos63 ~]# yum install spawn-fcgi -y\n<\/pre>\n<p>5. Start configure nginx. It is better to backup original nginx config file :<\/p>\n<pre>\n[root@centos63 ~]# cp \/etc\/nginx\/nginx.conf \/etc\/nginx\/nginx.conf.bak\n<\/pre>\n<p>6. Create nginx DocumentRoot for ehowstuff.local virtual host :<\/p>\n<pre>\n[root@centos63 ~]# mkdir -p \/var\/www\/html\/ehowstuff.local\n[root@centos63 ~]# mkdir -p \/var\/www\/html\/ehowstuff.local\/public_html\n[root@centos63 ~]# chown -R nginx:nginx \/var\/www\/html\/ehowstuff.local\/public_html\n<\/pre>\n<p>7. Create folder where to store access.log and error.log :<\/p>\n<pre>\n[root@centos63 ~]# mkdir -p \/var\/log\/nginx\/ehowstuff.local\n[root@centos63 ~]# chown -R nginx:nginx \/var\/log\/nginx\/ehowstuff.local\n<\/pre>\n<p>8. Modify default nginx config file :<\/p>\n<pre>\n[root@centos63 ~]# vi \/etc\/nginx\/nginx.conf\n<\/pre>\n<pre>\n\nuser  nginx;\nworker_processes  1;\n\nerror_log  \/var\/log\/nginx\/error.log warn;\npid        \/var\/run\/nginx.pid;\n\n\nevents {\n    worker_connections  1024;\n}\n\n\nhttp {\n    include       \/etc\/nginx\/mime.types;\n    default_type  application\/octet-stream;\n\n    log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n                      '$status $body_bytes_sent \"$http_referer\" '\n                      '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n\n    access_log  \/var\/log\/nginx\/access.log  main;\n\n    sendfile        on;\n    #tcp_nopush     on;\n\n    keepalive_timeout  65;\n\n    #gzip  on;\n\n    include \/etc\/nginx\/conf.d\/*.conf;\n}\n\n\n<\/pre>\n<p>9. Add the host configuration file under \/etc\/nginx\/conf.d\/. Nginx config file will load all *.conf files under conf.d folder :<\/p>\n<p>As example, the website domain is ehowstuff.local. So virtual server ehowstuff.local will be created and named as ehowstuff.local.conf :<\/p>\n<pre>\n[root@centos63 ~]# vi \/etc\/nginx\/conf.d\/ehowstuff.local.conf\n<\/pre>\n<pre>\nserver {\n    server_name www.ehowstuff.local ehowstuff.local;\n    access_log \/var\/log\/nginx\/ehowstuff.local\/access.log;\n    error_log \/var\/log\/nginx\/ehowstuff.local\/error.log;\n    root \/var\/www\/html\/ehowstuff.local\/public_html;\n\n    location \/ {\n        index index.html index.htm index.php;\n    }\n\n    location ~ \\.php$ {\n        include \/etc\/nginx\/fastcgi_params;\n        fastcgi_pass  127.0.0.1:9000;\n        fastcgi_index index.php;\n        fastcgi_param SCRIPT_FILENAME \/var\/www\/html\/ehowstuff.local\/public_html$fastcgi_script_name;\n    }\n}\n<\/pre>\n<p>10. Check your nginx version :<\/p>\n<pre>\n[root@centos63 ~]# nginx -v\nnginx version: nginx\/1.2.4\n<\/pre>\n<p>11. Configure php-fastcgi :<\/p>\n<pre>\n[root@centos63 ~]# cd \/opt\n[root@centos63 opt]# wget -O php-fastcgi-rpm.sh http:\/\/library.linode.com\/assets\/696-php-fastcgi-rpm.sh\n[root@centos63 opt]# mv php-fastcgi-rpm.sh \/usr\/bin\/php-fastcgi\n[root@centos63 opt]# chmod +x \/usr\/bin\/php-fastcgi\n<\/pre>\n<pre>\n[root@centos63 opt]# wget -O php-fastcgi-init-rpm.sh http:\/\/library.linode.com\/assets\/697-php-fastcgi-init-rpm.sh\n[root@centos63 opt]# mv php-fastcgi-init-rpm.sh \/etc\/rc.d\/init.d\/php-fastcgi\n[root@centos63 opt]# chmod +x \/etc\/rc.d\/init.d\/php-fastcgi\n<\/pre>\n<p>12. Start php-fastcgi to control spawn-fcgi :<\/p>\n<pre>\n[root@centos63 ~]# \/etc\/init.d\/php-fastcgi start\nStarting php-cgi: spawn-fcgi: child spawned successfully: PID: 3380\n                                                           [  OK  ]\n<\/pre>\n<p>13. Start Nginx service :<\/p>\n<pre>\n[root@centos63 ~]# service nginx start\n<\/pre>\n<p>14. Make Nginx and php-fastcgi start at boot :<\/p>\n<pre>\n[root@centos63 opt]# chkconfig --add nginx\n[root@centos63 opt]# chkconfig nginx on\n[root@centos63 opt]# chkconfig --add php-fastcgi\n[root@centos63 opt]# chkconfig php-fastcgi on\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>At the previous post, i have setup Nginx as reverse proxy to apache web server. This post will covers the steps how to setup nginx web server to use PHP-FastCGI&#8230;<\/p>\n","protected":false},"author":6,"featured_media":2750,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2055,1014],"tags":[1254,1536,1605,1914],"class_list":["post-4054","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-nginx","tag-centos-6-3","tag-linux","tag-nginx","tag-web-server"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/4054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=4054"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/4054\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/2750"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=4054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=4054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=4054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}