Page 1 of 1

[HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Tue Sep 11, 2018 5:06 pm
by xorro
Introduction

PageSpeed is one of the Nginx’s modules developed by Google to speed up the web site or web applications response time, it can automatically optimize the returned HTML to reduce the page load time. PageSpeed has the ability of image optimization using stripping meta-data, dynamic resizing and recompression.
Some of the main benefits of PageSpeed are listed below:
  • CSS & JavaScript minification, concatenation, inlining, and outlining.
  • Small resource inlining.
  • HTML rewriting.
  • Cache lifetime extension.
We are assuming that you have root permission, otherwise, you may start commands with “sudo”.

Image

Install Dependencies

First of all, we need to install some dependencies because we are going to compile Nginx from the source, so execute the following command to install all of the needed dependencies:

Code: Select all

yum install gcc cmake unzip wget gcc-c++ pcre-devel zlib-devel
Download and compile Nginx with Ngx_pagespeed

To install Nginx with “pagespeed” module you need to compile Nginx from the source but first, you have to download it using the following command:

Code: Select all

wget http://nginx.org/download/nginx-1.12.0.tar.gz
Then download the Ngx_pagespeed source as well:

Code: Select all

wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-stable.zip
Now that both needed sources are downloaded, extract them with the commands below:

Code: Select all

tar xvzf nginx-1.12.0.tar.gz

unzip v1.12.34.2-stable.zip
You will also need to download the PageSpeed Optimization Libraries to compile Nginx, execute the commands below one by one to get it download and extract it in the preferred route:

Code: Select all

cd ngx_pagespeed-1.12.34.2-stable

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

tar -xvzf 1.12.34.2-x64.tar.gz
Now we can start compiling Nginx with Ngx_pagespeed module.

First switch to the main directory of the Nginx’s source:

Code: Select all

cd ~/nginx-1.12.0
Run the following command to start compiling it:

Code: Select all

./configure --add-module=$HOME/ngx_pagespeed-1.12.34.2-stable --user=nobody --group=nobody --pid-path=/var/run/nginx.pid
Once the configuration is completed, compile the Nginx by the following command:

Code: Select all

make && make install
The process will take a few minutes.

When the installation process is finished you have to create Nginx symlinks:

Code: Select all

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

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
Create Nginx Systemd service

You will need to create a systemd service for Nginx in order to “start, stop and make your service run as startup.

Create a file named “nginx.service” with your text editor in the following path:

Code: Select all

nano /lib/systemd/system/nginx.service
Paste the following lines in the file then save and exit:

Code: Select all

[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
Run the following command to take effect:

Code: Select all

systemctl daemon-reload
Now you can start your Nginx and make it run as a startup service:

Code: Select all

systemctl start nginx

systemctl enable nginx
Configure Nginx

Now, Nginx is installed and has the PageSpeed support, but the module is disabled by default but before enabling it we need to create a cache directory for PageSpeed and also assign ownership for it to Nginx.

Code: Select all

mkdir -p /var/ngx_pagespeed_cache

chown -R nobody:nobody /var/ngx_pagespeed_cache
Next, you need to add some lines in the Nginx configuration. Open the “nginx.conf” with your text editor using the command below:

Code: Select all

nano /etc/nginx/nginx.conf
Add the following lines in the “server” directive:

Code: Select all

# 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" { }
Test your configuration with the command below:

Code: Select all

nginx -t
You should see the following output:

Code: Select all

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
Restart Nginx service to take effect:

Code: Select all

systemctl restart nginx
Test if PageSpeed is working

You can easily test your PageSpeed module using “curl” command:

Code: Select all

curl -I -p localhost
You should see the red line in your output:

Code: Select all

HTTP/1.1 200 OK
Server: nginx/1.12.0
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
Date: Sat, 22 Jul 2017 11:17:53 GMT
X-Page-Speed: 1.12.34.2-0
Cache-Control: max-age=0, no-cache

Re: [HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Wed Sep 12, 2018 12:17 pm
by Stesh
Why not use rpmbuild?

Re: [HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Thu Sep 13, 2018 12:25 am
by grayfolk
More simplest way:

Code: Select all

bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
     --nginx-version latest
https://www.modpagespeed.com/doc/build_ ... rom_source

:)

Re: [HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Fri Feb 08, 2019 2:18 pm
by maxmedeiros
Hi!

I was following step by step. On "make && make install" command I got this:

make: *** No rule to make target `build', needed by `default'. Stop.

Any idea what does it mean?

Thanks!

Re: [HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Fri Feb 08, 2019 2:26 pm
by maxmedeiros
maxmedeiros wrote:
Fri Feb 08, 2019 2:18 pm
Hi!

I was following step by step. On "make && make install" command I got this:

make: *** No rule to make target `build', needed by `default'. Stop.

Any idea what does it mean?

Thanks!
I got it! The name of the folder extracted was wrong. The correct were "incubator-pagespeed-ngx-1.12.34.2-stable". So, the command is this

Code: Select all

./configure --add-module=$HOME/incubator-pagespeed-ngx-1.12.34.2-stable --user=nobody --group=nobody --pid-path=/var/run/nginx.pid

Re: [HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Fri Feb 08, 2019 2:38 pm
by maxmedeiros
When I run "nginx -t" I got this

nginx: [emerg] unknown directive "pagespeed" in /etc/nginx/nginx.conf:135
nginx: configuration file /etc/nginx/nginx.conf test failed

Where exactly, in nginx.conf, I have to put this code:

Code: Select all

# 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" { }
Bellow witch line?

Thanks!

Re: [HowTo] install Nginx with Google PageSpeed module (ngx_pagespeed) on CentOS 7

Posted: Thu Nov 07, 2019 7:57 am
by sandy