{"id":5460,"date":"2014-10-13T15:21:08","date_gmt":"2014-10-13T07:21:08","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=5460"},"modified":"2023-06-23T13:12:59","modified_gmt":"2023-06-23T13:12:59","slug":"how-to-setup-nginxphp5-4-php-fpm-mysql-5-5-on-centos-6-5-vps","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-setup-nginxphp5-4-php-fpm-mysql-5-5-on-centos-6-5-vps\/","title":{"rendered":"How to Setup Nginx,PHP5.4, PHP-FPM, MySQL 5.5 On CentOS 6.5 VPS"},"content":{"rendered":"<p>This post will show you the procedure to setup Nginx, PHP5.4, PHP-FPM and MySQL 5.5 On CentOS 6.5 Virtual private server(VPS). You need to setup the required repo such as <a href=\"https:\/\/webhostinggeeks.com\/howto\/how-to-configure-epel-repository-on-centos-6-4-x86_64\/http:\/\" target=\"_blank\" rel=\"noopener\">EPEL<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/howto\/how-to-install-remi-yum-repository-on-centos-6-2-x86-and-x86_64\/\" target=\"_blank\" rel=\"noopener\">Remi<\/a> and also the NGINX repo.<\/p>\n<h5>What is NGINX ?<\/h5>\n<p>NGINX is an alternate web server for Apache. Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. Many websites and the web developer have moved to NGINX because it\u2019s scalable, low resources, can handle many users concurrency and good website performance. Now it still third most popular web server in the world and <a href=\"https:\/\/news.netcraft.com\/archives\/2014\/\">it serve just over 14% of all hostnames<\/a>.<\/p>\n<h5>What is PHP(PHP-FPM) ?<\/h5>\n<p>PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.<\/p>\n<h5>What is MySQL ?<\/h5>\n<p>MySQL Database server is one of the most popular used database in the internet especially for content management and blogging site.<\/p>\n<h4>Steps to setup\u00a0Nginx,PHP5.4, PHP-FPM, MySQL 5.5 On CentOS 6.5 VPS<\/h4>\n<p>1. Setup EPEL and Remi repository.<br \/>\nHow to prepared <a href=\"https:\/\/webhostinggeeks.com\/howto\/how-to-configure-epel-repository-on-centos-6-4-x86_64\/\" target=\"_blank\" rel=\"noopener\">EPEL<\/a> and how to configure <a href=\"https:\/\/webhostinggeeks.com\/howto\/how-to-install-remi-yum-repository-on-centos-6-2-x86-and-x86_64\/\" target=\"_blank\" rel=\"noopener\">Remi<\/a> repository in to CentOS.<\/p>\n<p>2. Install php 5.4, php-fpm and MySQL 5.5 Server :<\/p>\n<pre>[root@vps-08 ~]# yum --enablerepo=remi install php php-mysql php-fpm mysql mysql-server -y\n<\/pre>\n<p>3. Setup repository for nginx :<\/p>\n<pre>[root@vps-08 ~]# vi \/etc\/yum.repos.d\/nginx.repo\n<\/pre>\n<p>Add the following and save :<\/p>\n<pre>[nginx]\nname=nginx repo\nbaseurl=http:\/\/nginx.org\/packages\/centos\/$releasever\/$basearch\/\ngpgcheck=0\nenabled=1\n<\/pre>\n<p>4. Install and setup NGINX :<br \/>\nRun the following command to install NGINX.<\/p>\n<pre>[root@vps-08 ~]# yum install nginx -y\n<\/pre>\n<p>a. Setup NGINX config file :<\/p>\n<pre>[root@vps-08 ~]# vi \/etc\/nginx\/nginx.conf\n<\/pre>\n<p>Add the following and save:<\/p>\n<pre>user  nginx;\nworker_processes  2;\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    gzip_types text\/css text\/x-component application\/x-javascript application\/javascript text\/javascript text\/x-js text\/richtext image\/svg+xml text\/plain text\/xsd text\/xsl text\/xml image\/x-icon;\n\n    include \/etc\/nginx\/sites-available\/*.conf;\n\n}\n<\/pre>\n<p>b. Create sites-available directory and create nginx virtual host for example.com domain :<\/p>\n<pre>[root@vps-08 ~]# mkdir \/etc\/nginx\/sites-available\n<\/pre>\n<pre>[root@vps-08 ~]# vi \/etc\/nginx\/sites-available\/example.com.conf\n<\/pre>\n<p>Add the following and save :<\/p>\n<pre>server {\nlisten       80;\n    server_name example.com;\n    rewrite ^\/(.*)$ http:\/\/www.example.com\/$1 permanent;\n}\n\nserver {\n        server_name www.example.com;\n        root \/var\/www\/html\/example;\n        access_log \/var\/log\/nginx\/example.com.access.log;\n        error_log \/var\/log\/nginx\/example.com.error.log;\n        include conf.d\/common.conf;\n        include conf.d\/wordpress.conf;\n        include conf.d\/w3tc.conf;\n}\n<\/pre>\n<p>c. Create these three configuration files. It was optimized for WordPress site.<\/p>\n<pre>\/etc\/nginx\/conf.d\/common.conf\n\/etc\/nginx\/conf.d\/wordpress.conf\n\/etc\/nginx\/conf.d\/w3tc.conf\n<\/pre>\n<p>Create common.conf :<\/p>\n<pre>[root@vps-08 ~]# vi \/etc\/nginx\/conf.d\/common.conf\n<\/pre>\n<p>Add the following and save.<\/p>\n<pre># Global configuration file.\n# ESSENTIAL : Configure Nginx Listening Port\nlisten 80;\n# ESSENTIAL : Default file to serve. If the first file isn't found,\nindex index.php index.html index.htm;\n# ESSENTIAL : no favicon logs\nlocation = \/favicon.ico {\n    log_not_found off;\n    access_log off;\n}\n# ESSENTIAL : robots.txt\nlocation = \/robots.txt {\n    allow all;\n    log_not_found off;\n    access_log off;\n}\n# ESSENTIAL : Configure 404 Pages\nerror_page 404 \/404.html;\n# ESSENTIAL : Configure 50x Pages\nerror_page 500 502 503 504 \/50x.html;\n    location = \/50x.html {\n        root \/usr\/share\/nginx\/html;\n    }\n# SECURITY : Deny all attempts to access hidden files .abcde\nlocation ~ \/\\. {\n    deny all;\n}\n# PERFORMANCE : Set expires headers for static files and turn off logging.\nlocation ~* ^.+\\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {\n    access_log off; log_not_found off; expires 30d;\n}\n<\/pre>\n<p>Configure wordpress.conf :<\/p>\n<pre>[root@vps-08 ~]# vi \/etc\/nginx\/conf.d\/wordpress.conf\n<\/pre>\n<p>Add the following and save :<\/p>\n<pre># WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact\nlocation \/ {\n    try_files $uri $uri\/ \/index.php?q=$uri&amp;$args;\n}\n\n# SECURITY : Deny all attempts to access PHP Files in the uploads directory\nlocation ~* \/(?:uploads|files)\/.*\\.php$ {\n    deny all;\n}\n# REQUIREMENTS : Enable PHP Support\nlocation ~ \\.php$ {\n    # SECURITY : Zero day Exploit Protection\n    try_files $uri =404;\n    # ENABLE : Enable PHP, listen fpm sock\n    fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n    fastcgi_pass unix:\/var\/run\/php-fpm.sock;\n    fastcgi_index index.php;\n    include fastcgi_params;\n    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n}\n# PLUGINS : Enable Rewrite Rules for SiteMap\nrewrite ^\/sitemap(-+([a-zA-Z0-9_-]+))?\\.xml$ \"\/index.php?xml_sitemap=params=$2\" last;\nrewrite ^\/sitemap(-+([a-zA-Z0-9_-]+))?\\.xml\\.gz$ \"\/index.php?xml_sitemap=params=$2;zip=true\" last;\nrewrite ^\/sitemap(-+([a-zA-Z0-9_-]+))?\\.html$ \"\/index.php?xml_sitemap=params=$2;html=true\" last;\nrewrite ^\/sitemap(-+([a-zA-Z0-9_-]+))?\\.html.gz$ \"\/index.php?xml_sitemap=params=$2;html=true;zip=true\" last;\n<\/pre>\n<p>Create w3tc.conf file :<\/p>\n<pre>[root@vps ~]# vi \/etc\/nginx\/conf.d\/w3tc.conf\n<\/pre>\n<p>Add the following and save :<\/p>\n<pre># BEGIN W3TC Page Cache core\nset $w3tc_rewrite 1;\nif ($request_method = POST) {\n    set $w3tc_rewrite 0;\n}\nif ($query_string != \"\") {\n    set $w3tc_rewrite 0;\n}\nif ($http_cookie ~* \"(comment_author|wp\\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)\") {\n    set $w3tc_rewrite 0;\n}\nif ($http_cookie ~* \"(w3tc_preview)\") {\n    set $w3tc_rewrite _preview;\n}\nset $w3tc_enc \"\";\nif ($http_accept_encoding ~ gzip) {\n    set $w3tc_enc _gzip;\n}\nset $w3tc_ext \"\";\nif (-f \"$document_root\/wp-content\/cache\/page_enhanced\/$http_host\/$request_uri\/_index$w3tc_rewrite.html$w3tc_enc\") {\n    set $w3tc_ext .html;\n}\nif (-f \"$document_root\/wp-content\/cache\/page_enhanced\/$http_host\/$request_uri\/_index$w3tc_rewrite.xml$w3tc_enc\") {\n    set $w3tc_ext .xml;\n}\nif ($w3tc_ext = \"\") {\n  set $w3tc_rewrite 0;\n}\nif ($w3tc_rewrite = 1) {\n    rewrite .* \"\/wp-content\/cache\/page_enhanced\/$http_host\/$request_uri\/_index$w3tc_rewrite$w3tc_ext$w3tc_enc\" last;\n}\n# END W3TC Page Cache core\n<\/pre>\n<p>d. Modify default.conf file :<\/p>\n<pre>[root@vps-08 ~]# vi \/etc\/nginx\/conf.d\/default.conf\n<\/pre>\n<pre>server {\n    listen       80;\n    server_name  localhost;\n\n    location \/ {\n        root   \/usr\/share\/nginx\/html;\n        index  index.html index.htm;\n    }\n\n# redirect server error pages to the static page \/50x.html\n    #\n    error_page   500 502 503 504  \/50x.html;\n    location = \/50x.html {\n        root   \/usr\/share\/nginx\/html;\n    }\n}\n<\/pre>\n<p>5. Secure Mysql :<\/p>\n<pre>[root@vps-08 ~]# \/usr\/bin\/mysql_secure_installation\n<\/pre>\n<p>Sample :<\/p>\n<pre>[root@vps-08 ~]# \/usr\/bin\/mysql_secure_installation\n\nNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL\n      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!\n\n\nIn order to log into MySQL to secure it, we'll need the current\npassword for the root user.  If you've just installed MySQL, and\nyou haven't set the root password yet, the password will be blank,\nso you should just press enter here.\n\nEnter current password for root (enter for none):\nOK, successfully used password, moving on...\n\nSetting the root password ensures that nobody can log into the MySQL\nroot user without the proper authorisation.\n\nSet root password? [Y\/n] y\nNew password:\nRe-enter new password:\nPassword updated successfully!\nReloading privilege tables..\n ... Success!\n\n\nBy default, a MySQL installation has an anonymous user, allowing anyone\nto log into MySQL without having to have a user account created for\nthem.  This is intended only for testing, and to make the installation\ngo a bit smoother.  You should remove them before moving into a\nproduction environment.\n\nRemove anonymous users? [Y\/n] y\n ... Success!\n\nNormally, root should only be allowed to connect from 'localhost'.  This\nensures that someone cannot guess at the root password from the network.\n\nDisallow root login remotely? [Y\/n] n\n ... skipping.\n\nBy default, MySQL comes with a database named 'test' that anyone can\naccess.  This is also intended only for testing, and should be removed\nbefore moving into a production environment.\n\nRemove test database and access to it? [Y\/n] y\n - Dropping test database...\n ... Success!\n - Removing privileges on test database...\n ... Success!\n\nReloading the privilege tables will ensure that all changes made so far\nwill take effect immediately.\n\nReload privilege tables now? [Y\/n] y\n ... Success!\n\nCleaning up...\n\n\n\nAll done!  If you've completed all of the above steps, your MySQL\ninstallation should now be secure.\n\nThanks for using MySQL!\n\n\n<\/pre>\n<p>6. Configure php-fpm :<\/p>\n<pre>[root@vps-08 ~]# vi \/etc\/php-fpm.d\/www.conf\n<\/pre>\n<p>Update and uncomment the following :<\/p>\n<pre>listen = \/var\/run\/php-fpm.sock\n..\nlisten.mode = 0666\n..\nuser = nginx\ngroup = nginx\n..\npm = dynamic\n..\npm.max_children = 50\npm.start_servers = 10\npm.min_spare_servers = 10\npm.max_spare_servers = 10\npm.max_requests = 200\n..\nslowlog = \/var\/log\/php-fpm\/www-slow.log\n..\nphp_admin_value[error_log] = \/var\/log\/php-fpm\/www-error.log\nphp_admin_flag[log_errors] = on\n..\nphp_value[session.save_handler] = files\nphp_value[session.save_path] = \/var\/lib\/php\/session\n<\/pre>\n<p>7. Restart the NGINX and php-fpm :<br \/>\nRestart the NGINX and php-fpm service to apply the changes on the configuration files.<\/p>\n<pre>[root@vps-08 ~]# service nginx restart; service php-fpm restart\nStopping nginx:                                            [  OK  ]\nStarting nginx:                                            [  OK  ]\nStopping php-fpm:                                          [  OK  ]\nStarting php-fpm:                                          [  OK  ]\n<\/pre>\n<p>Check the service listened on your VPS :<\/p>\n<pre>[root@vps-08 ~]# netstat -plunt | grep LISTEN\ntcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1097\/rpcbind\ntcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      18070\/nginx\ntcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1130\/sshd\ntcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      18358\/mysqld\ntcp        0      0 :::111                      :::*                        LISTEN      1097\/rpcbind\ntcp        0      0 :::22                       :::*                        LISTEN      1130\/sshd\n<\/pre>\n<p>Now you can start to setup WordPress blog into your VPS server.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-6007 size-full lazyload\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2015\/02\/nginx-ehowstuff.png\" alt=\"setup nginx\" width=\"600\" height=\"349\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2015\/02\/nginx-ehowstuff.png 600w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2015\/02\/nginx-ehowstuff-300x175.png 300w\" data-sizes=\"(max-width: 600px) 100vw, 600px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 600px; --smush-placeholder-aspect-ratio: 600\/349;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will show you the procedure to setup Nginx, PHP5.4, PHP-FPM and MySQL 5.5 On CentOS 6.5 Virtual private server(VPS). You need to setup the required repo such as&#8230;<\/p>\n","protected":false},"author":6,"featured_media":5533,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2055,1014,1025],"tags":[1585,1605,1645,1652,1933],"class_list":["post-5460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-nginx","category-php","tag-mysql","tag-nginx","tag-ph-p5-4","tag-php-fpm","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5460","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=5460"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5460\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/5533"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=5460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=5460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=5460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}