Install Nginx with Ngx_Pagespeed on Ubuntu

By on January 20th, 2020


Install Nginx with Ngx_Pagespeed on Ubuntu

Nginx is a free, open-Source web server that can be used as a reverse proxy, load balancer, TCP/UDP proxy server, and IMAP/POP3 proxy server. Nginx is an HTTP web server known for its high performance, stability, simple configuration, low resource consumption, and more. The Nginx server does not rely on threads to handle the requests. Instead, it uses event-driven architecture.

Ngx_pagespeed or just page speed is a free, open-source Nginx module. It is designed to optimize your site by improving the website speed and reducing the page load time. This page speed reduces the time taken to load any content on the website. Some of the features of Ngx_pagespeed module are:

1) This module optimizes the images by stripping the meta-data, dynamic resizing, and recompression.

2) Ngx_pagespeed module offers HTML rewriting, cache lifetime extension, small resource inlining, and more.

3) If you are using the Ngx_pagespeed module, then HTTPS is supported by the URL control.

4) This module helps in configuring multiple servers and many more.


Install Nginx with Ngx_Pagespeed

The steps to install Nginx with speed optimization on the Ubuntu server are the following.


1) Install Nginx from Source

1) First, you need to install Nginx with Ngx_pagespeed from the source using the following package.

$ sudo apt-get install build-essential zliblg-dev libpcre3 libpcre3-dev unzip

2) After the installation, you need to download source files of the Nginx latest version (1.17.5 at the time of writing). Download the files using wget and then extract those files using the tar command.

$ mkdir -p ~/make_nginx

$ cd ~/make_nginx

$ wget -c

$ tar -xzvf nginx-1.17.5.tar.gz

3) Next, you need to download the ngx_pagespeed source files using the wget command, and after the download unzip the file.

$ wget -c

$ unzip

4) Then under the unzipped ngx_pagespeed directory, to compile Nginx download the PageSpeed optimization libraries using the following commands.

$ cd ngx_pagespeed-

$ wget -c

$ tar -xvzf


2) Configure and Compile Nginx with Ngx_Pagespeed

1) First, move to the Nginx directory and configure the Nginx source file using the following commands.

$ cd ~/make_nginx/nginx-1.17.5

$ ./configure –add-module=$HOME/make_nginx/ngx_pagespeed- ${PS_NGX_EXTRA_FLAGS}

2) Next, you need to compile and install Nginx using the following command.

$ make

$ sudo make install

3) After installation, you need to create the necessary symlinks for Nginx using the following commands.

$ sudo ln -s /usr/local/nginx/conf /etc/nginx

$ sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx


3) Creating Nginx Unit File for SystemD

You need to create the Nginx unit file for systemd manually. On the latest version of the Ubuntu server, the systemd is the init system.

1) First, you need to create the nginx.service file under the /lib/systemd/system directory.

$ sudo vi /lib/systemd/system/nginx.service

You need to download the Nginx systemd service file and paste the same unit file configuration into the created file. Then save and close the file.


Description=The NGINX HTTP and reverse proxy server




ExecStartPre=/usr/sbin/nginx -t


ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID



2) Start the Nginx service for now and then enable it to start at system boot by using the following commands.

$ sudo systemctl start nginx

$ sudo systemctl enable nginx

After starting the Nginx service, if you see the following error:

systemd[1]: nginx.service: PID file /run/ not readable (yet?) after start: No such file or directory

To solve the above error, you need to append the following lines in the Nginx configuration file.

#pid logs/;


pid  /run/;

After appending the above line in the configuration file, you need to reload the daemon and start the nginx service again.

$ sudo systemctl daemon-reload

$ sudo systemctl start nginx

$ sudo systemctl status nginx


4) Configure Nginx with Ngx_Pagespeed Module

Now the Nginx service is installed and running on your system. The next step is to enable the Ngx_pagespeed module. To configure the module in the Nginx service, you can do the following.

1) First, we need to create a directory to store the website cache. Then set the proper permission for this directory.

$ sudo mkdir -p /var/ngx_pagespeed_cache

$ sudo chown -R nobody:nogroup /var/ngx_pagespeed_cache

2) Next, you need to enable the Ngx_pagespeed module. For the same, you need to open the ‘/etc/nginx/nginx.conf’ file and add the following Ngx_pagespeed configuration lines within the server block. Then save and close the file.

$ sudo vi /etc/nginx/nginx.conf

# Pagespeed main settings

pagespeed on;

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 ~ “^/ngx_pagespeed_static/” { }

location ~ “^/ngx_pagespeed_beacon” { }

3) After making the changes in the configuration file, check if the syntax of the Nginx configuration file is throwing any error by running the following command.

$ sudo nginx -t

If there is no error in the configuration file, then the above command should have the below result.

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

4) After checking the status, you need to restart the Nginx service to reflect the latest changes.

$ sudo systemctl restart nginx


5) Testing Nginx with Ngx_Pagespeed

To test if the Ngx_pagespeed is conjunctive with the Nginx service, you can run the following command.

$ curl -I -p http://localhost

If the above command fails to show the header, then we need to ensure that the configuration file is proper.


If you need any further help, please do reach our support department.

Leave a Reply