Install Nginx with Google PageSpeed on Ubuntu 18.04 with Google Cloud Platform. In this guide you are going to learn how to setup and configure pagespeed module for your website with Nginx.

Pagespeed can automatically increase performance of website by changing the web resources, improves web latency and many more.

Prerequisites

  1. Your Compute Engine Instance running.
  2. For setting up Compute Engine, see the Setting up Compute Engine Instance.
  3. Set up Cloud DNS, see the Setting up Google Cloud DNS for your domain.

This setup can be done any of the Linux distributions and it will work. This demo is done on Google Compute Engine VM instance with 3.75 GB RAM.

PlatformMachine TypeCost (Monthly)
Digitalocean1 GB RAM$5
Google Cloud Platform600 MB RAM$4.28
Amazon Web Services1 GB RAM$14

Step 1: Install Nginx

If you haven’t installed Nginx you can install Nginx using the below commands.

sudo apt update && sudo apt upgrade
sudo apt install nginx

Once Nginx installed you can verify the status.

sudo service nginx status
● nginx.service - The NGINX HTTP and reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2019-07-26 09:22:51 UTC; 1h 24min ago
   Process: 14404 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
   Process: 14419 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Process: 14407 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Main PID: 14423 (nginx)
     Tasks: 10 (limit: 2361)
    CGroup: /system.slice/nginx.service
            ├─14423 nginx: master process /usr/sbin/nginx
            ├─14426 nginx: worker process
            └─14427 nginx: cache manager process

Now you can check your domain name in the browser. You will see the Nginx welcome page.

Step 2: Install PageSpeed and Modules

Install ngx_pagespeed with the following command.

sudo apt install libssl-dev
bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
--nginx-version latest

During installation you will see

Extracting nginx…About to build nginx. Do you have any additional ./configure arguments you would like to set? For example, if you would liketo build nginx with https support give –with-http_ssl_moduleIf you don’t have any, just press enter.
->

Here you can add the modules, enter the ones below

--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

Continue the installation by entering Y

Step 3: Configure Systemd for Nginx

To control Nginx process (start, stop, restart) you can configure it with a systemd service. To configure it edit the default systemd configuration for Nginx annd replace it with the below code.

sudo nano /lib/systemd/system/nginx.service

Replace it with the below code.

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Save the file and exit.

To apply the changes reload the systemd daemon.

sudo systemctl daemon-reload

Step 4: Setup Cache Directory

Create anew user named nginx without home directory.

sudo useradd --no-create-home nginx
sudo mkdir -p /var/cache/nginx/client_temp
sudo mkdir /var/cache/ngx_pagespeed

Step 5: Setup Correct Permissions

sudo chown nginx:nginx /var/cache/ngx_pagespeed
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Step 6: Enable PageSpeed

To enable pagespeed, you need to edit the Nginx virtual host configuration and add the pagespeed configutations.

sudo nano /etc/nginx/sites-available/default

Replace the contents of the file with the one below.

server {
     listen 80 default_server;
     listen [::]:80 default_server;

     server_name yourdomainname.com; 

     root /var/www/html;
     index index.html index.htm;

     access_log   logs/yourdomainname_access.log;
     error_log    logs/yourdomainname_error.log;

     location / {
         try_files $uri $uri/ =404;
     }

     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$" { }
 }

Save the file and exit.

Step 7: Test the configuration

Create a new index.html file the web root directory.

sudo nano /var/www/html/index.html

Enter any content and save the file.

Restart Nginx.

Once you have the test file, Nginx configured and pagespeed enabled you can test the configuration.

sudo nginx -t

If the test is successful you can restart Nginx.

If you get any error like …

 nginx: [emerg] dlopen() "/etc/nginx/modules/ngx_http_geoip_module.so" failed (/etc/nginx/modules/ngx_http_geoip_module.so: cannot open shared object file: No such file or directory) in /etc/nginx/modules-enabled/50-mod-http-geoip.conf:1

Edit the nginx.conf file and disable the modules (include /etc/nginx/modules-enabled/*.conf;) by putting a # infront or it.

sudo nano /etc/nginx/nginx.conf

So the configuration will look like this.

user www-data;
worker_processes auto;
pid /run/nginx.pid;
#include /etc/nginx/modules-enabled/*.conf;

Save the file and now you can restart Nginx.

sudo service nginx restart

Step 8: Test PageSpeed

To test the pagespeed make a request to your domain name using the curl command.

curl -I -X GET yourdomainname.com

You will get a result similar to the one below.

HTTP/2 200
server: nginx/1.17.2
content-type: text/html
vary: Accept-Encoding
date: Fri, 26 Jul 2019 12:00:42 GMT
x-page-speed: 1.13.35.2-0
cache-control: max-age=0, no-cache

The X-Page-Speed:(version) means PageSpeed is active and optimizing your website.

Conclusion

Now you have learned how to install and configure PageSpeed with Nginx on Google Cloud Platform.

Please feel free to post your issues at the comments box below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Where Writing Meets Entrepreneurship!

Get FREE Google Cloud Tutorials.
GET NOW
close-link
Please help us get 1000 likes in Facebook. Thank You!
LIKE NOW

Stay Connected!

Get latest tutorials on Google Cloud and Open Source topics
SUBSCRIBE
close-link