Last updated on August 28th, 2020 at 05:12 pm
Google PageSpeed is very helpful when you want to optimize your website speed. You can test your website speed on Google PageSpeed here. Google PageSpeed also provide a module for your server to optimize your content delivery currently they support NGINX and Apache. In this tutorial, I will explain to you how to configure PageSpeed on your NGINX Server. For this tutorial, I am using Digital Ocean Droplet you can create your account using this link and get a credit of 10$ in your account. Now not wasting any time I can give simple step which you can follow to Install NGINX, PHP, MySQL and PageSpeed on your cloud or in Digital Ocean Droplet.
Install NGINX, PHP, and MySQL Server on Droplet
Step 1. First login to your server using a terminal. Once login successfully to your server run below commands. First, we can update our local packages using apt
sudo apt-get update
Step 2. Now after updating and indexing packages we can now install NGINX to our server using below command.
sudo apt-get install nginx
Step 3. Now we can allow some port in our NGINX using commands.
sudo ufw reset sudo ufw app list sudo ufw app info OpenSSH sudo ufw allow 22/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw allow 80/udp sudo ufw allow 443/udp sudo ufw disable sudo ufw enable
Run all above commands one by one and after running all this command check firewall status using below command.
sudo ufw status
Step 4. Now if everything works fine you can open your browser and type your IP address in address bar and hit enter you will get the welcome page of NGINX.
Step 5. Now we can install MySQL on our server run below command
sudo apt-get install mysql-server
after running this command server prompt you to enter a password and confirm the password for your MySQL.
Step 6. Now install PHP using FastCGI process manager PHP-FPM run below command to install by default it will install PHP 7.0 version on the server.
sudo apt-get install php-fpm php-mysql
Step 7. Now open default configure file of fpm/php.ini and search for cgi.fix_pathinfo
and uncomment this and set its value to 0
Open the file in the editor using below command
sudo nano /etc/php/7.0/fpm/php.ini
and to search press cntrl+w and enter the term and hit enter and remove the comment and set the value to 0.
cgi.fix_pathinfo=0
Now restart your PHP FPM using below command
sudo systemctl restart php7.0-fpm
Configure NGINX to run PHP
To configure NGINX to run with PHP we need to edit default virtual host of NGINX, to do so run below command to open default config file.
sudo nano /etc/nginx/sites-available/default
Remove comments from the open file so that your file looks like this
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } }
Now verify your editing and reload the configuration
sudo nginx -t sudo systemctl reload nginx
Now we can check if PHP is running or not to create a file in which we can check phpinfo
sudo nano /var/www/html/info.php
and enter this code
<?php phpinfo();
save file and exit from the editor.
Open browser enters your IP address/info.php and you can check your phpinfo page.
Now we successfully install NGINX, PHP, and MySQL on our server.
Configure PageSpeed with NGINX server
To configure PageSpeed follow simple steps. You can also check the official documentation here.
Step 1. Run Automated install to install all dependencies and the latest version of PageSpeed Module
bash <(curl -f -L -sS https://ngxpagespeed.com/install) \ --nginx-version latest
Step 2. When the installation process is running it will prompt you to add some extra configuration. Below is some recommended module you can add them to your configuration.
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module
Step 3. You will ask to build NGINX press Y and hit enter to continue.
Step 4. If your build is successful you get this message as shown in below screenshot
If successfully installed and you get the message as shown above you need to restart your server.
sudo systemctl restart nginx
Step 5. Now create PageSpeed Cache folder check if not create at this location first at this location./var/cache/ngx_pagespeed/
If not then create one with owner access
mkdir /var/cache/ngx_pagespeed/ chown root:root /var/cache/ngx_pagespeed/
Step 6. Now add PageSpeed to your server block in your NGINX virtual hosts.
To edit your default host file enter below command in your terminal
sudo nano /etc/nginx/sites-available/default
after this add code inside server block.
pagespeed on; # Needs to exist and be writable by nginx. Use tmpfs for best performance. pagespeed FileCachePath /var/ngx_pagespeed_cache; # Ensure requests for pagespeed optimized resources go to the pagespeed handler # and no extraneous headers get set. location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { }
You need to add this code to your server block. After adding code to your default virtual host your file looks like this.
# Default server configuration # server { client_max_body_size 24000M; listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.0-fpm.sock; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } pagespeed on; pagespeed FileCachePath "/var/cache/ngx_pagespeed/"; pagespeed RewriteLevel OptimizeForBandwidth; location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { } }
Now verify your editing and restart NGINX
sudo nginx -t sudo systemctl restart nginx
Now PageSpeed Configuration part is done.
Test if PageSpeed module is running (on NGINX)
To test page speed is running on the server you open the terminal and enter the command
curl -I -X GET {ip addresss | web address}
If your output contains X-Page-Speed then its mean page speed is working on your server but if not then follow simple steps to verify your installation of PageSpeed.
Step 1. Login to your server and enter
nginx -V
Now look for page speed if unable to find that its mean PageSpeed is not installed on the server you can recheck installation steps.
Step 2. If you find page speed in module list but still page speed is not working then you can do one more thing you can create pagespeed.conf inside NGINX config.d folder
and add below code in that file
pagespeed on; pagespeed FetchWithGzip on; pagespeed FileCachePath /run/shm/pagespeed_cache; pagespeed RewriteLevel CoreFilters;
Now after this try again hope this trick will work for you. Please share and like this post if this helps.
Get $10 free from Digital ocean using this link: Digital Ocean Droplet