2009年10月12日 由 zubinhe | 编辑本文 留言 »
作者:何祖彬[RobinHe] Mail:zubin.he@gmail.com
BLOG : http://blog.csdn.net/zubin006/
Website : http://www.wearelinuxer.com/
始于2009年9月6日
2009年10月12日 增加了一些流程的相关名词解释。
转载请注明出处,本文也是采用两篇网友的大部分内容,因此转载时,请也将下面“参考文章”的出处也一并加入!谢谢!
本文中难免有不足、遗漏、错别字和句子不通之处,如发现、或者有更新改变之处,请与我联系以及时修正和完善!
PS:由于也是刚刚接触nginx,因此此笔记还不是很完整,需研究几日再逐渐完善和更新。
系统信息:
OS : Ubuntu 9.04 Server版
使用说明:
绿色加粗字体的绝大部分是输入的命令和系统输出显示的结果。
参考文章:
http://hi.baidu.com/meshikar/blog/item/afcde800485a8114738b6576.html
CentOS下Nginx配置笔记(一)安装准备篇
http://ipbfans.org/2008/06/centos-nginx-install
CentOS下Nginx配置笔记(二)PHP以及虚拟主机配置
http://ipbfans.org/2008/06/centos-nginx-php-virtualhost
Nginx 0.7.x + PHP 5.2.8(FastCGI)搭建胜过Apache十倍的Web服务器(第4版)[原创]
http://blog.s135.com/nginx_php_v4/
nginx+PHP +PHP-FPM(FastCGI)在Ubuntu上的安装和配置
http://blog.csdn.net/omohe/archive/2009/07/10/4336731.aspx
Nginx介绍:
Nginx (”engine x”) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新 浪博客 、新浪播客 、网易新闻 等门户网站频道,六间房 、56.com 等 视频分享网站,Discuz!官方论坛 、水木社区 等知名论坛,豆瓣 、YUPOO 相册 、海内SNS 、迅雷在线 等新兴Web 2.0网站。
Nginx 的官方中文维基:http://wiki.nginx.org/NginxChs
Nginx+Mysql+PHP组合的运行处理流程图:
1,客户端打开网页–>Browser–>Nginx侦听的端口[本文为60080]:
2,服务器端调用:Nginx–>执行–> .php [调用127.0.0.1:9000即php-fpm] –>调用–> php模块解释程序 –> Mysql 数据库操作.
3,将调用结果返回Nginx产生网页,即HTML丢给客户端的Browser。
一,安装并下载所需以及相关的软件:
为了少打sudo,我启用了ubuntu下的root帐号,方法:
e@u904:~$ sudo passwd
[sudo] password for e: //输入当前用户e的密码进行身份验证
Enter new UNIX password: //输入root的新密码
Retype new UNIX password: //确认root的新密码
passwd: password updated successfully
1,安装所需的编译工具:
# apt-get install vim elinks gcc g++ make libncurses5-dev libpng12-dev libjpeg62-dev zlib1g-dev libxml2-dev libssl-dev openssl libperl-dev
2,下载nginx,mysql和php:
#cd /usr/local/src
#mkdir nginx //存放nginx、mysql和php源代码的目录
#cd nginx
#wget http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.39.tar.gz
#wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz
//***#wget http://cn2.php.net/get/php-5.2.11.tar.gz/from/cn.php.net/mirror
#wget http://cn2.php.net/get/php-5.3.0.tar.gz/from/cn.php.net/mirror
二,安装mysql:
# tar -zxvf mysql-5.1.39.tar.gz
# cd mysql-5.1.39
请看INSTALL-SOURCE文件,它会教你如何一步一步安装Mysql:
#vi INSTALL-SOURCE
增加运行mysql的用户mysql和组mysql:
#groupadd mysql
#useradd -g mysql mysql
配置mysql:根据需要将相关文件安装到相关的目录中去。依个人需要而定,请了解以下各参数的作用后再进行下一步:
# ./configure –prefix=/usr/local/mysql51 –sysconfdir=/usr/local/mysql51/etc –enable-thread-safe-client
# make
# make install
# mkdir /usr/local/mysql51/etc
# cp support-files/my-medium.cnf /usr/local/mysql51/etc/my.cnf
#cd /usr/local/mysql51
#chown -R mysql .
#chgrp -R mysql .
#./bin/mysql_install_db –user=mysql
#chown -R root .
#chown -R mysql var
#cp share/mysql/mysql.server /etc/init.d/mysqld51
启动mysql:
#/etc/init/mysqld51 start
查看mysql是否启动成功:
#netstat -tnlp | grep mysql
如果有看到这一行:
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1328/mysqld
恭喜你,这说明mysql启动成功.
三,安装PHP[FastCGI模式]:#cd /usr/local/src/nginx#wget http://php-fpm.org/downloads/php-5.3.0-fpm-0.5.12.diff.gz
#tar -zxvf php-5.3.0.tar.gz
#gzip -cd php-5.3.0-fpm-0.5.12.diff.gz | patch -d php-5.3.0 -p1./configure –prefix=/usr/local/php530 –with-mysql=/usr/local/mysql51 –with-gd –with-zlib –with-gettext –with-zlib-dir –with-jpeg-dir –enable-mbstring –with-iconv-dir –with-pdo-mysql=/usr/local/mysql –with-openssl –enable-sockets –enable-fpm
PS: PHP 5.3.X跟PHP 5.2.X有所不同,如果要启用fastcgi, PHP 5.2.X必须:–enable-fastcgi –enable-fpm,而5.3.X只要:–enable-fpm,具体请看:
#./configure –help | grep fpm
–enable-fpm FastCGI: If this is enabled, the fastcgi support
#make
#make install
#cp php.ini-development /usr/local/php530/lib/php.ini编辑php-fpm.conf文件:
#vi /usr/local/php530/etc/php-fpm.conf
修改:
Unix user of processes
<!– <value name=”user”>nobody</value> –>
Unix group of processes
<!– <value name=”group”>nobody</value> –>
为:
Unix user of processes
<value name=”user”>www</value>
Unix group of processes
<value name=”group”>www</value>
也可以更改php-fpm的端口等,当然根据自已喜欢而定,不过最好按标准,我把启动php-fpm的用户和组设为www.可以依情况而做相关变更。
启动php-fpm:
#/usr/local/php530/sbin/php-fpm start
看一下启动是否成功:
#netstat -tlnp | grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 12555/php-cgi
恭喜您,启动成功了!
三,安装Nginx:
先下载并解压pcre,因为Nginx中rewrite功能需要使用pcre库才能工作:
#cd /usr/local/src/nginx
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz
#tar -zxvf pcre-7.9.tar.gz
#cd /usr/local/src/nginx
#tar -zxvf nginx-0.7.62.tar.gz
#cd nginx-0.7.62
#./configure –prefix=/usr/local/nginx –with-pcre=/usr/local/src/nginx/pcre-7.9 –user=www –group=www –with-http_perl_module –with-http_stub_status_module
这行暂时取消://PS:ssl模块,支持https。
PS:–with-pcre需要指向pcre的源代码目录。
#make
#make install编辑/usr/local/nginx/conf/nginx.conf配置文件,我服务器上的内容如下[下次再做较详细介绍]:
#vi /usr/local/nginx/conf/nginx.conf
###config file start ###
#user nobody;
user www www;
worker_processes 1;error_log /usr/local/nginx/logs/error.log crit;
#error_log /usr/local/nginx/logs/error.log notice;
#error_log /usr/local/nginx/logs/error.log info;#pid logs/nginx.pid;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
# ‘$status $body_bytes_sent “$http_referer” ‘
# ‘”$http_user_agent” “$http_x_forwarded_for”‘;
#access_log /usr/local/nginx/logs/access.log main;
access_log /usr/local/nginx/logs/access.log;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#gzip on;
server {
listen 60080;
#server_name localhost;
server_name ptovpn.3322.org;
#charset koi8-r;
#access_log /usr/local/nginx/logs/ptovpn.3322.org.access.log main;
access_log /usr/local/nginx/logs/ptovpn.3322.org.access.log;
location / {
#root /home/www/html;
root html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
#root /home/www/html;
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ /.php$ {
# proxy_pass http://127.0.0.1 ;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ /.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /nginx_status {
stub_status on;
access_log off;
allow 59.58.97.2;
deny all;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ //.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
###config file End ###
编辑/usr/local/nginx/conf/fcgi.conf
#vi /usr/local/nginx/conf/fcgi.conf
###config file End ###
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
###config file End ###
启动Nginx:
#/usr/local/nginx/sbin/nginx 查看是否启动成功: #netstat -tlnp | grep nginx
tcp 0 0 0.0.0.0:60080 0.0.0.0:* LISTEN 30309/nginx
由于我把nginx的端口改为60080,所以看到的是60080.
四,测试Nginx+Mysql+PHP:写一个php程序放在Doc root目录,即/home/www/html/phgg.php,内容如下:
<?php
phpinfo();
?> 打开您的浏览器,输入:
http://youipaddress:60080/phgg.php
如果能看到以下内容,恭喜你,你安装Nginx+Mysql+PHP成功…
PHP Version 5.3.0 |
System | Linux u904 2.6.28-11-server #42-Ubuntu SMP Fri Apr 17 02:48:10 UTC 2009 i686 |
Build Date | Oct 9 2009 15:53:01 |
Configure Command | ‘./configure’ ‘–prefix=/usr/local/php530′ ‘–with-mysql=/usr/local/mysql51′ ‘–with-gd’ ‘–with-zlib’ ‘–with-gettext’ ‘–with-zlib-dir’ ‘–with-jpeg-dir’ ‘–enable-mbstring’ ‘–with-iconv-dir’ ‘–with-pdo-mysql=/usr/local/mysql’ ‘–with-openssl’ ‘–enable-sockets’ ‘–enable-fpm’ |
Server API | CGI/FastCGI |
Virtual Directory Support | disabled |
Configuration File (php.ini) Path | /usr/local/php530/lib |
Loaded Configuration File | /usr/local/php530/lib/php.ini |
Scan this dir for additional .ini files | (none) |
Additional .ini files parsed | (none) |
PHP API | 20090626 |
PHP Extension | 20090626 |
Zend Extension | 220090626 |
Zend Extension Build | API220090626,NTS |
PHP Extension Build | API20090626,NTS |
Debug Build | no |
Thread Safety | disabled |
Zend Memory Manager | enabled |
Zend Multibyte Support | disabled |
IPv6 Support | enabled |
Registered PHP Streams | https, ftps, compress.zlib, php, file, glob, data, http, ftp, phar |
Registered Stream Socket Transports | tcp, udp, unix, udg, ssl, sslv3, sslv2, tls |
Registered Stream Filters | zlib.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk |
This program makes use of the Zend Scripting Language Engine: Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies |
…略…
学习感受:
由于Nginx+Mysql+PHP的构架是采用FastCGI模式,跟以往我们常用的Apache+Mysql+PHP的构架采用的模块化方式有所不 同,因此您得先了解一下FastCGI,以区分新的构架的运行流程方式后,才能更顺利的完成安装工作,并对这个构架有比较深的了解,而不是一味的安装成功 就好,这样的话以后查错就会很头痛。
附:
FastCGI
http://www.fastcgi.com/
CGICommonGatewayInterface的缩写。它的存在使得避免针对不同WebServerAPI发展不同的程式成为可能。软体发展者 可以用各种语言撰写程式作为W3Server与其他各种不同的伺服器间沟通的桥梁。在第五届国际WWW会议中,OpenMarket这家公司发表了一篇论 文,标题是“FastCGI:AHigh-PerformanceGatewayInterface”。文中提出了对於现有CGI规格的改进与功能的增 强。现有的CGI规格具有如下的特点: ●开放标准
●简单易了解
●适合各种程式语言
●与机器软硬体架构无关
●与Server的程序(process)无关 而CGI的缺点在於执行的速度不够快(相较於ServerAPI写的程 式)。而且CGI所扮演的角色也局限於反应者(responder)的角色。FastCGI除了具有CGI所具有的优点外,并且也大幅改进执行的速度。此 外最大的特点还有一个,就是它也使得分散执行应用程式成为可能的事情。目前可以发展FastCGI的Server有OpenMarket自己出的 SecureWebServer,NCSAHTTPD1.5.1,还有上面提到的ApacheHTTPServer。Apache用的 FastCGImodule可以在 http://www.fastcgi.com/servers/apache/连同说明文件一起取得。
————————————————————————————————
php-运行模式-ISAPI,fastcgi,CGI,cli,apache:
<script type="text/javascript">// <![CDATA[ document.body.oncopy = function() { if (window.clipboardData) { setTimeout(function() { var text = clipboardData.getData("text"); if (text && text.length>300) { text = text + "/r/n/n本文来自CSDN博客,转载请标明出处:" + location.href; clipboardData.setData("text", text); } }, 100); } } // ]]></script> <script type="text/javascript">// <![CDATA[ function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();} // ]]></script>
From : http://hi.baidu.com/24xinhui/blog/item/daf755ec12cdb13b269791d3.html
查看运行在哪个模式下 (2009-8-2 23:40)
php -v
配置信息
php -i |more
CLI 命令行模式 (2009-8-2)
类似 shell
http://www.php.net/manual/en/features.commandline.php
http://www.builderau.com.au/program/php/
PHP Command Line Interface
STDIN 标准的输入设备
STDOUT 标准的输出设备
STDERR 标准的错误设备
安装
# apt-get install php5-cli (或 php4-cli)
CLI 版 Hello World (記得 chmod +x)
#!/usr/bin/php
<?php echo ‘Hello World!’; ?>
IIS下PHP的ISAPI和FastCGI比较 (2009-8-2)
http://www.williamlong.info/archives/1846.html
1、CGI(通用网关接口/Common Gateway Interface)一般是可执行程序,例如EXE文件,和WEB服务器各自占据着不同的进程,而且一般一个CGI程序只能处理一个用户请求。这样,当用 户请求数量非常多时,会大量占用系统的资源,如内存、CPU时间等,造成效能低下。
2、ISAPI(Internet Server Application Program Interface)是微软提供的一套面向WEB服务的API接口,它能实现CGI提供的全部功能,并在此基础上进行了扩展,如提供了过滤器应用程序接 口。ISAPI应用大多数以DLL动态库的形式使用,可以在被用户请求后执行,,在处理完一个用户请求后不会马上消失,而是继续驻留在内存中等待处理别的 用户输入。此外,ISAPI的DLL应用程序和WEB服务器处于同一个进程中,效率要显著高于CGI。
3、FastCGI是可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。传统的CGI解释器的反复加 载 是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性等。
http://www.dualface.com/blog/?p=172
转载 廖宇雷
以 ISAPI 模式运行 PHP 的,这种方式最大的缺点就是稳定性不好,当 PHP 出错的时候,Apache进程也死掉了
FastCGI 模式运行 PHP 的优点:
以 FastCGI 模式运行 PHP 有几个主要的好处。首先就是 PHP 出错的时候不会搞垮 Apache,
只是 PHP 自己的进程当掉(但 FastCGI 会立即重新启动一个新 PHP 进程来代替当掉的进程)。
其次 FastCGI 模式运行 PHP 比 ISAPI 模式性能更好
最后,就是可以同时运行 PHP5 和 PHP4
FastCGI 模式的一些缺点:
说完了好处,也来说说缺点。用 FastCGI 模式更适合生产环境的服务器。
但对于开发用机器来说就不太合适。因为当使用 Zend Studio 调试程序时,
由于 FastCGI 会认为 PHP 进程超时,从而在页面返回 500 错误