Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89807
  • 博文数量: 14
  • 博客积分: 454
  • 博客等级: 下士
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 00:49
文章分类

全部博文(14)

文章存档

2012年(14)

我的朋友

分类: LINUX

2012-02-10 22:20:57

一直听说nginx对于fcgi支持是多么的好,俄罗斯轻量级web服务器nginx支持的并发连接时apache的10倍

多。但是占用物理内存不到150M,今天终于可以研究下了!
lighttpd如果加在其中也使作为配置php-cgi程序!
下面摘自官网评价
==============================================================
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器

。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过

两年半了。Igor 将源代码以类BSD许可证的形式发布。
Nginx 是一个很牛的高性能 Web 和反向代理服务器, 它具有有很多非常优越的特性:

在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品:Nginx 在美国是做虚拟主机生意

的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应。

Nginx 作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也

可以支持作为 HTTP 代理服务器对外进行服务。Nginx 采用 C 进行编写,不论是系统资源开销还是 CPU

使用效率都比 Perlbal 要好很多。

作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的

之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

Nginx 是一个 安装非常的简单,配置文件 非常简洁(还能够支持 perl 语法),Bugs 非常少的

服务器:Nginx 启动特别容易,并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动

。还能够不间断服务的情况下进行软件版本的升级。
==============================================================================
我们开始下载所需要的软件
Nginx的中文wiki:

wget
wget
wget
wget
wget

Mysql自己下载,地址我这便记录丢掉了!目前来说Php-fpm只有5.2.6的补丁!php-fpm是为PHP打的一个

FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi
php-fpm官网:


另外服务器上安装以下软件包
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype

freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel

glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel
默认都会安装~~

==========================================================

第一步:安装Mysql,不论是源代码还是二进制只要能运行就好了

cp mysql-5.0.22.tar.gz /usr/local/src
cd /usr/local/src
tar zxvf mysql-5.0.22.tar.gz
cd mysql-5.0.22
./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/var/lib/mysql --

with-charset=gbk --with-xcharset=all

make && make install

usr/local/mysql/bin/mysql_install_db

groupadd -g 20000 mysql
useradd -u 20000 root -g mysql mysql
chown -R mysql /var/lib/mysql

cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

/usr/local/mysql/bin/mysql/mysqld_safe --user=root &

/usr/local/mysql/bin/mysqladmin -u root -p password 6xuan


enter password:回车


/usr/local/mysql/bin/mysql -u root -p

输入密码:6xuan

进入mysql
成功了~~~~~~~~~~~~~~~~~~~~~~~~~


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二步:安装php和php-fpm
tar zxvf php-5.2.6.tar.gz
gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1
cd php-5.2.6/
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-gd=/usr --with-gd-lib=/usr/lib --with-jpeg dir=/usr --with-png-dir=/usr/include/ --with-zlib --enable-mbstring --enable-iconv --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --with-openssl

make && make install

cp php.ini-dist /usr/local/lib/php/php.ini


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第三步,配置xcache和memcache,让php加速

tar zxvf memcache-2.2.3.tgz
cd memcache-2.2.3/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../


tar zxvf xcache-1.2.2.tar.gz
cd xcache-1.2.2
[root@localhost xcache-1.2.2]# /usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache
make
make install
cd ../

执行
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-

non-zts-20060613/"\nextension = "memcache.so"\n#' /usr/local/lib/php/php.ini
sed -i 's#display_errors = On#display_errors = Off#' /usr/local/lib/php/php.ini
它将会自动修改php.ini

然后继续配置php.ini,让他增加xcache加速

vi /usr/local/lib/php/php.ini

在最后面加入

[xcache-common]
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so

[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "8e6867a5d05144cf4761d6481fc674a8"

[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第四步:配置php-fpm.conf
创建php-fpm启动的用户和组
[root@localhost php]# /usr/sbin/groupadd www -g 480
[root@localhost php]# /usr/sbin/useradd -u 480 -g www www


vi /usr/local/php/etc/php-fpm.conf
用下面的代替里面内容

===============================================================================

All relative paths in this config are relative to php's install prefix

Pid file
/usr/local/php/logs/php-fpm.pid

Error log file
/usr/local/php/logs/php-fpm.log

Log level
notice

When this amount of php processes exited with SIGSEGV or SIGBUS ...
10

... in a less than this interval of time, a graceful restart will be initiated.
Useful to work around accidental curruptions in accelerator's shared memory.
1m

Time limit on waiting child's reaction on signals from master
5s

Set to 'no' to debug fpm
yes

Name of pool. Used in logs and stats.
default

Address to accept fastcgi requests on.
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
127.0.0.1:9000


Set listen(2) backlog
-1

Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Many BSD-derrived systems allow connections regardless of permissions.


0666

Additional php.ini defines, specific to this pool of workers.

/usr/sbin/sendmail -t -i
1

Unix user of processes
www

Unix group of processes
www

Process manager settings

Sets style of controling worker process count.
Valid values are 'static' and 'apache-like'
static

Sets the limit on the number of simultaneous requests that will be served.
Equivalent to Apache MaxClients directive.
Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
Used with any pm_style.
128

Settings group for 'apache-like' pm style

Sets the number of server processes created on startup.
Used only when 'apache-like' pm_style is selected
20

Sets the desired minimum number of idle server processes.
Used only when 'apache-like' pm_style is selected
5

Sets the desired maximum number of idle server processes.
Used only when 'apache-like' pm_style is selected
35

The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
0s

The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
0s

The log file for slow requests
logs/slow.log

Set open file desc rlimit
51200

Set max core size rlimit
0

Chroot to this directory at the start, absolute path

Chdir to this directory at the start, absolute path

Redirect workers' stdout and stderr into main error log.
If not set, they will be redirected to /dev/null, according to FastCGI specs
yes

How much requests each process should execute before respawn.
Useful to work around memory leaks in 3rd party libraries.
For endless request processing please specify 0
Equivalent to PHP_FCGI_MAX_REQUESTS
10240

Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
Makes sense only with AF_INET listening socket.
127.0.0.1

Pass environment variables like LD_LIBRARY_PATH
All $VARIABLEs are taken from current environment

$HOSTNAME
/usr/local/bin:/usr/bin:/bin
/tmp
/tmp
/tmp
$OSTYPE
$MACHTYPE
2



==============================================================================
启动php-fpm进程
/usr/local/php/sbin/php-fpm start

你可以用 ps -aux| grep php-cgi
查看是否存在进程


netstat -ant看是否在9000端口侦听


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第5步,安装pcre
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install

~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第六步,安装配置nginx

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module

--with-http_ssl_module
make && make install

修改/usr/local/nginx/conf/nginx.conf

用下面内容替换

==========================================

user www www;

worker_processes 8;

error_log /var/log/nginx/nginx_error.log crit;

pid /var/run/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{
include mime.types;
default_type application/octet-stream;

     
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

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;

#limit_zone crawler $binary_remote_addr 10m;

server
{
listen 80;
server_name   localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;

#limit_conn crawler 20;

location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 1h;
}

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/access.log access;
}


===========================================================
里面的

error_log /var/log/nginx/nginx_error.log crit;

pid /var/run/nginx.pid;
请根绝自己的修改
运行以下命令检测配置文件是否无误:

# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

创建fcgi的配置文件
vi /usr/local/nginx/conf/fcgi.conf
里面输入

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;




然后启动nginx
启动Nginx
/usr/local/nginx/sbin/nginx

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

关于测试

在/usr/local/nginx/html里面创建phpinfo探针~~

阅读(2729) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~