Install and test nginx on Ubuntu 14.04

来源:互联网 发布:杨辉三角c语言程序六行 编辑:程序博客网 时间:2024/06/06 14:01

安装环境:
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-fpmsudo 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-fpm1757 ?        00:00:00 php5-fpm1760 ?        00:00:00 php5-fpm1761 ?        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-utilsab -c 20 -n 1000 http://localhost/index.html

输出测试结果:

Server Software:        nginx/1.4.6Server Hostname:        localhostServer Port:            80Document Path:          /index.htmlDocument Length:        968 bytesConcurrency Level:      20Time taken for tests:   0.067 secondsComplete requests:      1000Failed requests:        0Total transferred:      1209000 bytesHTML transferred:       968000 bytesRequests 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
0 0
原创粉丝点击