Get 100/100 Google PageSpeed Insights score with WordPress

 Use this Tutorial to Migrate Websites from Shared Hosting to VPS.Tutorial to Migrate Website(s) from Shared Hosting to VPS. Create a LEMP Stack Server configured with Linux(debian)/NGINX(pagespeed module)/MariaDB/PHP7 - with multiple WordPress websites configured with Letsencrypt(https), and Cloudflare CDN

From having multiple domains on a shared server achieving a server response time of 3 seconds, I migrated them across to a single server; getting a server response time to under 0.3 seconds. Also with a PageSpeed Insights score of 100%.

Create a LEMP Stack Server configured with:

  • Linux(debian Jessie)/
  • NGINX(pagespeed module)/
  • MariaDB/
  • PHP7 – with
  • Multiple WordPress websites configured with
  • Letsencrypt(https), and
  • Cloudflare CDN.

Introduction

Tutorial to Migrate Website(s) from Shared Hosting to VPS.

This tutorial will show you a comprehensive process to migrate your existing websites from a shared hosting server to a VPS, with far greater speed and security than your current setup.  This walk-through is also created for the purposes of anyone wanting to build their site(s) server from scratch.

After much research on this subject myself, I came across various websites that were either giving instructions on very simple tutorials, or offering paid services to complete server transfers/migrations.

This invariably led me to dig around and do my research in finding the missing pieces, in terms of getting past the technical stop/start, trial and error scenarios; to then be fully confident in doing this multiple times.

So in my case, without mentioning the name of my previous shared server provider(s), I felt that when I had outage issues, I could not interrogate/fix the given problems-as you may already know there are certain resource limitations when having a website on a shared server.

In addition, there are also security issues with shared hosting, as you are effectively sharing servers with everyone else, you will ostensibly experience downtime.

The main benefits for migrating are:

Cost – VPS service is only $5/month with multiple websites running.

Speed – I migrated from a LAMP(Apache) stack to creating LEMP (Linux, NGINX, MariaDB, PHP)stack.  You can also deploy your sites with Cloudflare free cloud Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.services.

Flexibility – You are solely responsible for looking after your server, not at the mercy of on-call technicians when there is significant downtime, chasing ticket updates- stabbing in the dark to interrogate a technical problem without the correct tools at your disposal.

Security – Also with logging into your server, you can fully secure your websites by integrating a free https secure connection for your visitors.

With all this in mind, if you follow this tutorial, you can have multiple sites with NGINX (which is very fast), Cloud based services to fulfil CDN requirements.  You will have a free https site, also running with Google page speed module, an open-source server module that optimises your site automatically.

All this achieved with only $5/month.  No brainer!!

Server Basics

Before you begin – I would recommend creating an SSH Key pair on your “Local Machine”:

After you have completed this you will create your VPS instance. I would recommend choosing Debian Jessie as the image, a size of 512MB would suffice for the price of $5, and the Data-centre region.

Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.

Now choose a New SSH Key:

markbarry.info

Here you will copy the output from your local machine Key you generated earlier:

Into the New SSH Key Option:

Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.

Server Setup

Now “Create” your VPS and you will be assigned a unique IP address. As you will have the pre-shared key you created earlier it will make your connection more secure.

To Login you will want to

For added security, you will now need to create a user of your choice:

For elevated root user privileges – add the user to the sudo group:

On your local machine you will now want to apply the same SSH to the new user

Back into your server terminal session, you will want to disable the root user login:

scroll down to:

change this to:

CTRL+X & Y – ENTER to save

Quit your session and re-login with you new user:

Furthermore,  you can optionally, change your default port to something different in “/etc/ssh/sshd_config” and alter port#(up to 65535). Under these conditions you have to login using:

Now you will want to create a basic firewall, to inhibit brute force attacks. There are many options to do so such as fail2ban, or UFW.  There are also options to install Nginx-Naxsi meaning (Nginx Anti XSS & SQL Injection), but at the moment of writing this-it cannot be installed coupled with Nginx pagespeed module.

Now we configure the Timezone & NTP synchronisation

For performance,  I recommend creating a swap file, as we have a VPS instance of 512MB RAM, we will create a file of 1GB.

Ensure this is only accessible to root users:

Mark the file as swap space and enable:

To ensure this change is permanent, for instance on a reboot type:

Lets tweak the performance settings of the server swap file:

You will want to also make this permanent with additionally adding cache pressure. Simply at the bottom of this file:

input these values:

CTRL+X & Y – ENTER to save

At this stage I would create a Snapshot/Backup of your server.

Building the LEMP Stack

Lets start by building NGINX with PageSpeed Module:

For those that have NGINX already installed and want to remove NGINX before you install PSM:

New install

Add the NGINX deb to the list:

Paste these

CTRL+X & Y – ENTER to save

Add the key when prompted for a missing one

Now install zip for the Page Speed Module

Now as per the official Google NGINX Pagespeed Module add these commands

Now add the additional modules when prompted(newer versions of Nginx psm may not need some of these modules):

As we are manually building NGINX Pagespeed Module we now have to create an INIT script:

Now add this to the script:

CTRL+X & Y – ENTER to save
Next create a file with permissions for Nginx cache files:

To find where NGINX is installed type:

Output should be:
nginx: /etc/nginx /usr/local/nginx
Now lets configure our conf file with the Pagespeed configuration:

Paste this into the bottom of the file:

CTRL+X & Y – ENTER to save

To Prevent excessive disk I/O You can further in depth optimise the nginx.conf file, depending on your requirements with recommended buffer size values.
NB: the “client_max_body_size” is the limit to which plugins/themes can be uploaded in your WP directory.

Now reboot the system and type:

You should see:
● nginx.service – LSB: starts the nginx web server
Loaded: loaded (/etc/init.d/nginx)
Active: active (exited)

Mark nginx to be blocked from further updates via apt-get

For every nginx component listed run sudo apt-mark hold

Additional commands useful for interrogation of NGINX

Reload the configuration and Curl:

The output should printout the page speed installed:

Troubleshooting

First check to see if HTTP connections on Port 80 is in use:

If it is not enabled then do so with this command:

Or alternatively you can use:

or:

For those that have issues with this error:

The fix for this is to:

Reboot the system &:

For users that are replacing their existing NGINX configurations, as mentioned previously; it was recommended to Stop the NGINX service before we integrated the new NGINX Pagespeed Module. If you didn’t apply this command, the error you can get is:

Type this command to see the status of Nginx to see which processes may still be running, listening on port 80:

Find the PID number and now type:

Now try:

Assuming you have an active Nginx service Loaded and enabled, check in your browser:

http://yourserver_ip

Tutorial to Migrate Website(s) from Shared Hosting to VPS. Create a LEMP Stack Server configured with Linux(debian)/NGINX(pagespeed module)/MariaDB/PHP7 - with multiple WordPress websites configured with Letsencrypt(https), and Cloudflare CDN

Install PHP7

Open up the main php conf file

CTRL + W (to find “pm.max_children”) ENTER
Change this value and also max requests on both lines respectively to:

This amendment is based on the calculation of using a server with 512 Mb memory and 220 Mb could be used for PHP-FPM, so if every process uses 24 Mb RAM, server max_children value is
220 / 24 = 9.17 (hence the value rounded off to 9).
Checking the activity of Memory usage you can simply type “htop” or “top”, sans the quotations, to interrogate real-time child processes usage.

Tweak some settings to secure php, by disallowing script execution:

Find the line with cgi.fix_pathinfo, ensuring to omit the “;”, and replace with

Also find these lines with CTRL + W, ENTER, and amend accordingly:

CTRL+X & Y – ENTER to save

Install MariaDB

Lets tweak some basic settings for a wordpress install:

change these lines to:

Migrate your files & upload to new Server:

I shall assume that you  are not granted SSH login access to your account – Log into your Shared Hosting account and navigate to your CPANEL – “Site Backup Pro” page.

The important files we need are the database sql.zip files, WP-config.php file and your WP-content folder.

Depending on whether your Hosting provider permits, you can download your full MySql, also your Website files.  Download both if you can or a Full Cpanel Backup.

If you cannot do this then you will have to manually download from your PHPMyadmin database in your CPanel:

php_admin_export markbarry.info

If you have limitations in accessing your website files through backup pro, simply navigate to your website files option and download the ‘WP-content’ folder, and the ‘WP-config.php’ file.  If you cannot see the files listed in your “Public_HTML directory” then you need to ask your hosting provider for elevated root privilege to access/view/modify and ultimately download this file.

Additionally, if you dont have access to PHPAdmin you can use these commands to do a mysqldump of the website database. First you need to create a new file

Now insert these lines unique to your credentials, and save:

Alter the permissions of the new file:

Now run this command as you will now not be prompted for password:

I gave this recommendation, as sometimes mysqldumps can have errors when using command “mysqldump -p”. This isnt the most secure option, but we are migrating anyway, and you can undo this option afterwards, so onwards you can now zip this file and SFTP to your new server.

Now you have these files you can upload them to your server /tmp directory with scp on your local machine:

Now navigate to your tmp folder in your server:

Now open the “wp-config.php” file you downloaded and copy the details highlighted:

Now login to your MariaDB (MySQL drop in)

Paste/replace the details related to your wp-config.php file

Now we source the .sql folder we uploaded and unzipped earlier from the /tmp folder. Feel free to delete this zip file after execution of this command:

Create NGINX server block(s):

There are other permutations you can create these files in Nginx such as yourdomain.com.conf, and creating all your domains in the same Nginx .conf server block. Like apache structure, we will create the folders for our sites config, if you are familiar with this construction:

What we need to do now is ensure our site is listening to port 80, and the Pagespeed Module is added to our site server block.  We will also add be factoring in cache for all necessary filetypes. In addition, we are securing our wordpress install to prevent standalone, maliciously planted code to be executed from any of the WP-content(uploads folder), WP-config.php, WP-includes and also XML RPC if for example you want to install Jetpack.  Paste this into your new file and replace with your domain:

CTRL+X & Y – ENTER to save

Now we need to symlink our sites-available to our sites enabled folder:

Now we need to make sure our nginx.conf file is looking at our new site created in the sites-enabled folder by opening:

and looking for these lines:

Also as Nginx uses the default user www-data, as we did so earlier whilst building Nginx, ensure that this is added at the top of this nginx.conf file:

CTRL+X & Y – ENTER to save

NB: Repeat this section to create an additional server block for multiple websites.

If you always want to see if your site is now actually configured with Pagespeed module navigate to this address and put your domain details in:

Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.

Create WordPress install:

Here we download the latest WordPress install to our /var/www/ folder we will create, and open up again our sites wp-config.php, and find/replace with the details we added earlier to our database:

Not only adding the details of our database/user/password, but also the authenticated unique key and salts.
Secondly you may have change the name of your table prefix to make it more secure, so also change this:

Now we secure ownership and sub-directory permissions:

Now we want to replace our previously uploaded ‘WP-Content’ folder with the the new WordPress install we downloaded.

Update your DNS records to point to your new server and restart PHP & Nginx:

If you want to update your Core-Wordpress/Themes/Plugins via command line you can also do if this using WP-CLI, without logging in through ‘yourdomain.com/wp-admin’.
Simply Curl to install WP-CLI:

Now you will want to change permissions back to your ‘yourusername’ and subdirectory folder permissions. This will prevent anyone from changing anything via ‘wp-login’:

Install Free SSL/TLS Certificate from Let’s Encrypt:

First we will open our firewall port to accept https connections:

or

Now to add the Debian Jessie backports repository and the certbot to configure our SSL certificate:

Now add this snippet to your website server block configuration file in:

with

now reload the configuration:

Request the cert:

should give you the printout:

Now recheck the certificate was created in the correct folders that we will add to our server block again:

With the printout:

For added security we will generate the Diffie–Hellman key exchange:

it will printout this, but will take a couple of minutes to finish:

You may want your site to still listen to http and https connections so you can request 301 redirects to https, whilst having you main server block listening to port 443 with ssl http2.  Here you will be adding the ssl certificate chain where you created earlier.

Now we will setup our auto-renewal using cron script to execute every month:

Input your site details:

Now when you login to yourdomain.com, you will need to change the https://www.yourdomain.com/wp-admin/options-general.php ‘WordPress & Site addressURL’ options to https, to now reflect your changes.
In addition, if you are getting ‘Mixed Content’ warnings you can use the plugin SSL Insecure Content Fixer.

Finally check your domain in a web browser to find the grade of your certificate https connection:

Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.

Cloudflare CDN setup:

I left this task to the end because there were conflicting issues when integrating cloudflare DNS records prior to configuring your SSL certificate.  In relation to this , you can setup a free account with Cloudflare and you will be prompted to change your DNS records in your DNS provider.  When you are done with adding your domain you will need to make a couple of changes:

Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.Achieve 100% Pagespeed. Tutorial to Migrate Website(s) from Shared Hosting to VPS.

As well as having your DNS records updated in your server for the ‘A’, ‘CNAME’ & ‘NS’ included, you will be asked for them in Cloudflare too.  There are other caching and minify options, but you will also need to create a ‘page rule’ as above to ‘Always use HTTPS’, when http version of your site is loaded.

10 thoughts on “Get 100/100 Google PageSpeed Insights score with WordPress”

  1. Hello would you mind letting me know which web host you’re using?
    I’ve loaded your blog in 3 completely different browsers and I must say this
    blog loads a lot faster then most. Can you recommend a good web hosting provider
    at a reasonable price? Cheers, I appreciate it!

    Reply
  2. Greate pieces. Keep posting such kind of info on your site.

    Im really impressed by your site.
    Hello there, You’ve performed an excellent job. I will definitely digg it and in my opinion recommend to my friends.
    I am sure they’ll be benefited from this web site.

    Reply
  3. Good day! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?

    My website covers a lot of the same subjects as yours and I believe we
    could greatly benefit from each other. If you’re interested feel free to shoot me an email.
    I look forward to hearing from you! Excellent blog by the way!

    Reply
  4. Thank you for the good writeup. It in fact was
    a amusement account it. Look advanced to far added agreeable
    from you! By the way, how can we communicate?

    Reply
  5. Hello, I think your site might be having browser compatibility issues.

    When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you a quick heads up!
    Other then that, fantastic blog!

    Reply
  6. Everything is very open with a precise clarification of the challenges.
    It was truly informative. Your website is extremely
    helpful. Thank you for sharing!

    Reply
  7. Great post. I was checking constantly this blog and I am impressed!
    Extremely useful information specifically the ultimate section :
    ) I maintain such info a lot. I used to be seeking this
    particular info for a long time. Thank you and best of luck.

    Reply

Leave a Comment

17 + five =