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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:
前言*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://hostrequest_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  'remoteaddrremote_user [timelocal]"request" '  
  87.               'statusbody_bytes_sent "$http_referer" '  
  88.               '"httpuseragent"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    documentrootfastcgi_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
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
344
分享
相关文章
ELK实现nginx、mysql、http的日志可视化实验
通过本文的步骤,你可以成功配置ELK(Elasticsearch, Logstash, Kibana)来实现nginx、mysql和http日志的可视化。通过Kibana,你可以直观地查看和分析日志数据,从而更好地监控和管理系统。希望这些步骤能帮助你在实际项目中有效地利用ELK来处理日志数据。
210 90
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
Nginx伪流媒体服务器搭建详细说明以及案例
Nginx伪流媒体服务器搭建步骤如下:1. 安装Nginx,根据系统选择命令;2. 编辑配置文件(/etc/nginx/nginx.conf),添加mp4相关设置;3. 创建视频目录/usr/share/nginx/html/videos并上传视频;4. 重启Nginx应用更改;5. 通过浏览器访问视频,如http://your_server_ip/videos/example.mp4。注意启用mp4模块,确保视频格式支持伪流媒体播放。
服务器数据恢复—云服务器上mysql数据库数据恢复案例
某ECS网站服务器,linux操作系统+mysql数据库。mysql数据库采用innodb作为默认存储引擎。 在执行数据库版本更新测试时,操作人员误误将在本来应该在测试库执行的sql脚本在生产库上执行,导致生产库上部分表被truncate,还有部分表中少量数据被delete。
85 25
|
2月前
|
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
94 20
当你的nginx服务器和php服务器不在一起的时候,这个nginx 的root目录问题
两个服务器的网站代码目录需要对齐,docker容器里面也是一样
如何解决 MySQL 数据库服务器 CPU 飙升的情况
大家好,我是 V 哥。当 MySQL 数据库服务器 CPU 飙升时,如何快速定位和解决问题至关重要。本文整理了一套实用的排查和优化套路,包括使用系统监控工具、分析慢查询日志、优化 SQL 查询、调整 MySQL 配置参数、优化数据库架构及检查硬件资源等步骤。通过一个电商业务系统的案例,详细展示了从问题发现到解决的全过程,帮助你有效降低 CPU 使用率,提升系统性能。关注 V 哥,掌握更多技术干货。
215 0
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
297 61
【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
39 0

推荐镜像

更多