Install and test nginx on Ubuntu 14.04

安装环境:
Ubuntu14.04 64bits
X64架构

1 安装nginx

1.1 Installation

sudo apt-get install nginx
/etc/init.d/nginx start

Check

htop (按Fn4,用nginx过滤一下)

In the output of the command, look for “nginx master process” and “nginx worker process” in the list. If you see these processes in the list, Nginx is running. 应该有一个master,多个worker。这样nginx安装成功了。

nginx默认带了一个静态页面,在默认的配置文件中指定了这个index.html.

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

用浏览器访问80端口,能看到类似显示如下信息的页面:

Welcome to nginx! 

1.2 控制启动,停止

nginx -s signal

Where signal may be one of the following:

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

2 修改配置文件,建自己的页面

总体来说配置文件中有几个重要概念,从大到小:

context >> blocks >> simple derivative

blocks就是若干个simple derivative用{}包括起来。context是若干个blocks。例如整个配置文件都是main context; The events and http directives reside in the main context, server in http, and location in server.

修改默认的配置文件:

vi /etc/nginx/sites-available/default

2.1 准备站点页面

First, create the /data/www directory and put an index.html file with any text content into it and create the /data/images directory and place some images in it.

然后修改配置文件,主要是设置一下站点的路径(将原来的root路径注释掉)。需要加入的配置信息如下所示:

server {
    root /data/www;
    index index.php index.html index.htm;

    location /images/ {
        root /data;
    }
}

然后就可以用浏览器访问自己的index.html以及放到images目录下的图片。

3 加入FastCGI页面的支持

nginx只能Serve静态也页面和静态文件,如果需要执行代码,如(php),最简单的需要使用FastCGI。

3.1 安装安PHP FPM (FastCGI Process Manager).

sudo apt-get install php5-fpm
sudo nano /etc/php5/fpm/php.ini   #修改配置文件
sudo service php5-fpm restart

修改配置文件主要是要改这个:We will change both of these conditions by uncommenting the line and setting it to “0” like this:

cgi.fix_pathinfo=0 

如果不这样设置,那么php对不认识的请求文件格式,默认的操作是执行,容易导致攻击。

check

ps -e |grep php5-fpm

1757 ?        00:00:00 php5-fpm
1760 ?        00:00:00 php5-fpm
1761 ?        00:00:00 php5-fpm

3.2 修改nginx配置文件

由于nginx和fastCGI都在一个机器运行,所以两者使用unix socket通讯。在default文件中加入:

location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

写一个简单的php文件,例如下面这个info.php

<?php var_export($_SERVER)?>

info.php放到/data/www目录下,使用浏览器访问,应该可以看到如下页面:

info.php

4 测试一下性能

使用ApacheBench工具。首先安装ApacheBench。安装了Apache就有,因为只是测试nginx,所以还可以装achebench standalone的版本。

sudo apt-get install apache2-utils
ab -c 20 -n 1000 http://localhost/index.html

输出测试结果:

Server Software:        nginx/1.4.6
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.html
Document Length:        968 bytes

Concurrency Level:      20
Time taken for tests:   0.067 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      1209000 bytes
HTML transferred:       968000 bytes
Requests per second:    15009.16 [#/sec] (mean)
Time per request:       1.333 [ms] (mean)
Time per request:       0.067 [ms] (mean, across all concurrent requests)
Transfer rate:          17720.77 [Kbytes/sec] received
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值