开发者社区> 科技小能手> 正文

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

简介:
+关注继续查看
前言*Nginx+php(FastCGI)+Memcached+Mysql+APC 是目前主流的高性能服务器搭建方式!适合大中型网站,小型站长也可以采用这种组合!
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括国内最大的电子地图MapBar、新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站,更多的网站都在使用Nginx配置

  1. 下载所需的安装包:这里采用源码包编译安装:本博客集成下载  
  2. http://blog.mgcrazy.com/download/nginx-0.7.61.tar.gz  
  3. http://blog.mgcrazy.com/download/pcre-8.01.tar.gz  
  4. http://blog.mgcrazy.com/download/memcache-2.2.5.tgz  
  5. http://blog.mgcrazy.com/download/libevent-1.4.12-stable.tar.gz  
  6. http://blog.mgcrazy.com/download/APC-3.1.4.tgz  
  7.    
  8. 下载到 /usr/src下  
  9. 另外还有两个包mysql-5.1.41.tar.gz、php-5.3.5.tar.gz 【其他相似版本也可以!】可以在官网下载。  
  10. 一、正式安装Nginx、【安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等】  
  11. 首先安装pcre:  
  12. cd  /usr/src  &&tar xzf pcre-8.01.tar.gz   &&cd pcre-8.01  &&  ./configure   --prefix=/usr/local/pcre  &&make &&make install  
  13. 然后再安装nginx :  
  14. 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  
  15.  
  16. 【nginx注意*  --with-pcre=/usr/src/pcre-8.01指向的是源码包解压的路径,而不是安装的路径,否则会报  
  17. 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.    
  3. mysql安装完毕,创建mysql用户和组并初始化数据库,并启动数据库。  
  4.    
  5. 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 &
三、安装 php :

  1. 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=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-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
 vi nginx.conf

  1. user  www www;  
  2. worker_processes 8;  
  3. error_log  /usr/local/nginx/logs/error.log  crit;  
  4. pid        /usr/local/nginx/nginx.pid;  
  5. #Specifies the value for maximum file descriptors that can be opened by this process.  
  6. worker_rlimit_nofile 51200;  
  7. events  
  8. {  
  9.   use epoll;  
  10.   worker_connections 51200;  
  11. }  
  12. http  
  13. {  
  14.   include      mime.types;  
  15. default_type  application/octet-stream;  
  16. charset    utf-8;  
  17. error_page  400 404 403 500 502 503 http://blog.mgcrazy.com;     
  18. server_names_hash_bucket_size 128;  
  19. client_header_buffer_size 2k;  
  20. large_client_header_buffers 4 4k;  
  21. client_max_body_size 8m;  
  22. sendfile on;  
  23. tcp_nopush        on;  
  24. keepalive_timeout 60;  
  25. fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2  
  26. keys_zone=TEST:10m  
  27. inactive=5m;  
  28. fastcgi_connect_timeout 300;  
  29. fastcgi_send_timeout 300;  
  30. fastcgi_read_timeout 300;  
  31. fastcgi_buffer_size 4k;  
  32. fastcgi_buffers 8 4k;  
  33. fastcgi_busy_buffers_size 8k;  
  34. fastcgi_temp_file_write_size 8k;  
  35. ##如下设置fastcGI_cache缓存,加速你的web站点!  
  36. fastcgi_cache TEST;  
  37. fastcgi_cache_valid 200 302 1h;  
  38. fastcgi_cache_valid 301 1d;  
  39. fastcgi_cache_valid any 1m;  
  40. fastcgi_cache_min_uses 1;  
  41. fastcgi_cache_use_stale error timeout invalid_header http_500;  
  42. fastcgi_cache_key http://$host$request_uri;    
  43. open_file_cache max=204800 inactive=20s;  
  44. open_file_cache_min_uses 1;  
  45. open_file_cache_valid 30s;  
  46. tcp_nodelay on;  
  47.    
  48. gzip on;  
  49. gzip_min_length       1k;  
  50. gzip_buffers        4 16k;  
  51. gzip_http_version 1.0;  
  52. gzip_comp_level 2;  
  53. gzip_types         text/plain application/x-javascript text/css application/xml;  
  54. gzip_vary on;  
  55.    
  56. ##设置301跳转,让二级域名跳转到你规定的url;  
  57.   server  
  58.   {  
  59.     listen       80;  
  60.     server_name blog.mgcrazy.com wgkgood.gicp.net linux.mgcrazy.com;  
  61.            if ($host = 'wgkgood.gicp.net' ) {  
  62.     rewrite ^/(.*)$ http://blog.mgcrazy.com/$1 permanent;     
  63. }  
  64.     if ($host = 'linux.mgcrazy.com' ) {  
  65.     rewrite ^/(.*)$ http://blog.mgcrazy.com/$1 permanent;  
  66. }  
  67.  
  68.     index index.php index.htm index.html;  
  69.     root  /home/webapps/www;  
  70.     #limit_conn   crawler  20;  
  71.     location ~ .*\.(php|php5)?$  
  72.     {  
  73.       #fastcgi_pass  unix:/tmp/php-cgi.sock;  
  74.       fastcgi_pass  127.0.0.1:9000;  
  75.       fastcgi_index index.php;  
  76.       include fcgi.conf;  
  77.     }  
  78.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  
  79.     {  
  80.       expires      30d;  
  81.     }  
  82.     location ~ .*\.(js|css)?$  
  83.     {  
  84.       expires      1h;  
  85.     }  
  86.     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '  
  87.               '$status $body_bytes_sent "$http_referer" '  
  88.               '"$http_user_agent" $http_x_forwarded_for';  
  89.     access_log  /usr/local/nginx/logs/access.log  access;  
  90.       }  
  91. }
 
Nginx配置完毕!启动nginx ;/usr/local/nginx/sbin/nginx 即可,重启nginx命令如下/usr/local/nginx/sbin/nginx –s  reload
 
此配置文件仅供参考,感谢张宴老师!
 
配置fcgi.conf文件如下
 

  1. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;  
  2. fastcgi_param  SERVER_SOFTWARE    nginx;  
  3.  
  4. fastcgi_param  QUERY_STRING       $query_string;  
  5. fastcgi_param  REQUEST_METHOD     $request_method;  
  6. fastcgi_param  CONTENT_TYPE       $content_type;  
  7. fastcgi_param  CONTENT_LENGTH     $content_length;  
  8.  
  9. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  
  10. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;  
  11. fastcgi_param  REQUEST_URI        $request_uri;  
  12. fastcgi_param  DOCUMENT_URI       $document_uri;  
  13. fastcgi_param  DOCUMENT_ROOT      $document_root;  
  14. fastcgi_param  SERVER_PROTOCOL    $server_protocol;  
  15.  
  16. fastcgi_param  REMOTE_ADDR        $remote_addr;  
  17. fastcgi_param  REMOTE_PORT        $remote_port;  
  18. fastcgi_param  SERVER_ADDR        $server_addr;  
  19. fastcgi_param  SERVER_PORT        $server_port;  
  20. fastcgi_param  SERVER_NAME        $server_name;  
  21.  
  22. # PHP only, required if PHP was built with --enable-force-cgi-redirect  
  23. fastcgi_param  REDIRECT_STATUS    200;
 五、配置php配置文件:

  1. cd /usr/local/php5/etc/ && cp   php-fpm.conf.default   php-fpm.conf   
  2.    
  3. 然后根据提示修改php-fpm.conf里面的选项。  
  4.    
  5. 配置完毕后,启动php-fpm  
  6. 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配置:

  1. cd /usr/src && tar xzf APC-3.1.4.tgz &&cd APC-3.1.4  
  2. /usr/local/php5/bin/phpize && ./configure --enable-apc --enable-apc-mmap --with-php-config=/usr/local/php5/bin/php-config &&make&& make install  
  3.    
  4. 安装完后会生成一个apc.so在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里面
七、安装memcached,使fastcGI支持memcached
首先安装libevent

  1. 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  
  2. 然后安装memcached  
  3. 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  
  4. 安装完后,会在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里生成一个memcache.so 这个模块
八、接下来修改php.ini

  1. 默认的php.ini在/usr/local/php5/lib/php.ini 你也可以指定:  
  2. extension_dir = "./" 
  3. 修改为  
  4. extension_dir="/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626" 
  5. 把下面这些添加到最后:  
  6. extension = apc.so  
  7. extension=memcache.so //这里引用缓存模块  
  8. [APC]  
  9. apc.enabled = 1 
  10. apc.shm_segments = 1 
  11. apc.shm_size = 64M 
  12. apc.optimization = 1 
  13. apc.num_files_hint = 0 
  14. apc.ttl=7200 
  15. apc.user_ttl=7200 
  16. apc.gc_ttl = 3600 
  17. apc.cache_by_default = on
安装到此已经完成!
重新启动nginx和php-fpm ,用测试页面访问。
此文章仅供参考!有不妥之处欢迎指正!共同学习!
 

本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/521209

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-JWT和中间件(Middleware)的使用EP07
前文再续,上一回我们完成了用户的登录逻辑,将之前用户管理模块中添加的用户账号进行账号和密码的校验,过程中使用图形验证码强制进行人机交互,防止账号的密码被暴力破解。本回我们需要为登录成功的用户生成Token,并且通过Iris的中间件(Middleware)进行鉴权操作。
37 0
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-登录与图形验证码(captcha)EP06
书接上回,上一回我们按照“低耦合高内聚”的组织架构方针对项目的整体结构进行了优化,本回将会继续编写业务,那就是用户的登录逻辑,将之前用户管理模块中添加的用户账号进行账号和密码的校验,校验通过后留存当前登录用户的信息,过程中使用图形验证码强制进行人机交互,防止账号的密码被暴力破解。
24 0
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-项目结构优化EP05
前文再续,上一回我们完成了用户管理模块的CURD(增删改查)功能,功能层面,无甚大观,但有一个结构性的缺陷显而易见,那就是项目结构过度耦合,项目的耦合性(Coupling),也叫耦合度,进而言之,模块之间的关系,是对项目结构中各模块间相互联系紧密程度的一种量化。耦合的强弱取决于模块间调用的复杂性、调用模块之间的方式以及通过函数或者方法传送数据对象的多少。模块间的耦合度是指模块之间的依赖关系,包括包含关系、控制关系、调用关系、数据传递关系以及依赖关系。项目模块的相互依赖越多,其耦合性越强,同时表明其独立性越差,愈加难以维护。
26 0
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-完善用户管理EP04
书接上回,上一回我们完成了用户管理页面的构建,并且通过前端的Vue.js框架动态地获取表单数据,同时异步请求后端Iris接口进行入库操作,过程中使用函数封装可复用的逻辑。 本回我们将继续完善用户管理功能。
16 0
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-用户系统EP03
前文再续,之前一篇我们已经配置好了数据库以及模板引擎,现在可以在逻辑层编写具体业务代码了,博客平台和大多数在线平台一样,都是基于用户账号体系来进行操作,所以我们需要针对用户表完成用户账号的CURD操作。
12 0
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-模板与数据库EP02
书接上回,上次我们搭建好了项目入口文件,同时配置了路由体系,接着就可以配置项目的模板了,这里我们采用Iris内置的模板引擎,事实上,采用模板引擎并不意味着前后端耦合,模板中的数据保持其独立性即可,也就是说模板的数据操作交互方式采用http接口请求的形式,Iris并不参与模板逻辑,只返回Json格式的数据即可。前端集成数据双向绑定机制的框架Vue.js。
26 0
彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-项目入口与路由EP01
书接上回,我们已经安装好Iris框架,并且构建好了Iris项目,同时配置了fresh自动监控项目的实时编译,万事俱备,只欠东风,彩虹女神蓄势待发。现在我们来看看Iris的基础功能,如何编写项目入口文件以及配置路由系统。
21 0
急如闪电快如风,彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-初始化项目EP00
在Golang Web编程的世界里,君不言高性能则已,言高性能必称Iris。彩虹女神的名号响彻寰宇、名动江湖,单论一个快字,无人能出其右,就连以简洁轻量著称于世的Gin也难以望其项背,只见彩虹女神Iris回眸一笑撩人心扉:“虽然你们也不是那么慢,但我还是快那么一点点......”,本次就让我们来一睹彩虹女神Iris的芳颜,感受宇宙最快Web框架的神乎其神。
38 0
架构与思维:互联网高性能Web架构
架构与思维:互联网高性能Web架构
85 0
FastAPI 学习之路(一)fastapi--高性能web开发框架
FastAPI 学习之路(一)fastapi--高性能web开发框架
336 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
边缘安全,让Web加速有保障
立即下载
使用CNFS搭建弹性Web服务
立即下载
WEB框架0day漏洞的发掘及分析经验分享
立即下载