{"id":4921,"date":"2014-04-23T00:32:10","date_gmt":"2014-04-22T16:32:10","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=4921"},"modified":"2023-06-23T18:35:21","modified_gmt":"2023-06-23T18:35:21","slug":"how-to-run-wordpress-on-nginx-php-fpm-and-mysql","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-run-wordpress-on-nginx-php-fpm-and-mysql\/","title":{"rendered":"How to Setup WordPress on Nginx, PHP-FPM and MySQL"},"content":{"rendered":"<p>WordPress, a widely used blogging platform, can be served using either Apache or NGINX. While Apache is the most popular web server and has served many of the world&#8217;s largest websites, an alternative web server, NGINX (pronounced &#8220;Engine X&#8221;), has been gaining traction. NGINX is an open-source web server and a reverse proxy server for HTTP, SMTP, POP3, and IMAP protocols.<\/p>\n<p>Many web developers and websites have migrated to NGINX due to its scalability, low resource usage, ability to handle many concurrent users, and excellent website performance. For larger and busier websites, it is recommended to host your websites and blogs on a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS hosting<\/a> or a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server<\/a> and run NGINX as a web server.<\/p>\n<p>In this tutorial, we will guide you on how to set up WordPress on NGINX, PHP-FPM, and MySQL. This tutorial assumes that MySQL has been prepared and configured for WordPress and was tested on CentOS 6.5.<\/p>\n<h2>Step 1: Prepare the NGINX Repository<\/h2>\n<p>The first step is to prepare the NGINX repository. To do this, you need to edit the nginx.repo file located in the \/etc\/yum.repos.d\/ directory. You can use the vi editor to open and edit this file. The content of the nginx.repo file should be as follows:<\/p>\n<pre>\r\n[nginx]\r\nname=nginx repo\r\nbaseurl=http:\/\/nginx.org\/packages\/centos\/$releasever\/$basearch\/\r\ngpgcheck=0\r\nenabled=1\r\n<\/pre>\n<h2>Step 2: Install NGINX, PHP, php-fpm, and MySQL Server<\/h2>\n<p>The next step is to install NGINX, PHP, php-fpm, and MySQL server. You can do this by running the following command:<\/p>\n<pre>\r\nyum install nginx php php-cli php-mysql php-gd php-xml php-fpm mysql mysql-server -y\r\n<\/pre>\n<p>This command will install NGINX, PHP, php-fpm, and MySQL server on your system.<\/p>\n<h2>Step 3: Configure PHP<\/h2>\n<p>After installing the necessary software, you need to configure PHP. Open the php.ini file located in the \/etc\/ directory and set cgi.fix_pathinfo=0. The relevant section of the php.ini file should look like this:<\/p>\n<pre>\r\n; cgi.fix_pathinfo provides *real* PATH_INFO\/PATH_TRANSLATED support for CGI. PHP's\r\n; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok\r\n; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting\r\n; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting\r\n; of zero causes PHP to behave as before. Default is 1. You should fix your scripts\r\n; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.\r\n; http:\/\/www.php.net\/manual\/en\/ini.core.php#ini.cgi.fix-pathinfo\r\ncgi.fix_pathinfo=0\r\n<\/pre>\n<h2>Step 4: Set Date Timezone in PHP<\/h2>\n<p>Next, you need to set the date.timezone in the php.ini file. The relevant section of the php.ini file should look like this:<\/p>\n<pre>\r\n[Date]\r\n; Defines the default timezone used by the date functions\r\n; http:\/\/www.php.net\/manual\/en\/datetime.configuration.php#ini.date.timezone\r\ndate.timezone = \"Asia\/Kuala_Lumpur\"\r\n<\/pre>\n<h2>Step 5: Configure PHP-FPM to Use a UNIX Socket<\/h2>\n<p>This server configuration is set up for PHP-FPM to use a UNIX socket. To do this, open the www.conf file located in the \/etc\/php-fpm.d\/ directory and specify the .sock path and change the user to run php-fpm:<\/p>\n<pre>\r\nlisten = \/var\/run\/php-fpm.sock\r\nuser = nginx\r\ngroup = nginx\r\n<\/pre>\n<h2>Step 6: Backup NGINX Config File<\/h2>\n<p>Before making any changes to the NGINX config file, it&#8217;s a good practice to create a backup. You can do this by running the following command:<\/p>\n<pre>\r\ncp -p \/etc\/nginx\/nginx.conf \/etc\/nginx\/nginx.conf.bak\r\n<\/pre>\n<h2>Step 7: Create sites-available Folder<\/h2>\n<p>Next, create a sites-available folder under the \/etc\/nginx\/ directory. This can be done with the following command:<\/p>\n<pre>\r\nmkdir \/etc\/nginx\/sites-available\r\n<\/pre>\n<h2>Step 8: Adjust NGINX Worker Processes &#038; Connections<\/h2>\n<p>NGINX worker processes should equal the number of processors. To check the number of CPUs on your VPS server, use one of the following commands:<\/p>\n<pre>\r\nlscpu | grep '^CPU(s)'\r\ncat \/proc\/cpuinfo | grep processor\r\n<\/pre>\n<p>Then, configure the nginx.conf file as follows:<\/p>\n<pre>\r\nuser nginx;\r\nworker_processes 1;\r\nerror_log \/var\/log\/nginx\/error.log warn;\r\npid \/var\/run\/nginx.pid;\r\nevents {\r\n worker_connections 1024;\r\n}\r\nhttp {\r\n include \/etc\/nginx\/mime.types;\r\n default_type application\/octet-stream;\r\n log_format main '$remote_addr - $remote_user [$time_local] \\\"$request\\\" '\r\n '$status $body_bytes_sent \\\"$http_referer\\\" '\r\n '\\\"$http_user_agent\\\" \\\"$http_x_forwarded_for\\\"';\r\n access_log \/var\/log\/nginx\/access.log main;\r\n sendfile on;\r\n #tcp_nopush on;\r\n keepalive_timeout 65;\r\n gzip on;\r\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;\r\n \r\n include \/etc\/nginx\/sites-available\/*.conf;\r\n}\r\n<\/pre>\n<h2>Step 9: Create common.conf and wordpress.conf<\/h2>\n<p>Create common.conf and wordpress.conf under the \/etc\/nginx\/conf.d\/ folder. The common.conf file should include the following:<\/p>\n<pre>\r\n# Global configuration file.\r\n# ESSENTIAL : Configure Nginx Listening Port\r\nlisten 80;\r\n# ESSENTIAL : Default file to serve. If the first file isn't found,\r\nindex index.php index.html index.htm;\r\n# ESSENTIAL : no favicon logs\r\nlocation = \/favicon.ico {\r\n log_not_found off;\r\n access_log off;\r\n}\r\n# ESSENTIAL : robots.txt\r\nlocation = \/robots.txt {\r\n allow all;\r\n log_not_found off;\r\n access_log off;\r\n}\r\n# ESSENTIAL : Configure 404 Pages\r\nerror_page 404 \/404.html;\r\n# ESSENTIAL : Configure 50x Pages\r\nerror_page 500 502 503 504 \/50x.html;\r\n location = \/50x.html {\r\n root \/usr\/share\/nginx\/html;\r\n }\r\n# SECURITY : Deny all attempts to access hidden files .abcde\r\nlocation ~ \/\\\\. {\r\n deny all;\r\n}\r\n# PERFORMANCE : Set expires headers for static files and turn off logging.\r\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)$ {\r\n access_log off; log_not_found off; expires 30d;\r\n}\r\n<\/pre>\n<p>The wordpress.conf file should include the following:<\/p>\n<pre>\r\n# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact\r\nlocation \/ {\r\n try_files $uri $uri\/ \/index.php?q=$uri&amp;$args;\r\n}\r\n# SECURITY : Deny all attempts to access PHP Files in the uploads directory\r\nlocation ~* \/(?:uploads|files)\/.*\\\\.php$ {\r\n deny all;\r\n}\r\n# REQUIREMENTS : Enable PHP Support\r\nlocation ~ \\\\.php$ {\r\n # SECURITY : Zero day Exploit Protection\r\n try_files $uri =404;\r\n # ENABLE : Enable PHP, listen fpm sock\r\n fastcgi_split_path_info ^(.+\\\\.php)(\/.+)$;\r\n fastcgi_pass unix:\/var\/run\/php-fpm.sock;\r\n fastcgi_index index.php;\r\n include fastcgi_params;\r\n fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\r\n}\r\n# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap\r\nrewrite ^\/sitemap_index\\\\.xml$ \/index.php?sitemap=1 last;\r\nrewrite ^\/([^\/]+?)-sitemap([0-9]+)?\\\\.xml$ \/index.php?sitemap=$1&amp;sitemap_n=$2 last;\r\n<\/pre>\n<h2>Step 10: Create a Virtual Server<\/h2>\n<p>Create a virtual server under the \/etc\/nginx\/sites-available\/ directory. For example, if you want to configure a WordPress site with the domain www.webhostinggeeks.local, set up as follows:<\/p>\n<pre>\r\nserver {\r\n server_name webhostinggeeks.local;\r\n rewrite ^\/(.*)$ http:\/\/www.webhostinggeeks.local\/$1 permanent;\r\n}\r\nserver {\r\n server_name www.webhostinggeeks.local;\r\n root \/var\/www\/html\/wordpress;\r\n access_log \/var\/log\/nginx\/www.webhostinggeeks.local.access.log;\r\n error_log \/var\/log\/nginx\/www.webhostinggeeks.local.error.log;\r\n include conf.d\/common.conf;\r\n include conf.d\/wordpress.conf;\r\n}\r\n<\/pre>\n<h2>Step 11: Start php-fpm and NGINX<\/h2>\n<p>Start php-fpm and NGINX by running the following commands:<\/p>\n<pre>\r\n\/etc\/init.d\/php-fpm start\r\nStarting php-fpm:                                          [  OK  ]\r\n\r\n\/etc\/init.d\/nginx start\r\nStarting nginx:                                            [  OK  ]\r\n<\/pre>\n<h2>Step 12: Enable php-fpm and NGINX at Boot<\/h2>\n<p>To ensure that php-fpm and NGINX start at boot, run the following commands:<\/p>\n<pre>\r\nchkconfig php-fpm on\r\nchkconfig nginx on\r\n<\/pre>\n<h2>Step 13: Verify the Required Port<\/h2>\n<p>Finally, verify that the required port is present by running the following command:<\/p>\n<pre>\r\nnetstat -plunt\r\n<\/pre>\n<p>This command will display the active Internet connections on your server.<\/p>\n<pre>\r\nActive Internet connections (only servers)\r\nProto Recv-Q Send-Q Local Address               Foreign Address             State       PID\/Program name\r\ntcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1042\/rpcbind\r\ntcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3174\/nginx\r\ntcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1096\/sshd\r\ntcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3394\/mysqld\r\ntcp        0      0 :::111                      :::*                        LISTEN      1042\/rpcbind\r\ntcp        0      0 :::22                       :::*                        LISTEN      1096\/sshd\r\nudp        0      0 0.0.0.0:111                 0.0.0.0:*                               1042\/rpcbind\r\nudp        0      0 0.0.0.0:793                 0.0.0.0:*                               1042\/rpcbind\r\nudp        0      0 :::111                      :::*                                    1042\/rpcbind\r\nudp        0      0 :::793                      :::*                                    1042\/rpcbind\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>By following these steps, you can successfully set up WordPress on NGINX, PHP-FPM, and MySQL. This configuration provides a scalable and high-performance solution for hosting your WordPress sites.<\/p>\n<p>Remember, for larger and busier websites, it is recommended to host your websites and blogs on a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS server<\/a> or a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server<\/a> and run NGINX as a web server.<\/p>\n<p>For more information on different types of web servers, you can visit our articles on <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/nginx-server-explained\/\">NGINX<\/a>, and <a href=\"https:\/\/webhostinggeeks.com\/blog\/litespeed-web-server-explained\/\">LiteSpeed<\/a>.<\/p>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">vi \/etc\/yum.repos.d\/nginx.repo<\/span> \u2013 Opens the nginx.repo file for editing<\/li>\n<li><span class=\"fw-bold\">yum install nginx php php-cli php-mysql php-gd php-xml php-fpm mysql mysql-server -y<\/span> \u2013 Installs NGINX, PHP, php-fpm, and MySQL server<\/li>\n<li><span class=\"fw-bold\">vi \/etc\/php.ini<\/span> \u2013 Opens the php.ini file for editing<\/li>\n<li><span class=\"fw-bold\">vim \/etc\/php-fpm.d\/www.conf<\/span> \u2013 Opens the www.conf file for editing<\/li>\n<li><span class=\"fw-bold\">cp -p \/etc\/nginx\/nginx.conf \/etc\/nginx\/nginx.conf.bak<\/span> \u2013 Creates a backup of the NGINX config file<\/li>\n<li><span class=\"fw-bold\">mkdir \/etc\/nginx\/sites-available<\/span> \u2013 Creates a sites-available folder under the \/etc\/nginx\/ directory<\/li>\n<li><span class=\"fw-bold\">lscpu | grep &#8216;^CPU(s)&#8217;<\/span> \u2013 Checks the number of CPUs on your VPS server<\/li>\n<li><span class=\"fw-bold\">cat \/proc\/cpuinfo | grep processor<\/span> \u2013 Another command to check the number of CPUs on your VPS server<\/li>\n<li><span class=\"fw-bold\">vi \/etc\/nginx\/sites-available\/webhostinggeeks.local.conf<\/span> \u2013 Opens the webhostinggeeks.local.conf file for editing<\/li>\n<li><span class=\"fw-bold\">\/etc\/init.d\/php-fpm start<\/span> \u2013 Starts php-fpm<\/li>\n<li><span class=\"fw-bold\">\/etc\/init.d\/nginx start<\/span> \u2013 Starts NGINX<\/li>\n<li><span class=\"fw-bold\">chkconfig php-fpm on<\/span> \u2013 Enables php-fpm at boot<\/li>\n<li><span class=\"fw-bold\">chkconfig nginx on<\/span> \u2013 Enables NGINX at boot<\/li>\n<li><span class=\"fw-bold\">netstat -plunt<\/span> \u2013 Displays the active Internet connections on your server<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the advantage of using NGINX over Apache?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. Unlike Apache, which is a process-driven server, NGINX is event-driven, which allows it to provide high performance under heavy load.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is PHP-FPM and why is it used?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">PHP-FPM (FastCGI Process Manager) isan alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. It allows the website to handle strenuous loads, and it provides on-the-fly error handling, a major advantage over the traditional PHP.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the purpose of the sites-available directory in NGINX?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The sites-available directory is typically used to store all of your server block configurations. These configurations are not enabled until they are linked to the sites-enabled directory. This setup allows you to easily enable or disable sites by creating or removing symbolic links.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Why do we need to set cgi.fix_pathinfo=0 in php.ini file?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Setting cgi.fix_pathinfo=0 in the php.ini file is a security measure. When this setting is enabled (1), the PHP interpreter tries to guess the intended script to execute even when the path is not correctly specified in the request. This behavior could lead to unauthorized code execution. By setting it to 0, we tell PHP not to guess and only execute scripts with a valid path in the request.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the role of worker_processes in NGINX?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The worker_processes directive in NGINX determines how many worker processes are created to handle the load of incoming requests. Each worker process is single-threaded and handles connections in a non-blocking manner. The optimal number of worker processes depends on many factors, but a common approach is to set it equal to the number of CPU cores.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>WordPress, a widely used blogging platform, can be served using either Apache or NGINX. While Apache is the most popular web server and has served many of the world&#8217;s largest&#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":[2056,1014],"tags":[1536,1605,1652,1933],"class_list":["post-4921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cms","category-nginx","tag-linux","tag-nginx","tag-php-fpm","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/4921","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=4921"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/4921\/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=4921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=4921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=4921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}