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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
前言*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
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
前端开发 数据库 UED
构建高性能Web应用的关键技术
本文将介绍构建高性能Web应用的关键技术,包括前端优化、后端优化、数据库优化等方面。通过深入讨论各项技术的原理和实践方法,帮助开发者们提升Web应用的响应速度和用户体验。
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
26 0
|
1月前
|
负载均衡 Java 中间件
使用Go语言构建高性能Web服务
Go语言作为一种快速、高效的编程语言,其在构建高性能Web服务方面具有独特优势。本文将探讨如何利用Go语言开发和优化Web服务,以实现更高的性能和可伸缩性。
|
1月前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
163 0
|
24天前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
39 0
|
11天前
|
关系型数据库 MySQL 数据库
卸载云服务器上的 MySQL 数据库
卸载云服务器上的 MySQL 数据库
30 0
|
3天前
|
缓存 监控 数据库
Flask性能优化:打造高性能Web应用
【4月更文挑战第16天】本文介绍了提升Flask应用性能的七大策略:优化代码逻辑,减少数据库查询,使用WSGI服务器(如Gunicorn、uWSGI),启用缓存(如Flask-Caching),优化数据库操作,采用异步处理与并发(如Celery、Sanic),以及持续监控与调优。通过这些手段,开发者能有效优化Flask应用,适应大型或高并发场景,打造高性能的Web服务。
|
6天前
|
关系型数据库 MySQL 数据库连接
Django(四):Django项目部署数据库及服务器配置详解(MySQL)
Django(四):Django项目部署数据库及服务器配置详解(MySQL)
29 11
|
26天前
|
Java 关系型数据库 MySQL
Flink1.18.1和CDC2.4.1 本地没问题 提交任务到服务器 报错java.lang.NoClassDefFoundError: Could not initialize class io.debezium.connector.mysql.MySqlConnectorConfig
【2月更文挑战第33天】Flink1.18.1和CDC2.4.1 本地没问题 提交任务到服务器 报错java.lang.NoClassDefFoundError: Could not initialize class io.debezium.connector.mysql.MySqlConnectorConfig
46 2
|
1月前
|
监控 前端开发 JavaScript
构建高性能Web应用:前端性能优化的关键策略与实践
本文将深入探讨前端性能优化的关键策略与实践,从资源加载、渲染优化、代码压缩等多个方面提供实用的优化建议。通过对前端性能优化的深入剖析,帮助开发者全面提升Web应用的用户体验和性能表现。

热门文章

最新文章