Page speed SEO tutorial part 3 – Nginx | Varnish Https | PHP7.1 | Percona.
Page speed for SEO, In this section, I will show you how to get your server up and running using high-performance server-side software. Below you will see a full list of software that we will install during this section of the page speed for SEO tutorial.
Software that will be installed and configured are.
- Nginx
- PHP 7.1
- PHP dependences
- Percona 5.7
- Varnish
By using the setup shown below, you can achieve vast gains in server speed and page load times, without needing to spend a fortune of high priced servers.
Page speed for SEO really does impact your websites rankings, I’m going to tell you why!
When building its databases of results, Google. will look at user experience and user intent as the main ranking factors. On average 50% of bounce rate comes down to site speed and page load times. This will see your website drop in Google rankings, Due to user experience being so low because of your bounce rates.
Today is the day! say no more to slow speeds and give your website the boost it deserves!
To follow this page speed SEO seo tutorial more easily, I have created a step by step video.
Follow the Page speed for SEO video And Copy the code From here. Its the easiest way to follow this full SEO page speed guide Part 3.
Now you know what we will be doing during this section of the page speed SEO tutorial, lets get on with it.
The first thing to do is open up PuTTy and login to your server. once logged in you will see similar to the image below.
Now as long as you are logged in successfully as root then you are set to go. The code below is given in sections you can just copy and past. To paste into Putty you just right click the mouse, nothing else.
As in all sections, you can choose to watch the full setup from my video listed below. The code follows after.
This will add the sources required for PHP 7.1. illustration images will be provided throughout these steps
1 |
#sudo apt-get install -y python-software-properties |
1 |
#sudo add-apt-repository ppa:ondrej/php |
Press enter when requested
Now we will add the percona repo to your source list. Use the below lines to achieve this.
1 |
#echo "deb http://repo.percona.com/apt xenial main" >> /etc/apt/sources.list |
1 |
#echo "deb-src http://repo.percona.com/apt xenial main" >> /etc/apt/sources.list |
Now Update your repo list using the below code.
1 |
#apt-get update |
Next we will install Nginx, Varnish and PHP 7.1 and Percona. With Page speed SEO in mind
1 |
#sudo apt-get --allow-unauthenticated install -y nginx varnish gksu php7.1-curl php7.1 php7.1-fpm php7.1-cli php7.1-common php7.1-mbstring php7.1-gd php7.1-intl php7.1-xml php7.1-mysql php7.1-mcrypt php7.1-zip percona-server-server-5.7 |
During the install, you will be required to enter a password for Percona.
type your password and hit enter.
You will need to confirm your password so go ahead and enter your password again.
Now we will configure our Nginx Server. Nginx is the actual server software, Nginx is vital for page speed SEO, it’s the only server software you require, it’s much faster than Apache and deals with requests that’s more in tune with the age of super fast internet and 4G connections on mobiles. Meaning its super fast if configured correctly and vastly increases page speed for SEO.
The first step is to create a directory for your website by running the below code change hobbydabber.com to match your domain.
1 |
#sudo mkdir -p /var/www/hobbydabber.com/html |
Nginx uses the user www-data so we will change the user ownership on your website directory. remember to replace hobbydabber.com with your URL
1 |
#sudo chown -R www-data:www-data /var/www/hobbydabber.com/html |
The permissions should already be set correctly but just to make sure run the below command
1 |
#sudo chmod -R 755 /var/www |
now we will create an index.php file so we can test the server is rendering your pages.
run the below code make sure to change hobbydabber.com to match your domain
1 |
#sudo nano /var/www/hobbydabber.com/html/index.php |
Then copy and paste the below HTML and past into the console
1 2 3 4 5 6 7 8 |
<html> <head> <title>Welcome to hobbydabber.com!</title> </head> <body> <h1>Success! The hobbydabber.com server block is working!</h1> </body> </html> |
Save the file by using ctrl +x then Y then enter!
we will next create the server block for Nginx, Change the domain to match your domain
1 |
#sudo nano /etc/nginx/sites-available/hobbydabber.com |
Now copy and paste from below into the console. remember to change all hobbydabber.com to match your domain.
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 |
#server { client_max_body_size 200M; listen 8080; listen [::]:8080; listen 443; ssl on; ssl_certificate /etc/nginx/certs/hobbydabber_cert.pem; ssl_certificate_key /etc/nginx/certs/hobbydabber_key.key; server_name hobbydabber.com www.hobbydabber.com; root /var/www/hobbydabber.com/html; port_in_redirect off; index index.html index.htm index.php; keepalive_timeout 65; location / { try_files $uri $uri/ /index.php?$args; } location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff)$ { expires 365d; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/run/php/php7.1-fpm.sock; } } |
The next step is to enable the Nginx server block, remember to change hobbydabber.com to match your domain.
1 |
#sudo ln -s /etc/nginx/sites-available/hobbydabber.com /etc/nginx/sites-enabled/ |
If you are planning on creating more server blocks on the same server then we need to change a setting in our nginx.conf file in regards to
hash bucket memory issues. run the below
1 |
#sudo nano /etc/nginx/nginx.conf |
Now use the arrow keys and go down till you find the below line, and delete the “#”
#server_names_hash_bucket_size 64;
once the # has been deleted save the file by pressing ctrl and x then y then enter
Next, we need to edit the listen to ports on the default Nginx file. use the below code and change the listen ports for “80” to “8080”
1 |
#sudo nano /etc/nginx/sites-available/default |
We need to now configure our SSL certificates with cloudFlare. This is not so much a page speed factor for SEO but A SSL Certificate increases security for your visitors along with conversions.
First, we will create a directory for our certificates to live. Use the code below.
1 |
#mkdir /etc/nginx/certs/ |
Next, we will create our certificate file.
1 |
#sudo nano /etc/nginx/certs/hobbydabber_cert.pem |
Leave this empty file open for the time being whilst we go to CloudFlare and get our certificate and key.
head over to CloudFlare and log in as shown on day 2 of this tutorial then choose the crypto tab.
Now scroll down to Origin Certificates and click “create Certificate”
Cloudflare will show you a few options however we will not adjust any of them, So just click the next button.
Now you will receive your certificate and Key, first copy the text from your certificate.
then paste into the already open file in PuTTy then save using ctrl + x then Y then enter.
Next, we will create the key for our certificate.
1 |
#sudo nano /etc/nginx/certs/hobbydabber_key.key |
now go back to cloudflare and copy the key and then paste into PuTTy.
Save the key in PuTTy and exit CloudFlare
now check the Nginx config is working by running
1 |
#sudo nginx -t |
if all is well you should see
- nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
- nginx: configuration file /etc/nginx/nginx.conf test is successful
now restart nginx
1 |
#sudo systemctl restart nginx |
Your website will be unavailable at the moment this is because we have added the configuration for varnish within Nginx,
however, we have not yet setup Varnish. This is our next step.
Setup Varnish.
1 |
#sudo nano /etc/varnish/custom.vcl |
Copy the below code and paste into the console, remember change all domain name references to match your domain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
vcl 4.0; backend default { .host = "127.0.0.1"; .port = "8080"; .between_bytes_timeout = 30s; } sub vcl_recv { if (client.ip != "127.0.0.1" && req.http.host ~ "hobbydabber.com") { set req.http.x-redir = "http://www.hobbydabber.com" + req.url; } if (req.url ~ "wp-admin|wp-login") { return (pass); } set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", ""); set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", ""); if (req.http.cookie == "") { unset req.http.cookie; } } |
Now save that file by holding ctrl and x then hit y and enter.
Next edit the default vcl by using the below code
1 |
#nano /etc/varnish/default.vcl |
Delete all code from within that file by holding alt + A then use the down arrow to highlight. once highlighted press ctrl + k to delete.
Now the file is clean copy and paste the below code.
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 34 35 36 37 38 39 40 41 42 43 44 |
# # This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { if (!(req.url ~ "wp-(login|admin)")) { unset req.http.cookie; } } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. } sub vcl_fetch { if (!(req.url ~ "wp-(login|admin)")) { unset beresp.http.set-cookie; } } |
Now create a link in your system.d folder by using the below code
1 |
#nano /etc/systemd/system/varnish.service |
then copy and paste from below.
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 |
#[Unit] Description=Varnish Cache, a high-performance HTTP accelerator [Service] Type=forking # Maximum number of open files (for ulimit -n) LimitNOFILE=131072 # Locked shared memory - should suffice to lock the shared memory log # (varnishd -l argument) # Default log size is 80MB vsl + 1M vsm + header -> 82MB # unit is bytes LimitMEMLOCK=85983232 # On systemd >= 228 enable this to avoid "fork failed" on reload. #TasksMax=infinity # Maximum size of the corefile. LimitCORE=infinity # Set WARMUP_TIME to force a delay in reload-vcl between vcl.load and vcl.use # This is useful when backend probe definitions need some time before declaring # configured backends healthy, to avoid routing traffic to a non-healthy backend. #WARMUP_TIME=0 ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/custom.vcl -S /etc/varnish/secret -s malloc,256m ExecReload=/usr/share/varnish/reload-vcl [Install] WantedBy=multi-user.target |
save and exit.
Lets reload systemd service. run the below code.
1 |
#sudo systemctl daemon-reload |
now restart varnish
1 |
#sudo systemctl restart varnish.service |
Restart Nginx
1 |
#sudo systemctl restart nginx |
you should now have a working server running Nginx-fpm via varnish cache running with secure SSL
Now we will test our setup of varnish is actually working and serving varnish cached pages. This can only be tested from the command line because varnish is passing the cache to nginx-fpm so online test tools will not work.
To do the test just past the below code into the command line.
1 |
# curl -I http://localhost:80 |