Install Nginx with Ngx_pagespeed on CentOS 7

Updated on November 21, 2023
Install Nginx with Ngx_pagespeed on CentOS 7 header image

Introduction

Ngx-pagespeed is an open source Nginx module that speeds up your site and reduces page load time. It does so by rewriting web pages to reduce latency and bandwidth. Ngx-pagespeed also provides many optimization filters that are used to optimize various files such as css, html, png, and jpg.

Prerequisites

  • A newly deployed Vultr instance running CentOS 7.
  • A sudo user.

Step 1: Update the system

Before installing any packages on your CentOS server instance, it is recommended to update the system. Login using the sudo user and run the following commands to update the system.

sudo yum -y update  
sudo reboot

Once the system has rebooted, log in again as the sudo user and continue with the next steps.

Step 2: Download dependencies

To install Nginx with ngx-pagespeed we have to compile Nginx from source. To do so we need to download some software by running the following command.

sudo yum -y install gcc-c++ pcre-devel zlib-devel make unzip

Next, we need to download the source code for Nginx and ngx-pagespeed. At the time of writing, the latest stable version of is Nginx 1.12.0 and the latest ngx-pagespeed version is 1.12.34.2.

NPS_VERSION=1.12.34.2
NGINX_VERSION=1.12.0
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
wget https://github.com/pagespeed/ngx_pagespeed/archive/v${NPS_VERSION}-beta.zip

Extract the downloaded packages.

tar -xvzf nginx-${NGINX_VERSION}.tar.gz
unzip v${NPS_VERSION}-beta.zip

Step 3: Download PageSpeed Optimization Libraries

The PageSpeed Optimization Libraries (psol) are required to compile Nginx. Download and extract them in the ngx-pagespeed source directory with the following commands.

cd ngx_pagespeed-${NPS_VERSION}-beta
psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
[ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
wget ${psol_url}
tar -xzvf $(basename ${psol_url})
cd ..

Step 4: Configure and compile Nginx

The Nginx source can now be compiled with the pagespeed module. Go to the Nginx source directory.

cd nginx-${NGINX_VERSION}

Configure the source with the following command.

./configure --add-module=$HOME/ngx_pagespeed-${NPS_VERSION}-beta --user=nobody --group=nobody --pid-path=/var/run/nginx.pid ${PS_NGX_EXTRA_FLAGS}

Once the configuration is completed, compile Nginx with the command:

sudo make

This can take several minutes. After that you can go ahead and install the software with the command:

sudo make install

Nginx has now been installed in the directory /usr/local/nginx. For convenience, we can create the following symlinks:

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

Step 5: Create Startup Script

To be able to stop and start Nginx you have to manually create a startup script. A template for this is provided by Nginx here. Copy content of the template and insert it into the /etc/init.d/nginx file using a text editor such as nano. Also set the right permissions for the file.

sudo nano /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx

You can now start and then add it to the default runlevels (so Nginx starts and stops along with your instance) with the command:

sudo service nginx start
sudo systemctl enable nginx

Step 6: Enable ngx_pagespeed

To enable ngx-pagespeed, start by creating a cache directory and assigning ownership for it to Nginx:

sudo mkdir -p /var/ngx_pagespeed_cache
sudo chown -R nobody:nobody /var/ngx_pagespeed_cache

Open the /etc/nginx/nginx.conf file:

sudo nano /etc/nginx/nginx.conf

And add the following lines within the server block:

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

Finally, restart Nginx for the changes to take effect:

sudo systemctl restart nginx

Step 6: Test

To check if ngx-pagespeed was properly installed on your server, run the following command:

curl -I -p http://localhost| grep X-Page-Speed

The outpout should looks like this:

X-Page-Speed: 1.12.34.2-0

Ngx-pagespeed is now installed on your server. If you don't get this outpout, make sure that you've enabled ngx-pagespeed as explained before.