读书人

ubuntu配备nginx

发布时间: 2012-11-07 09:56:10 作者: rapoo

ubuntu配置nginx

附正确配置的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 显示的内容意思如下:

读书人网 >编程

热点推荐