如何在 Ubuntu 15.04 中安装 nginx 和 google pagespeed
nginx pagespeed
Nginx （engine-x）是一个开源的高性能 HTTP 服务器、反向代理和 IMAP/POP3 代理服务器。nginx 杰出的功能有：稳定、丰富的功能集、简单的配置和低资源消耗。nginx 被用于一些高性能网站并在站长之间变得越来越流行。本教程会从源码构建一个带有 google paespeed 模块的用于 Ubuntu 15.04 的 nginx .deb 安装包。


pagespeed 是一个由 google 开发的 web 服务器模块来加速网站响应时间、优化 html 和减少页面加载时间。ngx\_pagespeed 的功能如下：


* 图像优化：去除元数据、动态缩放、重压缩。
* CSS 与 JavaScript 压缩、串联、内联、外联。
* 小资源内联
* 图像与 JavaScript 延迟加载
* HTML 重写
* 缓存生命期插件


更多请见 <https://developers.google.com/speed/pagespeed/module/>。


### 前置要求


* Ubuntu Server 15.04 64位
* root 权限


本篇我们将要：


* 安装必备软件包
* 安装带 ngx\_pagespeed 的 nginx
* 测试


#### 安装必备包



```
sudo apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev

```

### 安装带 ngx\_pagespeed 的 nginx


#### 第一步 - 添加nginx仓库



```
vim /etc/apt/sources.list.d/nginx.list

```

加入下面的行：



```
deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx

```

更新仓库：



```
sudo apt-get update

```

注意：如果你看到信息：GPG error [...] NO\_PUBKEY [...] 等等


请添加key：



```
sudo sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys KEYNUMBER
sudo apt-get update

```

#### 第二步 - 从仓库下载 nginx 1.8



```
sudo su
cd ~
mkdir -p ~/new/nginx_source/
cd ~/new/nginx_source/
apt-get source nginx
apt-get build-dep nginx

```

#### 第三步 - 下载 Pagespeed



```
cd ~
mkdir -p ~/new/ngx_pagespeed/
cd ~/new/ngx_pagespeed/
ngx_version=1.9.32.3
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${ngx_version}-beta.zip
unzip release-${ngx_version}-beta.zip

cd ngx_pagespeed-release-1.9.32.3-beta/
wget https://dl.google.com/dl/page-speed/psol/${ngx_version}.tar.gz
tar -xzf 1.9.32.3.tar.gz

```

#### 第四步 - 配置 nginx 来编译 Pagespeed



```
cd ~/new/nginx_source/nginx-1.8.0/debin/
vim rules

```

在两处 CFLAGS `.configure` 下添加模块：



```
--add-module=../../ngx_pagespeed/ngx_pagespeed-release-1.9.32.3-beta \

```

![adding pagespeed to nginx](/data/attachment/album/201506/27/214218nt7v3stq8tpfof0v.png)


![adding pagespeed to nginx](/data/attachment/album/201506/27/214220gzbz44d4cez79bdb.png)


#### 第五步 - 打包 nginx 软件包并安装



```
cd ~/new/nginx_source/nginx-1.8.0/
dpkg-buildpackage -b

```

dpkg-buildpackage 会编译 ~/new/ngix\_source/ 为 nginx.deb。打包完成后，看一下目录：



```
cd ~/new/ngix_source/
ls

```

![nginx builded with pagespeed](/data/attachment/album/201506/27/214222u3k57g8bhgeuwhbr.png)


接着安装 nginx。



```
dpkg -i nginx_1.8.0-1~trusty_amd64.deb

```

![Install nginx](/data/attachment/album/201506/27/214224gr29ttctttktc4a4.png)


### 测试


运行 nginx -V 测试 nginx 是否已经自带 ngx\_pagespeed。



```
nginx -V

```

![nginx -V](/data/attachment/album/201506/27/214226y1aovlfbqwoc7mmo.png)


### 总结


稳定、快速、开源的 nginx 支持许多不同的优化模块。这其中之一是 google 开发的‘pagespeed’。不像 apache，nginx 模块不是动态加载的，因此你必须在编译之前就选择好需要的模块。




---


via: <https://www.howtoforge.com/tutorial/how-to-install-nginx-and-google-pagespeed-on-ubuntu-15-04/> 


作者：Muhammad Arul 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to Install nginx and google pagespeed on Ubuntu 15.04 (Vivid Vervet)

### This tutorial exists for these OS versions

[Ubuntu 20.04 (Focal Fossa)](/how-to-install-nginx-with-google-pagespeed-on-ubuntu-20-04/)[Ubuntu 16.04 (Xenial Xerus)](/tutorial/how-to-install-nginx-and-google-pagespeed-on-ubuntu-16-04/)**Ubuntu 15.04 (Vivid Vervet)**

### On this page

**Nginx (engine-x)** is a open source and high performance HTTP server, reverse proxy and IMAP/POP3 proxy server. The outstanding features of Nginx are: stability, rich feature set, simple configuration and low resource consumption. Nginx is being used by some of the largest websites on the internet and is gaining more and more popularity in the webmaster community. This tutorials shows how to build a nginx .deb package for Ubuntu 15.04 from source that has Google pagespeed module compiled in.

**Pagespeed** is a web server module developed by Google to speed up a website response times, optimize html and reduce the page load time. ngx_pagespeed features include :

- Image optimization: stripping meta-data, dynamic resizing, recompression.
- CSS & JavaScript minification, concatenation, inlining, and outlining.
- Small resource inlining.
- Deferring image and JavaScript loading.
- HTML rewriting.
- Cache lifetime extension.

see more [https://developers.google.com/speed/pagespeed/module/](https://developers.google.com/speed/pagespeed/module/).

## Prerequisites

- Ubuntu Server 15.04 - 64 bit
- root privileges

What we will do in this tutorial :

- Install the prerequisite packages.
- Installing nginx with ngx_pagespeed.
- Testing.

### Install the prerequisite packages

sudo apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev

### Installing nginx with ngx_pagespeed

#### Step 1 - Adding nginx repository

vim /etc/apt/sources.list.d/nginx.list

add the line:

deb http://nginx.org/packages/ubuntu/ trusty nginx deb-src http://nginx.org/packages/ubuntu/ trusty nginx

Update your repository:

sudo apt-get update

note : if you get the messege : GPG error [...] NO_PUBKEY [...] bla bla

please add the the key:

sudo sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys KEYNUMBER

sudo apt-get update

#### Step 2 - Download nginx 1.8 from ubuntu repository

sudo su

cd ~

mkdir -p ~/new/nginx_source/

cd ~/new/nginx_source/

apt-get source nginx

apt-get build-dep nginx

#### Step 3 - Download Pagespeed

cd ~

mkdir -p ~/new/ngx_pagespeed/

cd ~/new/ngx_pagespeed/

ngx_version=1.9.32.3

wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${ngx_version}-beta.zip

unzip release-${ngx_version}-beta.zip

cd ngx_pagespeed-release-1.9.32.3-beta/

wget https://dl.google.com/dl/page-speed/psol/${ngx_version}.tar.gz

tar -xzf 1.9.32.3.tar.gz

#### Step 4 - Configure nginx to build with Pagespeed

cd ~/new/nginx_source/nginx-1.8.0/debin/

vim rules

add the module under CFLAGS `.configure` :

--add-module=../../ngx_pagespeed/ngx_pagespeed-release-1.9.32.3-beta \

#### Step 5 - Build nginx package and Install

cd ~/new/nginx_source/nginx-1.8.0/

dpkg-buildpackage -b

The dpkg-buildpackage command will build the nginx.deb under ~/new/ngix_source/ Once package building is complete, please look in the directory:

cd ~/new/ngix_source/

ls

And then install nginx.

dpkg -i nginx_1.8.0-1~trusty_amd64.deb

### Testing

Run nginx -V to see the ngx_pagespeed was builted with nginx.

nginx -V

## Conclusion

The nginx web server there is a stable and fast open source http server that supports a variety of modules for optimization. One of these modules is the 'PageSpeed module' which is developed by google. Unlike apache, nginx modules are not dynamically loadable, so you have to select the desired modules before you build the nginx package.