Question

Best PPA for Nginx with Google Pagespeed

I’ve been trying to install PageSpeed on an existing Nginx server for the last 12 hrs with no luck, I keep running into different errors and having to read over and again to solve them. This is my first VPS

But I understand that some PPAs have this inbuilt. I don’t mind starting a new server to have this installed from the scratch and rebuild WP on it. I know that Easyengine had it but took Pagespeed out in Ver 3.6

Any other PPA suggestions will be welcome

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@ososoba

Before We Get Started

This is meant to be a copy and paste command, meaning you copy it from top to bottom and paste it to the CLI, then hit enter and let it run its course.

This installs NGINX w/Pagespeed, PHP 7.1, and MariaDB, but it will take a little bit of time to complete, so be patient; source compiles take time.

I’ve provided configuration samples for NGINX and your first Server Block below the command with a few details. This should get you started with a working build environment. You’ll still need to create your database(s), upload your PHP files, etc.

Finally … this is a long post. Take your time :-).

The One-Liner

apt-get update \
&& apt-get -y upgrade \
&& apt-get -y install autoconf automake bc bison build-essential ccache cmake curl dh-systemd flex gcc geoip-bin google-perftools g++ icu-devtools libacl1-dev libbz2-dev libcap-ng-dev libcap-ng-utils libcurl4-openssl-dev libdmalloc-dev libenchant-dev libevent-dev libexpat1-dev libfontconfig1-dev libfreetype6-dev libgd-dev libgeoip-dev libghc-iconv-dev libgmp-dev libgoogle-perftools-dev libice-dev libice6 libicu-dev libjbig-dev libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev libluajit-5.1-2 libluajit-5.1-common libluajit-5.1-dev liblzma-dev libmhash-dev libmhash2 libmm-dev libncurses5-dev libnspr4-dev libpam0g-dev libpcre3 libpcre3-dev libperl-dev libpng-dev libpng12-dev libpthread-stubs0-dev libreadline-dev libselinux1-dev libsm-dev libsm6 libssl-dev libtidy-dev libtiff5-dev libtiffxx5 libunbound-dev libvpx-dev libvpx3 libwebp-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxml2-dev libxpm-dev libxslt1-dev libxt-dev libxt6 make nano perl pkg-config software-properties-common systemtap-sdt-dev unzip webp wget xtrans-dev zip zlib1g-dev zlibc \
&& add-apt-repository -y ppa:ondrej/php \
&& apt-get update \
&& apt-get -y install php7.1-cli php7.1-dev php7.1-fpm php7.1-bcmath php7.1-bz2 php7.1-common php7.1-curl php7.1-gd php7.1-gmp php7.1-imap php7.1-intl php7.1-json php7.1-mbstring php7.1-mysql php7.1-readline php7.1-recode php7.1-soap php7.1-sqlite3 php7.1-xml php7.1-xmlrpc php7.1-zip php7.1-opcache php7.1-xsl php-yaml \
&& mkdir -p /usr/local/src/packages/{modules,nginx,openssl,pcre,zlib} \
&& mkdir -p /etc/nginx/{cache/{client,fastcgi,proxy,uwsgi,scgi},config/php,lock,logs,modules,pid,sites,ssl} \
&& useradd -d /etc/nginx nginx \
&& cd /usr/local/src/packages/nginx \
&& wget https://nginx.org/download/nginx-1.11.10.tar.gz \
&& tar xvf nginx-1.11.10.tar.gz --strip-components=1 \
&& cd /usr/local/src/packages/openssl \
&& wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz \
&& tar xvf openssl-1.1.0e.tar.gz --strip-components=1 \
&& cd /usr/local/src/packages/pcre \
&& wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz \
&& tar xvf pcre-8.40.tar.gz --strip-components=1 \
&& cd /usr/local/src/packages/zlib \
&& wget http://www.zlib.net/zlib-1.2.11.tar.gz \
&& tar xvf zlib-1.2.11.tar.gz --strip-components=1 \
&& cd /usr/local/src/packages/modules \
&& wget https://github.com/openresty/headers-more-nginx-module/archive/v0.32.tar.gz \
&& tar zxf v0.32.tar.gz \
&& wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz \
&& tar zxf v0.3.0.tar.gz \
&& wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz \
&& tar zxf 2.3.tar.gz \
&& NPS_VERSION="1.12.34.2" \
&& cd /usr/local/src/packages/modules/ \
&& wget https://github.com/pagespeed/ngx_pagespeed/archive/v${NPS_VERSION}-beta.zip \
&& unzip v${NPS_VERSION}-beta.zip \
&& cd /usr/local/src/packages/modules/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 /usr/local/src/packages/nginx \
&& ./configure --prefix=/etc/nginx \
               --sbin-path=/usr/sbin/nginx \
               --conf-path=/etc/nginx/config/nginx.conf \
               --lock-path=/etc/nginx/lock/nginx.lock \
               --pid-path=/etc/nginx/pid/nginx.pid \
               --error-log-path=/etc/nginx/logs/error.log \
               --http-log-path=/etc/nginx/logs/access.log \
               --http-client-body-temp-path=/etc/nginx/cache/client \
               --http-proxy-temp-path=/etc/nginx/cache/proxy \
               --http-fastcgi-temp-path=/etc/nginx/cache/fastcgi \
               --http-uwsgi-temp-path=/etc/nginx/cache/uwsgi \
               --http-scgi-temp-path=/etc/nginx/cache/scgi \
               --user=nginx \
               --group=nginx \
               --with-poll_module \
               --with-threads \
               --with-file-aio \
               --with-http_ssl_module \
               --with-http_v2_module \
               --with-http_realip_module \
               --with-http_addition_module \
               --with-http_xslt_module \
               --with-http_image_filter_module \
               --with-http_sub_module \
               --with-http_dav_module \
               --with-http_flv_module \
               --with-http_mp4_module \
               --with-http_gunzip_module \
               --with-http_gzip_static_module \
               --with-http_auth_request_module \
               --with-http_random_index_module \
               --with-http_secure_link_module \
               --with-http_degradation_module \
               --with-http_slice_module \
               --with-http_stub_status_module \
               --with-stream \
               --with-stream_ssl_module \
               --with-stream_realip_module \
               --with-stream_geoip_module \
               --with-stream_ssl_preread_module \
               --with-google_perftools_module \
               --with-pcre=/usr/local/src/packages/pcre \
               --with-pcre-jit \
               --with-zlib=/usr/local/src/packages/zlib \
               --with-openssl=/usr/local/src/packages/openssl \
               --add-module=/usr/local/src/packages/modules/ngx_devel_kit-0.3.0 \
               --add-module=/usr/local/src/packages/modules/headers-more-nginx-module-0.32 \
               --add-module=/usr/local/src/packages/modules/ngx_cache_purge-2.3 \
               --add-module=/usr/local/src/packages/modules/ngx_pagespeed-1.12.34.2-beta \
&& make \
&& make install

Once the above completes, we need to run the following, which will delete our existing NGINX config and files we don’t need.

rm -rf /etc/nginx/config/nginx.conf \
&& rm -rf /etc/nginx/config/*.default

Now, we’ll implement our actual NGINX configuration by creating a new NGINX configuration file.

nano /etc/nginx/config/nginx.conf

and paste in:

user                                                    nginx nginx;
worker_processes                                        1;
worker_priority                                         -10;

worker_rlimit_nofile                                    260000;
timer_resolution                                        100ms;

pcre_jit                                                on;

events {
    worker_connections                                  10000;
    accept_mutex                                        off;
    accept_mutex_delay                                  200ms;
    use                                                 epoll;
}


http {
    map_hash_bucket_size                                128;
    map_hash_max_size                                   4096;
    server_names_hash_bucket_size                       128;
    server_names_hash_max_size                          2048;
    variables_hash_max_size                             2048;

    index                                               index.php index.html index.htm;
    include                                             mime.types;
    default_type                                        application/octet-stream;
    charset                                             utf-8;

    sendfile                                            on;
    sendfile_max_chunk                                  512k;
    tcp_nopush                                          on;
    tcp_nodelay                                         on;
    server_tokens                                       off;
    server_name_in_redirect                             off;

    keepalive_timeout                                   5;
    keepalive_requests                                  500;
    lingering_time                                      20s;
    lingering_timeout                                   5s;
    keepalive_disable                                   msie6;

    gzip                                                on;
    gzip_vary                                           on;
    gzip_disable                                        "MSIE [1-6]\.";
    gzip_static                                         on;
    gzip_min_length                                     1400;
    gzip_buffers                                        32 8k;
    gzip_http_version                                   1.0;
    gzip_comp_level                                     5;
    gzip_proxied                                        any;
    gzip_types                                          text/plain
                                                        text/css
                                                        text/xml
                                                        application/javascript
                                                        application/x-javascript
                                                        application/xml
                                                        application/xml+rss
                                                        application/ecmascript
                                                        application/json
                                                        image/svg+xml;

    client_body_buffer_size                             256k;
    client_body_in_file_only                            off;
    client_body_timeout                                 10s;
    client_header_buffer_size                           64k;
    client_header_timeout                               5s;
    client_max_body_size                                50m;
    connection_pool_size                                512;
    directio                                            4m;
    ignore_invalid_headers                              on;
    large_client_header_buffers                         8 64k;
    output_buffers                                      8 256k;
    postpone_output                                     1460;
    proxy_temp_path                                     /etc/nginx/cache/proxy;
    request_pool_size                                   32k;
    reset_timedout_connection                           on;
    send_timeout                                        10s;
    types_hash_max_size                                 2048;

    open_file_cache                                     max=50000 inactive=60s;
    open_file_cache_valid                               120s;
    open_file_cache_min_uses                            2;
    open_file_cache_errors                              off;
    open_log_file_cache                                 max=10000 inactive=30s min_uses=2;

    include                                             /etc/nginx/sites/*.conf;
}

Now we’ll create our PHP FastCGI Params file, much like we did the above.

nano /etc/nginx/config/php/fastcgi_params.conf

and paste in:

    fastcgi_param  SCRIPT_FILENAME    $request_filename;

    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 512 16k;
    fastcgi_busy_buffers_size 1m;
    fastcgi_temp_file_write_size 4m;
    fastcgi_max_temp_file_size 4m;
    fastcgi_intercept_errors off;

    fastcgi_param  PATH_INFO          $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;
    fastcgi_param  HTTP_PROXY         "";

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    fastcgi_param  REDIRECT_STATUS    200;

Ok, now we need to create a server block for your domain, so we’ll create that now. Simply replace yourdomain.com with your actual domain.

nano /etc/nginx/sites/yourdomain.com.conf

and paste in:

server
{
    listen                          80 default_server;
    listen                          [::]:80;
    server_name                     yourdomain.com www.yourdomain.com;

    root                            /home/username/htdocs/public;

    location /
    {
        try_files $uri $uri/ =404;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass   /run/php/php7.1-fpm.sock;
        fastcgi_index  index.php;

        include /etc/nginx/config/php/fastcgi_params.conf;
    }
}

Change /home/username/htdocs/public to the actual directory where you’re installing WordPress (i.e. where the WordPress index.php file resides).

Finally, we need to make sure PHP-FPM can read/write on your files, so whichever directory you set as the root above, i.e. /home/username/htdocs/public, we need to change ownership of everything to www-data.

chown -R www-data:www-data /home/username/htdocs/public

That’ll make all files and directories owned by www-data, as they should be. Now we just need to start NGINX and restart PHP-FPM for good measure.

Type in nginx and hit enter, then:

system php7.1-fpm restart

@ososoba

You don’t specifically need a PPA as they have their own auto-installer that’s available. It’ll automate a source build of NGINX w/ ngx_pagespeed as a module. All you need to do is run:

bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version latest

… and follow the on-screen prompts.

Keep in mind, this is a very barebones installation of NGINX. If you need specific modules compiled in, you’ll need to specify those when the script prompts you.

References

https://modpagespeed.com/doc/build_ngx_pagespeed_from_source

https://modpagespeed.com/doc/configuration

http://wiki.nginx.org/InitScripts

Ive written a tutorial on this lEMP stack pagespeed mod. Hope this helps anyone needing mariadb php7 pagespeed mod nginx and letsencrypt markbarry.info

@jtittle Why does it overwrite my nginx every time I upgrade my server and there is an new nginx version. I gotta go offline every time to fix it. Anyways to stop that?

Thank you I appreciate everything you do.

Hi! @jtittle @jtittle1 ,

Is this still working? ubuntu 16.04x64 1G

cd /usr/local/src git clone https://github.com/serveradminsh/installers.git

cd installers/nginx chmod +x installer.sh ./installer.sh or cd /opt/installers/nginx chmod +x installer.sh ./installer.sh

Then copy above nginx.conf, fastcgi_params, and domain.com.conf?

I’m totally new on droplets. I tried installing ngx_pagespeed + ssl with no avail. Well, I have a working L E MariaDB P and WP, but after installing fastcgi, redis, phpredis I don’t know how to purge the cache, I don’t even know if the modules are properly working. I hope ngx_cache_purge will not give me headache.

What will be the best droplet for this kind of installation? Can I stay in ubuntu16.04 lts 1g x 64? I don’t have much traffic, just started.

Thanks

Thank you @jtittle .

Unfortunately there is somewhere a mistake.

When i just replace beta with stable then it tries to get the zip from…

https://github.com/pagespeed/ngx_pagespeed/archive/v1.11.33.4-stable.zip

…which should be…

https://github.com/pagespeed/ngx_pagespeed/archive/latest-stable.zip

And when i adjust the code like so…

line 57:

pagespeedVers="1.11.33.4"

from line 126:

    cd /usr/local/src/github \
    && wget https://github.com/pagespeed/ngx_pagespeed/archive/latest-stable.zip \
    && unzip ngx_pagespeed-latest-stable.zip \
    && cd ngx_pagespeed-latest-stable \
    && export psol_url=https://dl.google.com/dl/page-speed/psol/1.11.33.4.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})

and on line 232:

--add-module=/usr/local/src/github/ngx_pagespeed-latest-stable \

…i get:

./auto/configure: error: no /usr/local/src/github/ngx_pagespeed-latest-stable/config was found

:-(

Any idea what’s going wrong here?

Hi @jtittle,

unfortunately i am having some issues with the beta version of pagespeed which is compiled with nginx in your installer.sh. When changing the pagespeedVers in line 57 to 1.11.33.4 i get the error: ngx_pagespeed: pagespeed optimization library not found.

How can we change the installer.sh so the latest stable version of pagespeed is installed rather then the beta version?

Thank you again for your time and help.

Best regards nolybom

Hi @jtittle, please don’t let this project die.

@nolybom

You’ll need to install the PHP packages that you require for your website(s). The apt-cache command only searches for packages – it won’t install them for you.

You’ll need to choose which packages you want to install and use apt or apt-get to install.

i.e.

apt -y install php7.1-fpm php7.1-json php7.1-gd

etc.

You’d repeat this for each version that you wish to install.

@jtittle

WOW! This is by far the most reliable and amazing approach of setting up an nginx server…and trust me…i am looking around for a while.

I have followed each step and must admit that i struggled a little when setting up pagespeed and naxsi because of no documentation, but found solutions thru the web.

I can provide some handpicked pagespeed-configs if you’d like to integrate them in your examples. Let me know if that’s interesting.

My next step is to install different php versions (5.6, 7,0 7.1) and be able to switch between them in server blocks. Any idea how to achieve that without facing big troubles?

HHVM is also worth noticing and might be a good idea to integrate in your installers. What do you think?