ubuntu+nginx安装配置应用说明,ubuntunginx


 

 

附正确配置的nginx配置文件。

 

推荐实例安装:博客迁移到nginx的过程

 

 

 

第一步,安装nginx

apt-get update
apt-get install nginx
即可完成安装

启动nginx:
/etc/init.d/nginx start
然后就可以访问了,http://localhost/ , 一切正常!如果不能访问,先不要继续,看看是什么原因,解决之后再继续。

第二步,安装Php和mysql
安装php和MySQL:
apt-get install php5-cli php5-cgi mysql-server-5.0 php5-mysql

第三步,安装FastCgi和配置
我们需要/usr/bin/spawn-fcgi这个文件,而它是属于lighttpd这个包里面的,所以我们安装lighttpd然后把它设置为开机不启动:

apt-get install lighttpd #我们只要/usr/bin/spawn-fcgi
rcconf #去掉lighttpd开机自启动--------------------------------------------强烈推荐
修改nginx的配置文件:/etc/nginx/sites-available/default
修改 server_name 192.168.200.100;
修改index的一行修改为:
index index.php index.html index.htm;

去掉下面部分的注释并修改为:
location ~ \.php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;

}


在server{}内定义日志文件的位置和相应的格式:
access_log /var/log/nginx/localhost_access.log combined;

 

access_log off;//表示关闭

 


重新启动nginx:
/etc/init.d/nginx stop
/etc/init.d/nginx start


启动fastcgi php:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

 
以下步骤我直接运行rcconf设置php-cgi为开机自启动即可,所以跳过
---------------------------------------为了让php-cgi开机自启动:
cd /etc/init.d
cp nginx php-cgi
vim php-cgi

替换nginx为php-cgi

并修改相应部分为:
DAEMON=/usr/bin/spawn-fcgi
DAEMON_OPTS="-a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi"
...
stop)
        echo -n "Stopping $DESC: "
        pkill -9 php-cgi
        echo "$NAME."

-------------------------------------------------

在/var/www/nginx-default/目录下创建一个文件:  /var/www/nginx-default/index.php
文件内容是:

< ?php phpinfo();?>

然后浏览器访问nginx就可以看到一切正常了

 

------------------------------------------------------------END 安装成功

 

配置文件目录 /etc/nginx/    nginx.conf     /sites-available/default

www目录 /var/www/nginx-default/

 

启动fastcgi php:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

 

 

日志文件:

localhost.access.log  /var/log/nginx/localhost.access.log

access.log  /var/log/nginx/access.log

error.log    /var/log/nginx/error.log

 

 

---------------重定向nginx错误页面的方法

error_page 404  /404.html;

这个404.html保证在nginx主目录下的html目录中即可,如果需要在出现404错误后直接跳转到另外一个地址,可以直接设置如下:


error_page 404
http://www.***.net ;


同样的方式可以定义常见的403、500等错误。


特别注意的是404.html文件页面大小要超过512k,不然会被ie浏览器替换为ie默认的错误页面。

 

 

------------------------------虚拟主机配置

server {
    listen   80;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www/nginx-default;
        index index.php index.html index.htm;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /images {
        root   /usr/share;
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}


server {
    listen   80;
    server_name  sdsssdf.localhost.com;
    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www/nginx-default/console;
        index index.php index.html index.htm;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /images {
        root   /usr/share;
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

 

----------------------监控  

 

location ~ ^/NginxStatus/ {

stub_status on; #Nginx 状态监控配置     
}


 

 

 

这样通过 http://localhost/NginxStatus/(最后的/不能掉) 监控到 Nginx 的运行信息:

 

Active connections: 1
server accepts handled requests
 1 1 5
Reading: 0 Writing: 1 Waiting: 0

 

 

NginxStatus 显示的内容意思如下:

  • active connections – 当前 Nginx 正处理的活动连接数。
  • server accepts handled requests -- 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )。
  • reading -- nginx 读取到客户端的 Header 信息数。
  • writing -- nginx 返回给客户端的 Header 信息数。
  • waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

 

-------------------------------静态文件处理

通过正则表达式,我们可让 Nginx 识别出各种静态文件

 

location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
        root /var/www/nginx-default/html;
        expires 24h;
        }

对于例如图片、静态 HTML 文件、js 脚本文件和 css 样式文件等,我们希望 Nginx 直接处理并返回给浏览器,这样可以大大的加快网页浏览时的速度。因此对于这类文件我们需要通过 root 指令来指定文件的存放路径,同时因为这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。您可以使用例如以下的格式来书写 Expires:

 

 

expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;

 

 

这样当你输入http://192.168.200.100/1.html的时候会自动跳转到var/www/nginx-default/html/1.html

 

例如 images 路径下的所有请求可以写为:

 

 

 

location ~ ^/images/ {
    root /opt/webapp/images;
}

 

 

 

 

------------------------动态页面请求处理[集群]

Nginx 本身并不支持现在流行的 JSP、ASP、PHP、PERL 等动态页面,但是它可以通过反向代理将请求发送到后端的服务器,例如 Tomcat、Apache、IIS 等来完成动态页面的请求处理。前面的配置示例中,我们首先定义了由 Nginx 直接处理的一些静态文件请求后,其他所有的请求通过 proxy_pass 指令传送给后端的服务器 (在上述例子中是 Tomcat)。最简单的 proxy_pass 用法如下:

 

location / {
    proxy_pass        http://localhost:8080;
    proxy_set_header  X-Real-IP  $remote_addr;
}

 

 

这里我们没有使用到集群,而是将请求直接送到运行在 8080 端口的 Tomcat 服务上来完成类似 JSP 和 Servlet 的请求处理。

当页面的访问量非常大的时候,往往需要多个应用服务器来共同承担动态页面的执行操作,这时我们就需要使用集群的架构。 Nginx 通过 upstream 指令来定义一个服务器的集群,最前面那个完整的例子中我们定义了一个名为 tomcats 的集群,这个集群中包括了三台服务器共 6 个 Tomcat 服务。而 proxy_pass 指令的写法变成了:

 

 

 

# 集群中的所有后台服务器的配置信息
    upstream tomcats {
     server 192.168.0.11:8080 weight=10;
     server 192.168.0.11:8081 weight=10;
     server 192.168.0.12:8080 weight=10;
     server 192.168.0.12:8081 weight=10;
     server 192.168.0.13:8080 weight=10;
     server 192.168.0.13:8081 weight=10;
    }
    location / {
        proxy_pass http://tomcats;# 反向代理
        include proxy.conf;
        }

 

 

----------------------压力测试

wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz  
tar zxvf webbench-1.5.tar.gz  
cd webbench-1.5  
make && make install

#webbench -c 100 -t 10 http://192.168.200.100/info.php

参数说明:-c表示并发数,-t表示持续时间(秒)

 

 

root@ubuntu-desktop:/etc/nginx/sites-available# webbench -c 100 -t 10 http://192.168.200.100/info.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.200.100/info.php
100 clients, running 10 sec.

Speed=19032 pages/min, 18074373 bytes/sec.
Requests: 3172 susceed, 0 failed.

 

 

 

 

相关内容

    暂无相关文章