`

Nginx+php(FastCGI)+Memcached+Mysql+APC高性能web服务器安装(转)

 
阅读更多
Nginx+php(FastCGI)+Memcached+Mysql+APC高性能web服务器安装
Nginx介绍:
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括国内最大的电子地图MapBar、新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56.com等视频分享网 站,Discuz!官方论坛、水木社区等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站,更多的网站都在使用Nginx配置。
Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网 易、 腾讯,另外知名的微网志Plurk也使用nginx。
为什么Nginx的性能要比Apache高得多?
这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的 Squid、Memcached都采用的是epoll网络I/O模型。 处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。
Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。在高连接并发的情况下,Nginx是Apache服务器不错的替代品: Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue 作为开发模型.
Nginx作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理 服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多. 。Nginx可作为7层负载均衡服务器来使用。
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
Nginx 是一个安装非常的简单 , 配置文件非常简洁(还能够支持perl语法), Bugs 非常少的服务器: Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够不间断服务的情况下进行软件版本的升级 .
总体来说nginx的有以下八大优点:
1.高并发连接:官方测试能支撑5万并发连接,在实际生产环境中跑到2,~3W并发连接。
2.内存消耗少:在3W并发连接下,开启的10个NGINX进程才消耗150M内存(15M*10=150M)
3.配置文件非常简单:风格跟程序一样通俗易懂。
4.成本低廉:Nginx作为开源软件,可以免费使用,而购买F5 BIG-IP、NetScaler
等硬件负载均衡交换机则需要十多万至几十万人民币。
5.支持rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分发到不同的后端服务器群组。
6.内置的健康检查功能:如果Nginx Proxy后端的后台web服务器宕机了,不会音响前端访问。
7.节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头。
8.稳定性高:用于反向代理,宕机的概率微乎其微。

介绍这么多nginx 的优点.下面来具体实现下LEMP架构(linux+nginx+mysql+php/perl/python)
可能有朋友对静态动态解析过程不太清楚,笔者按照自己理解简单阐述,如有错误地方请前辈多加指点。
nginx只是提供一个静态web服务,动态web服务还需要与PHP等脚本语言结合使用。
Nginx+php(FastCGI)+Memcached+Mysql+APC 是目前主流的高性能服务器搭建方式!适合大中型网站,小型站长也可以采用这种组合!
前面 nginx php mysql大家已经很熟悉了,这里简单介绍下 memcached 和 APC 。
一:memcached
在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东。这里简单介绍一下,memcached 是高效、快速的分布式内存对象缓存系统,主要用于加速 WEB 动态应用程序。由于nginx对静态网站加速效果明显,而动态一直效果不太好,有了memcached,生活将变得简单。
二:APC

Alternative PHP Cache(APC)是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开并且强健的架构来 缓存和优化 PHP 的中间代码。
安装配置:
准备工作,需要上面提到的软件另外还有两个包mysql-5.1.41.tar.gz、php-5.3.5.tar.gz (其他相似版本也可以)可以在官网下载。
一、编译安装Nginx (安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等)
(1)首先配置安装pcre:
cd  /usr/src  &&tar xzf pcre-8.01.tar.gz   &&cd pcre-8.01  &&  ./configure   –prefix=/usr/local/pcre  &&make &&make install
(2)然后再编译安装nginx :
useradd www  && cd  /usr/src && tar xzf  nginx-0.7.61.tar.gz   &&cd  nginx-0.7.61  &&  ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-openssl=/usr/ –with-pcre=/usr/src/pcre-8.01  –user=www –group=www  &&make  &&make install
【nginx注意*  –with-pcre=/usr/src/pcre-8.01指向的是源码包解压的路径,而不是安装的路径,否则会报
make[1]: *** [/usr/local/pcre/Makefile] Error 127 错误】
二、接下来安装mysql
(1)、cd   /usr/src && tar xzf  mysql-5.1.41.tar.gz   && cd mysql-5.1.41  && ./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase  && make  &&make install
(2)、mysql安装完毕,创建mysql用户和组并初始化数据库,并启动数据库。
cd  /usr/local/mysql && useradd mysql && chown -R  mysql:mysql   /usr/local/mysql  &&  /usr/local/mysql/bin/mysql_install_db  –user=mysql   &&   chown -R   mysql:mysql  var/  && ./bin/mysqld_safe   –user=mysql &
【如果mysql启动报错,请检查 /usr/local/mysql/var 目录,mysql是否有权限】
三、安装 php :
cd   /usr/src    &&tar xzf  php-5.3.5.tar.gz   && cd php-5.3.5  && ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql –with-mysqli=/u
sr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-li
bxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem
–enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex  –enable-fpm  –enable-sockets  && make  &&make install
安装完毕!【注意这个参数在此可以不加–enable-fastcgi;其他之前版本需要加上,以上安装根据自己的选择添加,如果报错,根据具体报错找原因】
四、整合Nginx和php(FastCGI)安装完php-5.3.5后支持fastCGI
(1)、配置nginx ,拷贝nginx配置文件:
user  www www;
worker_processes 8;
error_log  /usr/local/nginx/logs/error.log  crit;
pid        /usr/local/nginx/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;
charset    utf-8;
error_page  400 404 403 500 502 503 http://blog.mgcrazy.com;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush        on;
keepalive_timeout 60;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 8k;
fastcgi_temp_file_write_size 8k;
##如下设置fastcGI_cache缓存,加速你的web站点!
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
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;
##设置301跳转,让二级域名跳转到你规定的url;
server
{
listen       80;
server_name blog.mgcrazy.com wgkgood.gicp.net linux.mgcrazy.com;
if ($host = ‘wgkgood.gicp.net’ ) {
rewrite ^/(.*)$ http://blog.mgcrazy.com/$1 permanent;
}
if ($host = ‘linux.mgcrazy.com’ ) {
rewrite ^/(.*)$ http://blog.mgcrazy.com/$1 permanent;
}
index index.php index.htm index.html;
root  /home/webapps/www;
#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  /usr/local/nginx/logs/access.log  access;
}
}
Nginx配置完毕!启动nginx ;/usr/local/nginx/sbin/nginx 即可,重启nginx命令如下/usr/local/nginx/sbin/nginx –s  reload
(2)、配置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;
五、配置php配置文件:
cd /usr/local/php5/etc/ && cp   php-fpm.conf.default   php-fpm.conf
然后根据提示编辑php-fpm.conf里面的选项。
开启php-fpm
cp /usr/src/php-5.3.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 然后启动 /etc/init.d/php-fpm start 即可。
六、安装apc配置:
cd /usr/src && tar xzf APC-3.1.4.tgz &&cd APC-3.1.4
/usr/local/php5/bin/phpize && ./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php5/bin/php-config &&make&& make install
安装完后会生成一个apc.so在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里面。
七、安装memcached,使fastcGI支持memcached
首先安装libevent
cd /usr/src && tar xzf libevent-1.4.12-stable.tar.gz && cd libevent-1.4.12-stable && ./configure –prefix=/usr/local/libevent &&make && make install
然后安装memcached  //缓存加速
tar xzf memcache-2.2.5.tar.gz && cd memcache-2.2.5 && /usr/local/php5/bin/phpize && ./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent –with-php-config=/usr/local/php5/bin/php-config &&make &&make install
安装完后,会在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里生成一个memcache.so 这个模块:
八、编辑php.ini
默认的php.ini在/usr/local/php5/lib/php.ini
extension_dir = “./”   //修改路径
修改为
extension_dir=”/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626″
把下面这些添加到最后:
extension = apc.so
extension=memcache.so //这里引用缓存模块
[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64M
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl = 3600
apc.cache_by_default = on


FROM:http://blog.geiliit.net/archives/301.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics