1. 安装 Nginx

1. 使用源

1
2
3
sudo add-apt-repository ppa:nginx/stable  
sudo apt-get update  
sudo apt-get upgrade nginx -y

2. 安装

1
sudo yum install nginx

3. 配置

然后配置站点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
listen 80 ;
listen [::]:80 ipv6only=on;

root /usr/share/nginx/html/typecho;
index index.php index.html index.htm;

server_name applehater.cn;

location / {
#if (-f $request_filename/index.html){
# rewrite (.*) $1/index.html break;
#}
#if (-f $request_filename/index.php){
# rewrite (.*) $1/index.php;
#}
#if (!-f $request_filename){
# rewrite (.*) /index.php;
#}
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

2. 安装 PHP7

1. 添加源

1
2
3
4
5
$ sudo apt-get install python-software-properties software-properties-common

$ sudo add-apt-repository ppa:ondrej/php

$ sudo apt-get update

2. 安装 PHP7 和常用的扩展

1
$ sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-curl php7.0-cli php7.0-mcrypt php7.0-mbstring php7.0-dom

3. 配置 PHP7

去掉路径前缀,编辑/etc/php/7.0/fpm/php.ini

1
cgi.fix_pathinfo=0

启动服务

1
2
3
sudo phpenmod mcrypt

sudo service php7.0-fpm restart

3. 测试

1. 使用cli

1
2
3
4
5
6
php -v

PHP 7.0.5-3+donate.sury.org~trusty+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

2. 使用 phpinfo

创建文件/usr/share/nginx/html/typecho/info.php

1
2
3
<?php
echo phpinfo();
?>

显示详细信息页面

3. ab 对比

这是php5.6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Server Software:        nginx/1.8.1
Server Hostname: applehater.cn
Server Port: 80

Document Path: /
Document Length: 11494 bytes

Concurrency Level: 1
Time taken for tests: 3.321 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 1171500 bytes
HTML transferred: 1149400 bytes
Requests per second: 30.11 [#/sec] (mean)
Time per request: 33.207 [ms] (mean)
Time per request: 33.207 [ms] (mean, across all concurrent requests)
Transfer rate: 344.52 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 30 33 1.9 33 39
Waiting: 28 32 1.9 32 38
Total: 30 33 2.0 33 39

Percentage of the requests served within a certain time (ms)
50% 33
66% 33
75% 34
80% 35
90% 36
95% 38
98% 39
99% 39
100% 39 (longest request)

更新到 php7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Server Software:        nginx/1.8.1
Server Hostname: applehater.cn
Server Port: 80

Document Path: /
Document Length: 11434 bytes

Concurrency Level: 1
Time taken for tests: 1.442 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 1161800 bytes
HTML transferred: 1143400 bytes
Requests per second: 69.34 [#/sec] (mean)
Time per request: 14.421 [ms] (mean)
Time per request: 14.421 [ms] (mean, across all concurrent requests)
Transfer rate: 786.73 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 13 14 1.3 14 22
Waiting: 13 14 1.1 14 22
Total: 13 14 1.3 14 22

Percentage of the requests served within a certain time (ms)
50% 14
66% 14
75% 15
80% 15
90% 15
95% 17
98% 20
99% 22
100% 22 (longest request)

可见升级之后qps提高了130%,请求时间从33.207 ms下降到14.421 ms 减少了130% !