Install and test nginx on Ubuntu 14.04

原创 2016年05月31日 21:58:22

安装环境:
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

Comlile and Install OpenVas 8 on Ubuntu Server 14.04

转自:https://www.mockel.se/index.php/2015/04/openvas-8-on-ubuntu-server-14-04/ After a question o...

Ubuntu 14.04安装PostgreSQL How To Install and Use PostgreSQL on Ubuntu 14.04

Introduction Relational database management systems are a key component of many web sites and appli...

Install Ubuntu 14.04 on i.MX7D-SDB

  • 2017年09月06日 18:59
  • 583KB
  • 下载

Install jdk8 on Ubuntu 14.04

JDK is an important tool which consists the base of Java programming as well as supports many other ...

How To Install Apache Kafka on Ubuntu 14.04

#How To Install Apache Kafka on Ubuntu 14.04IntroductionApache Kafka is a popular distributed messag...

How To Install Go 1.6 on Ubuntu 14.04

How to Install Go 1.6 on Ubuntu 14.04 Introduction Go is a modern programming language developed...

How to Install Metasploit on Linux Ubuntu 14.04

原文章:点击打开链接 Install Metasploit Framework in Ubuntu 14.04. Here is how to download and inst...

INSTALL NGINX, PHP AND MYSQL ON OS X

INSTALL NGINX, PHP AND MYSQL ON OS X 19. OCTOBER 2014 — 503 COMMENTS Recently I got a new MacBo...

Install R and RStudio on Ubuntu

I wanted to try Linux system thus I formatted an old laptop bought 7 years ago (HP Pavilion dm3, Fre...

Teamviewer install on AWS RHEL7 and Ubuntu

Tried to install Teamviewer on AWS RHEL 7 and Ubuntu, with the fail result, note down the procedure,
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Install and test nginx on Ubuntu 14.04
举报原因:
原因补充:

(最多只允许输入30个字)