Build an extremely fast WordPress with GCP Part 2: Nginx

Apache or Nginx, what softwares should be used to for your server to achieve the maximum performance? If you have a small free server from GCP from part 1, and you want it to be able to handle average 1000 clients per second, Nginx is the best option. Apache use more server resources and must be configured properly to get the best performance. Nginx can handle very large amount of traffic even in a limited environment. We have done the loading test with the server from Google Cloud Platform from part 1 and the result was great without deep optimization. 1000 client/sec is not the maximum that this setup can get, it is the limit of the free test we could get. When we tried the same test using Apache, the test was aborted at the beginning because server crashed. That is why we will use Nginx. We are not the server expert, so we will not discuss how to optimize Apache to pass the same test. What we will need is NGINX + MySQL + PHP-FPM + Microcaching + WordPress.

1. install Nginx.

1
2
3
sudo apt-get update -y
sudo apt-get install nginx -y
sudo service nginx start

2. install Mysql (or MariaDB).

1
2
3
4
5
sudo apt-get install mysql-server -y
sudo service mysql stop
sudo mysql_install_db # follow instructions
sudo service mysql start
sudo mysql_secure_installation # follow instructions

3. install Install PHP-FPM

1
sudo apt-get install php-fpm php-mysql php-gd php-cli php-xml php-mbstring

Add cgi.fix_pathinfo=0 to php.ini, if you know how to add, jump to step 4, otherwise, continue below.

1
2
3
4
5
# if you use different version of PHP, change 7.0 to your version e.g. 8.0
sudo nano /etc/php/7.0/fpm/php.ini

# paste cgi.fix_pathinfo=0
# press ctrl + X key, and type Y to save the file.
1
2
3
# restart PHP-FPM service
# if you use different version of PHP, change the 7.0 accordingly
sudo service php7.0-fpm restart

4. configure Nginx to use PHP-FPM to process php files

1
2
3
4
5
6
7
8
9
10
11
12
sudo nano /etc/nginx/sites-available/default

# add the following to the server { } block

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Restart Nginx

1
sudo service nginx restart

5. add Microcaching to Nginx

With Microcaching, Nginx can handle large amount of traffic because dynamic inquiries/files are cached to prevent overloading your database. For example if each client to your website generate 2 database inquiry, 1000 client will have 2000 inquires. If you set up cache for 5 seconds, 1000 clients within 5 seconds will only hit your database 2 times if both inquires are cached. That is the magic of this setup.

1
2
3
# always backup the file you want to change
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
sudo nano /etc/nginx/sites-available/default

Add the following to the file. You can add this before the server {} block

1
2
3
# Microcaching
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=qualityology-com:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

Add the following to location ~ \.php$ {} block. Below configuration will set the cache to expire after 10 seconds, and no cache for logged in users and commenters. You can change the 10s to longer time, but 10s is long enough for your need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#Cache everything by default
set $no_cache 0;

#Don't cache logged in users or commenters
if ( $http_cookie ~* "comment_author_wordpress_(?!test_cookie)wp-postpass_" ) {
set $no_cache 1;
}

#Don't cache the following URLs
if ($request_uri ~* "/(wp-admin/wp-login.php)")
{
set $no_cache 1;
}

#matches keys_zone in fastcgi_cache_path
fastcgi_cache qualityology-com;

#don't serve pages defined earlier
fastcgi_cache_bypass $no_cache;

#don't cache pages defined earlier
fastcgi_no_cache $no_cache;

#defines the default cache time
fastcgi_cache_valid any 10s;

#unsure what the impacts of this variable is
fastcgi_max_temp_file_size 2M;

#Use stale cache items while updating in the background
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_cache_lock on;
fastcgi_cache_lock_timeout 10s;

Test if your configuration

1
sudo nginx -t

If no error, reload your Nginx or restart it

1
sudo service nginx start

6. install WordPress

1
2
3
4
5
6
cd /var/www/html/
sudo wget http://wordpress.org/latest.tar.gz
sudo tar xzvf latest.tar.gz
sudo rm latest.tar.gz
sudo mv wordpress/* ./
sudo chown -R www-data:www-data /var/www/html/*

You are all set. Now, open your domain or IP to setup WordPress

Edit Hosts file to override DNS record for domains iPhone's battery draining? Here's what you can do

Comments