Skip to content

Commit 1e17302

Browse files
authored
[UPDATE] Nginx with Pagespeed (linode#965)
[UPDATE] Nginx with Pagespeed
1 parent 45f3c2d commit 1e17302

File tree

2 files changed

+152
-164
lines changed

2 files changed

+152
-164
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
author:
3+
name: Linode Community
4+
email: docs@linode.com
5+
description: 'PageSpeed is an open source Google project created to optimize website performance. Learn how to set up PageSpeed for Nginx.'
6+
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
7+
alias: ['websites/nginx/nginx-with-pagespeed-on-ubuntu-14-04/','web-servers/nginx/nginx-with-pagespeed-on-ubuntu-14-04/ ', 'web-servers/nginx/nginx-with-pagespeed-on-ubuntu-16-04/']
8+
published: 'Tuesday, November 3rd, 2015'
9+
modified: Thursday, September 14th, 2017
10+
modified_by:
11+
name: Linode
12+
title: 'Install Nginx ngx_pagespeed Module on Ubuntu 16.04'
13+
contributor:
14+
name: Vaibhav Rajput
15+
link: https://twitter.com/rootaux
16+
external_resources:
17+
- '[Google PageSpeed Filter Docs](https://developers.google.com/speed/pagespeed/module/config_filters)'
18+
- '[Pagespeed Filter Examples](https://www.ngxpagespeed.com/)'
19+
---
20+
21+
*This is a Linode Community guide. Write for us and earn $300 per published guide.*
22+
<hr>
23+
24+
Pagespeed is a tool built by Google that boosts the speed and performance of a website by automatically minifying assets (such as CSS, Javascript, and images), and applying other web performance best practices.
25+
26+
This guide will show you how to use Pagespeed with Nginx to reduce the page load times of your website, using the [ngx_pagespeed](https://developers.google.com/speed/pagespeed/module/) module.
27+
28+
## Before You Begin
29+
30+
1. Familiarize yourself with our [Getting Started](/docs/getting-started) guide and complete the steps for setting your Linode's hostname and timezone.
31+
32+
2. This guide will use `sudo` wherever possible. Complete the sections of our [Securing Your Server](/docs/security/securing-your-server) to create a standard user account, harden SSH access and remove unnecessary network services.
33+
34+
3. Update your system:
35+
36+
sudo apt-get update && sudo apt-get upgrade
37+
38+
## Install ngx_pagespeed
39+
40+
Download and install the `ngx_pagespeed` module and its dependencies.
41+
42+
1. Install the required dependencies:
43+
44+
sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
45+
46+
2. Make sure you are in the home directory:
47+
48+
cd
49+
50+
3. Define the version of `ngx_pagespeed` to be installed :
51+
52+
NPS_VERSION=1.12.34.2-stable
53+
54+
{:.note}
55+
> This guide uses the current stable version as of this writing. However, you can check
56+
> the [ngx_pagespeed release notes](https://www.modpagespeed.com/doc/release_notes) and update this command with the most recent version.
57+
58+
4. Download the module source code:
59+
60+
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
61+
62+
5. Extract the file and move to the module's directory:
63+
64+
unzip v${NPS_VERSION}.zip
65+
cd ngx_pagespeed-${NPS_VERSION}
66+
67+
6. Pagespeed also requires some additional files. Download and unzip them:
68+
69+
NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
70+
psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz [ -e scripts/format_binary_url.sh ]
71+
psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
72+
wget ${psol_url}
73+
tar -xzvf $(basename ${psol_url})
74+
75+
##Download and build Nginx
76+
77+
Now compile Nginx with the `ngx_pagespeed` module.
78+
79+
1. Move back to your home directory:
80+
81+
cd
82+
83+
2. Define the version of Nginx which will be used, so that you won't have to write it repeatedly. At the time of this writing, the current stable version of Nginx is 1.12.1:
84+
85+
NGINX_VERSION=1.12.1
86+
87+
3. Download Nginx source from its official website using `wget`:
88+
89+
cd
90+
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
91+
92+
4. Extract the Nginx source:
93+
94+
tar -xvzf nginx-${NGINX_VERSION}.tar.gz
95+
96+
5. Move to the Nginx directory:
97+
98+
cd nginx-${NGINX_VERSION}/
99+
100+
6. Configure Nginx to include the PageSpeed module:
101+
102+
./configure --add-module=$HOME/ngx_pagespeed-${NPS_VERSION} ${PS_NGX_EXTRA_FLAGS}
103+
104+
7. Build and install Nginx:
105+
106+
make
107+
sudo make install
108+
109+
## Configuring Nginx with ngx_pagespeed
110+
111+
1. Pagespeed requires a new directory where it can store the cache of minified CSS and javascript. Create this directory if it doesn't already exist:
112+
113+
sudo mkdir /var/ngx_pagespeed_cache
114+
115+
2. Change the ownership of the directory so that the webserver can write to it:
116+
117+
sudo chown www-data:www-data /var/ngx_pagespeed_cache
118+
119+
3. Open `/usr/local/nginx/conf/nginx.conf` and add the following code to the server block where you want to enable the PageSpeed module:
120+
121+
{: .file-excerpt}
122+
/usr/local/nginx/conf/nginx.conf
123+
: ~~~ conf
124+
pagespeed on;
125+
pagespeed FileCachePath /var/ngx_pagespeed_cache;
126+
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
127+
add_header "" "";
128+
}
129+
location ~ "^/pagespeed_static/" { }
130+
location ~ "^/ngx_pagespeed_beacon$" { }
131+
~~~
132+
133+
Also make sure that Nginx is running as `www-data`. In the top of the `conf` file, uncomment `user` and replace `nobody` with `www-data`.
134+
135+
## Starting Nginx
136+
Once everything is configured correctly, start the web server.
137+
138+
1. To start the web server:
139+
140+
sudo /usr/local/nginx/sbin/nginx
141+
142+
2. To stop the web server:
143+
144+
sudo /usr/local/nginx/sbin/nginx -s stop
145+
146+
## Check Your Installation
147+
148+
You may want to check if the module is working before deploying the application:
149+
150+
curl -I localhost:80/
151+
152+
You should see something like `X-Page-Speed: 1.9.32.6` in the response. Congratulations, you have successfully installed `ngx_pagespeed` on your Linode.

docs/web-servers/nginx/nginx-with-pagespeed-on-ubuntu-14-04.md

Lines changed: 0 additions & 164 deletions
This file was deleted.

0 commit comments

Comments
 (0)