Canadahost Official Blog

Install and configure pagespeed on Nginx on CentOS 7

Pagespeed is a module in Nginx which can be used for optimizing your site automatically through reduction of resources which consequently leads to a reduction of site loading time. The objective, therefore, is to boost the page loading speed. In this article, we’ll guide you on how to install and configure pagespeed module in Nginx. Since Nginx does not support dynamic loading of modules unlike other web browsers like Apache, We are going to build Nginx from source so that we can add the module.

To install and configure pagespeed, we first need to have root access to our CentOS server. Next, we need to follow the procedure as outlined below

Downloading Source and all the necessary Dependencies

First, down the prerequisite packages that you’ll need

sudo yum install wget curl unzip gcc-c++ pcre-devel zlib-devel

Create a folder in your home directory in which you are going to download source packages for Nginx.

mkdir ~/custom-nginx

cd to the newly created directory:

cd ~/custom-nginx

Next, while still in the directory, download the Nginx source package using the command below

 wget http://nginx.org/download/nginx-1.9.15.tar.gz

After the download is complete, extract the Nginx tarball using the command below.

tar zxvf nginx-1.9.15.tar.gz

To verify that we are on the same page, run the ls command to list the folder’s contents.

ls

The expected output should look like this:

nginx-1.9.15  nginx-1.9.15.tar.gz

At the time of penning down this tutorial, the latest Nginx version is nginx-1.9.15
To start adding the ngx_pagespeed module, navigate to the modules folder within the extracted folder nginx-1.9.15:

cd nginx-1.9.15/src/http/modules/

In this directory, download the latest ngx_pagespeed source archive from its Github repository with the command:

wget https://github.com/pagespeed/ngx_pagespeed/archive/master.zip

Upon successful completion of the download, extract it with the unzip utility like this:

unzip master.zip

This creates a new directory labeled incubator-ngx_pagespeed-master inside the ~/custom-nginx/nginx-1.9.15/src/http/modules/ path.
For convenience sake, rename this directory to just ngx_pagespeed with the command:

mv incubator-ngx_pagespeed-master ngx_pagespeed

cd to the new ngx_pagespeed directory:

cd ngx_pagespeed

Once inside, download the PageSpeed Optimization Libraries (psol) which are necessary for the compilation of source package

 wget https://dl.google.com/dl/page-speed/psol/1.9.32.6.tar.gz

Finally, extract the psol package inside the ~/custom-nginx/nginx-1.4.6/debian/modules/ngx_pagespeed directory

sudo tar -xzvf 1.9.32.6.tar.gz

Configuration and compilation of Source

Next, we are going to compile and configure the source

Navigate to the parent directory of the Nginx source:

cd custom-nginx/ nginx-1.9.15

Configure the source using the command as shown

./configure --add-module=/home/your_user/custom-nginx/nginx-1.8.0/src/http/modules/ngx_pagespeed/ --user=nobody --group=nobody --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid

Be sure to replace the your_user attribute with the username in this path.

Once configuration completes successfully, compile using the following command

  make

Next, install the software

 make install

The custom Nginx is installed in the directory /usr/local/nginx For convenience, we are going to create two symbolic links. First for the configuration files:

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

The command allows you to locate the configuration files in the /etc/nginx/ directory
Create a symbolic link to the main binary in the /usr/sbin/ directory for easy location and so that you can include it in the startup script. This will be also be required for the startup script later.

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

Creation of the startup script

Fortunately, For CentOS 7 , a startup script is readi;y available on the

Create /etc/init.d/nginx file

vim /etc/init.d/nginx

Copy the script mentioned earlier into the new directory and save the file

Make the script executable using the command below

chmod +x /etc/init.d/nginx

Afterward, start nginx with the command below

systemctl start nginx

To enable nginx on bootup

systemctl enable nginx

Enabling pagespeed Module

The next step is enabling the ngx_pagespeed module.

Before enabling the module, to create a folder, where it will cache the files for your website:

mkdir -p /var/ngx_pagespeed_cache

Remember to change the ownership of this folder to the Nginx user so that the web server can store files in it:

chown -R nobody:nobody /var/ngx_pagespeed_cache

Next, open the main Nginx configuration file nginx.conf in your favorite text editor like this:

vim /etc/nginx/nginx.conf
In this file add the following lines to the server block and save the changes:

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

install and configure pagespeed

Save and Exit

Restart Nginx

systemctl restart nginx

Testing the installation

To check if the ngx_pagespeed module was successfully installed, run the command below

/usr/sbin/nginx -V

Output

nginx version: nginx/1.9.15
...
configure arguments: --add-module=/home/your_user/custom-nginx/nginx-1.8.0/src/http/modules/ngx_pagespeed/
...

To confirm that pagespeed is enabled run the command below

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

You should see the output below as an indicator that pagespeed works as expected.

Output

X-Page-Speed: 1.9.32.6-7321

Wrapping up

In this article, we have seen how you can install and configure pagespeed. Note that you’ll have to maintain and re-install these packages by yourself when a new version is rolled out.

Installation and Securing of Memcached on CentOS 7
Enable GZIP Compression

Canadahost

Canadian Web Hosting Service Provider, Domain Names, Canadian Network, Canadian Servers and Canadian IP Address.

Add comment