fbpx

NGINX / Server Setup

Install NGINX with ngx_pagespeed (Google PageSpeed) dynamic module in CentOS/RHEL, Amazon Linux and Fedora Linux

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

You can use our repository to easily install the RPM package for the dynamic PageSpeed module for NGINX (ngx_pagespeed).
The dynamic module is compatible with the latest stable or mainline NGINX for maintained releases of CentOS/RHEL, Amazon Linux, and Fedora Linux.

Step 1. Add GetPageSpeed extras YUM repository

First, run the following command to add our repository:

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm

Step 2. Install nginx and ngx_pagespeed

Then, all you have to do to install NGINX and the PageSpeed module:

sudo yum -y install nginx nginx-module-pagespeed

Follow the installation prompt to import GPG public key that is used for verifying packages.

Step 3. Enable the module

Next, enable your NGINX to load PageSpeed dynamic module by editing its configuration.
Follow the installer’s suggestion:

----------------------------------------------------------------------

The PageSpeed dynamic module for nginx has been installed.
To enable this module, add the following to /etc/nginx/nginx.conf
and reload nginx:

    load_module modules/ngx_pagespeed.so;

Please refer to the module documentation for further details:
https://developers.google.com/speed/pagespeed/module/configuration

----------------------------------------------------------------------

3.1. Configure Pagespeed cache backend

PageSpeed module needs to store its optimized resources somewhere. This is called the cache backend.

Typically, a file-based cache backend is enough, provided that you have SSD. Put the following line within http { ... } section of nginx.conf for file-based cache:

pagespeed FileCachePath /var/cache/pagespeed; 

3.2. Enable Pagespeed for a website

Finally, enable PageSpeed for a specific website(s).

In every server block where PageSpeed needs to be enabled, add:

pagespeed on;

# 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 ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

That’s it!

Your complete nginx.conf may look like this:

# ... other directives ...   

load_module modules/ngx_pagespeed.so;

http {

    pagespeed FileCachePath /var/cache/pagespeed; 
    pagespeed SslCertDirectory /etc/pki/tls/certs;
    pagespeed SslCertFile /etc/pki/tls/cert.pem;

    # Enables pagespeed for each website:
    pagespeed on; 

    # ... other directives ...

    server {

        # ... other directives ...

        # 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 ~ "^/pagespeed_static/" { }
        location ~ "^/ngx_pagespeed_beacon$" { }             
    }
}

SELinux compatibility

A dependency SELinux module built for NGINX PageSpeed will be automatically installed for you at the same time with the PageSpeed module.

Tip: if you have SELinux enabled, you may want to check our post about SELinux configuration for nginx.

Caveats

We don’t know of any, at this time.

Alternatives?

You can optimize your website’s client-side performance without ngx_pagespeed if you put some effort into it.

  1. Alkin Veysal Fehim

    Processing Dependency: nginx = 1:1.10.1-1.el7.ngx for package: 1:nginx-module-pagespeed-1.10.1.1.11.33.4-1.el7.ngx.x86_64

    Reply
    • Danila Vershinin

      We have moved repository and building ngx_pagespeed for latest nginx version 1.10.3. Let me know if this works for you (see updated instructions in article).

      Reply
      • Alkin Veysal Fehim

        Updated:
        nginx-module-pagespeed.x86_64 1:1.11.33.4-1.el7.gps

        Complete!

        Reply
  2. John

    Could not find ngx_pagespeed in nginx -V

    Reply
    • Danila Vershinin

      Hi John,

      It’s not supposed to be in nginx -V output. The configure arguments you see there show how the stable Nginx was configured. Obviously it has no knowledge of dynamic modules built for it later on. In other words, with nginx -V, you will see only dynamic modules that were built at exact same time with Nginx.

      Reply
  3. sahil kataria

    Hi Danila,

    Nginx_pagespeed installed and loaded module with nginx but it’s not caching in /var/cache/pagespeed/

    Reply
    • Danila Vershinin

      Have you at minimum enabled the module in a server block? pagespeed on;

      Reply
  4. ade pondra

    Hi Danila,

    installation nginx-modul-nps getting error “Error: Package: 1:nginx-module-pagespeed-1.12.0.1.11.33.4-1.el7.gps.x86_64 (getpagespeed-extras-x86_64)” how to solve this?

    Reply
  5. Danila Vershinin

    Which version of nginx you have? Please post output of yum list installed | grep nginx.
    You have to be using official stable Nginx (not mainline). Our builds are compatible with either nginx 1.10.3 or 1.12.0 stable.

    Reply
    • ade pondra

      i already use nginx 1.10.3. This the output :

      nginx.x86_64 1:1.10.3-1.el7.ngx @getpagespeed-extras-x86_64
      nginx-filesystem.noarch 1:1.10.2-1.el7 @epel

      Reply
    • ade pondra

      i already used nginx 1.10.3 and then i tried to use nginx 1.12.0 for instalation of nginx-module-pagespeed from your repo. this is the output of yum list installed :

      nginx.x86_64 1:1.10.3-1.el7.ngx @getpagespeed-extras-x86_64
      nginx-filesystem.noarch 1:1.10.2-1.el7 @epel

      Reply
      • Danila Vershinin

        Please email me (address at the top). The error ““Error: Package: 1:nginx-module-pagespeed-1.12.0.1.11.33.4-1.el7.gps.x86_64 (getpagespeed-extras-x86_64)” seems incomplete and I can’t understand what is the problem unless all details are provided)

        Reply
        • ade pondra

          After i remove and install nginx again, i can installed the “nginx-module-pagespeed”, but when i try to add “load_module modules/ngx_pagespeed.so” to /etc/nginx/nginx.conf under http. Ningx is not allowed “load_module”. This is the error messages:

          nginx: [emerg] “load_module” directive is not allowed here in /etc/nginx/nginx.conf:29
          nginx: configuration file /etc/nginx/nginx.conf test failed

          Where should i put it? i’m sorry about my bad english 🙂

          Reply
          • Danila Vershinin

            It should go outside http block (I think it’s safe to put it at the very top of nginx.conf.

  6. frankie

    Processing Dependency: nginx = 1:1.12.0-1.el7.ngx for package: 1:nginx-module-pagespeed-1.12.0.1.11.33.4-1.el7.gps.x86_64

    Reply
  7. CJ

    Is there a Debian version?

    Reply
    • Danila Vershinin

      No Debian versions at this time. We only work with RedHat based distributions.

      Reply
  8. KILLER

    yum list installed | grep nginx
    nginx.x86_64                     1:1.12.1-1.el7.ngx                  @nginx
    nginx-module-pagespeed.x86_64          1:1.12.1.1.12.34.2-1.el7.gps        @getpagespeed-extras-x86_64
    

    when start nginx:

    nginx: [emerg] dlopen() "/etc/nginx/modules/ngx_pagespeed.so" failed (/etc/nginx/modules/ngx_pagespeed.so: cannot enable executable stack as shared object requires: Permission denied) in /etc/nginx/nginx.conf:1
    
    Reply
    • Danila Vershinin

      Apparently you have SELinux enabled. So you will need to run:

      setsebool -P httpd_execmem on
      
      Reply
  9. Boris Günthner

    There seems to be a version conflict with the latest nginx and pagespeed module:

    Fehler: Paket: 1:nginx-module-pagespeed-1.12.2.1.12.34.3-1.el7.gps.x86_64 (getpagespeed-extras-x86_64)
                Benötigt: nginx = 1:1.12.2-1.el7.ngx
                Installiert: 1:nginx-1.12.1-1.el7.ngx.x86_64 (@nginx)
                    nginx = 1:1.12.1-1.el7.ngx
    

    Sorry that it is in german, I can translate this if you want to, but you should be able to read the error anyway

    Reply
    • Danila Vershinin

      Hi Boris,

      It appears that in the new nginx 1.12.2 build the packages are branched out (different RPMs) between RedHat 7 < 7.4 and RedHat >= 7.4, thus the issue.
      In your current situation, you should be able to install the module without upgrading to latest nginx:

      yum install  nginx-module-pagespeed-1.12.1.1.12.34.2
      

      I’ll rebuild the packages when I have the time, to follow new convention. Then subsequent upgrade to 1.12.2 will work.

      Reply
  10. Aaron Kili

    Hello,

    I have followed this guide and installed Pagespeed successfully. I also enabled the module in a server block? using pagespeed on; but nothing seems to be working.

    After running curl -I -p http://localhost, i don’t see X-Page-Speed header in server headers.

    Reply
    • Danila Vershinin

      Hi Aaron,

      The X-Page-Speed header would only show up for a server block where you have pagespeed on;. Make sure that it’s specified for localhost server block (as per your test).
      The module installation went fine, so I suggest you to post issue to ngx_pagespeed upstream and look at its documentation.

      Reply
  11. Apurva Pandya

    Hello

    I installed this and also loaded the module successfully, but after adding pagespeed on; directive I am getting following error

    Starting nginx - high performance web server...
    nginx[13914]: nginx: [warn] "ssl_stapling" ignored, issuer certificate not found for certificate "/home/new/ssl/now_biog..._com.crt"
    
    nginx[13914]: nginx: [emerg] "gzip_types" directive FileCachePath must be set in /etc/nginx/nginx.conf:38
    nginx[13914]: nginx: configuration file /etc/nginx/nginx.conf test failed
    systemd[1]: nginx.service: control process exited, code=exited status=1
    systemd[1]: Failed to start nginx - high performance web server.
    systemd[1]: Unit nginx.service entered failed state.
    systemd[1]: nginx.service failed.
    

    But without pagespeed on; directive its restarting and working perfectly without pagespeed.

    Reply
    • Danila Vershinin

      Just as the error message says, you need to set FileCachePath directive (see documentation):

      pagespeed FileCachePath /var/cache/pagespeed;
      
      Reply
  12. Santiago Hoyos

    Hi,I need help, I have a problem to install it with last version of Nginx and when i try to install with yum show this :

    ---> Package nginx-module-pagespeed.x86_64 1:1.12.1.1.12.34.2-1.el7.gps will be installed
    --> Processing Dependency: nginx = 1:1.12.1-1.el7.ngx for package: 1:nginx-module-pagespeed-1.12.1.1.12.34.2-1.el7.gps.x86_64
    --> Finished Dependency Resolution
    Error: Package: 1:nginx-module-pagespeed-1.12.1.1.12.34.2-1.el7.gps.x86_64 (getpagespeed-extras-x86_64)
               Requires: nginx = 1:1.12.1-1.el7.ngx
               Installed: 1:nginx-1.12.2-1.el7.x86_64 (@epel)
                   nginx = 1:1.12.2-1.el7
               Available: nginx1w-1.12.1-1.w7.x86_64 (webtatic)
                   nginx = 1.12.1-1.w7
    

    my version of nginx is : nginx-1.12.2-1.el7.x86_64

    Any tips to install ?

    Reply
    • Santiago Hoyos

      Hi , i solve it, I add the nginx repositori in my yum, update it and work.

      Reply
      • Danila Vershinin

        For anyone reading comments section: it is no longer required to add nginx repository, although things are compatible with it.

        Reply
  13. Apurva Pandya

    can you make an update for nginx version 1.12.2-2.el7 ?

    Reply
    • Danila Vershinin

      No, since this is package by EPEL and not coming directly from nginx. So you shouldn’t really use it. Please follow this post on how to install nginx properly. Remove currently installed nginx first (yum remove nginx).

      Reply
  14. jaime lesmes

    Hello, thanks for the article. I would like to implement it. Per I have
    nginx / 1.14.0 and CentOS 6.9, which version would apply to my system?

    Reply
    • Danila Vershinin

      You may try yum install https://extras.getpagespeed.com/release-latest.rpm then yum install nginx-module-pagespeed

      Reply
  15. Saad

    Hi,

    At step 2, i got this error:

    (1/9): getpagespeed-extras-x86_64/x86_64/group_gz | 301 B 00:00:01
    (2/9): getpagespeed-extras-noarch/primary_db | 22 kB 00:00:01
    (3/9): epel/x86_64/updateinfo | 942 kB 00:00:02
    (4/9): getpagespeed-extras-x86_64/x86_64/primary_db | 62 kB 00:00:02
    (5/9): ius/x86_64/primary_db | 380 kB 00:00:01
    (6/9): updates/7/x86_64/primary_db | 5.2 MB 00:00:01
    epel/x86_64/primary FAILED
    http://mirrors.aliyun.com/epel/7/x86_64/repodata/d1516ecd56df2d9706acbb6c3cd65d35e4b511425de311db22b8f29b7474a9d3-primary.xml.gz: [Errno 14] HTTP Error 404 – Not Found
    Trying other mirror.
    To address this issue please refer to the below wiki article

    https://wiki.centos.org/yum-errors

    If above article doesn’t help to resolve this issue please use https://bugs.centos.org/.

    (7/9): webtatic/x86_64/primary_db | 353 kB 00:00:03
    (8/9): epel/x86_64/primary | 3.6 MB 00:00:04
    (9/9): remi-safe/primary_db | 1.3 MB 00:00:07
    epel 12685/12685
    Resolving Dependencies
    –> Running transaction check
    —> Package nginx.x86_64 1:1.12.2-2.el7 will be updated
    —> Package nginx.x86_64 1:1.14.0-1.el7_4.ngx will be an update
    —> Package nginx-module-pagespeed.x86_64 1:1.14.0.1.13.35.2-1.el7_4.gps will be installed
    –> Finished Dependency Resolution

    Dependencies Resolved

    ===================================================================================

    Package Arch Version Repository Size

    Installing:
    nginx-module-pagespeed
    x86_64 1:1.14.0.1.13.35.2-1.el7_4.gps getpagespeed-extras-x86_64 8.0 M
    Updating:
    nginx x86_64 1:1.14.0-1.el7_4.ngx getpagespeed-extras-x86_64 750 k

    Transaction Summary

    Install 1 Package
    Upgrade 1 Package

    Total download size: 8.7 M

    Reply
    • Danila Vershinin

      I don’t see any error aside from failed EPEL repository server

      Reply
      • Saad

        at step 3:

        I inserted: load_module modules/ngx_pagespeed.so;
        i tried to restart and i’m getting this error:

        nginx: [emerg] module “/usr/lib64/nginx/modules/ngx_http_geoip_module.so” version 1012002 instead of 1014000 in /usr/share…eoip.conf:1
        nginx.service: control process exited, code=exited status=1
        Failed to start nginx – high performance web server.
        Unit nginx.service entered failed state.
        nginx.service failed.

        Appreciate your help.

        Reply
        • Danila Vershinin

          You need to remove webtatic repository and all packages you had from it. It’s an old conflicting repository with ancient nginx versions. Right now you have a problem with geoip module installed from webtatic.

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.