编程小贴士

给你的编程提供小点子


编译PHP并与Ngnix整合

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。

nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx

一、首先我们先初步认识下FastCGI和Ngnix+FastCGI的运行原理

1、apache+PHP和ngnix+php的区别

apache一般是把php当做自己的一个模块来启动;而ngnix则是把http请求变量转发给php进程,即php独立进程,与ngnix通信,这种方式叫做FastCGI运行方式。

所以,apache所编译的php不能用于ngnix。

先看一个简单的图:

tips1000_2015-06-22_06-43-46

以下这段我认为是在网络上找到的最通俗易懂的解释,我将它复制进来方便大家理解:

1、什么是FastCGI呢?

FastCGI是一个可伸缩地、高速地在 HTTP server和动态脚本语言间通信的接口。多数流行的HTTP server都支持FastCGI,包括Apache、Nginx和lighttpd等。同时,FastCGI也被许多脚本语言支持,其中就有PHP。

FastCGI是从CGI发展改进而来 的。传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后将结果返回给HTTP服务 器。这在处理高并发访问时几乎是不可用的。另外传统的CGI接口方式安全性也很差,现在已经很少使用了。

FastCGI接口方式采用C/S结构, 可以将HTTP服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当HTTP服务器每次遇到动态程序时,可以将其直 接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客 户端,这在很大程度上提高了整个应用系统的性能。

2、Nginx+FastCGI运行原理 

Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。

wrapper: 为了调用CGI程序, 还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如 端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后 Fork(派生)出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口, 沿着固定的socket传递给Nginx;最后,Nginx将返回的数据(html页面或者图片)发送给客户端。这就是Nginx+FastCGI的整个 运作过程,

tips1000_2015-06-22_06-43-45

所以,我们首先需要一个wrapper,这个wrapper需要完成的工作:

  1. 通过调用fastcgi(库)的函数通过socket和ningx通信(读写socket是fastcgi内部实现的功能,对wrapper是非透明的)
  2. 调度thread,进行fork和kill
  3. 和application(php)进行通信

二、编译PHP

首先我们先认识一个概念:php-fpm

(1)PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到

(2)PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用

(3)新版的PHP已经集成了php-fpm,在./configure的时候带 –enable-fpm参数即可开启PHP-FPM

接下来我们开始安装编译PHP

1、编译前我们需要先安装一些php需要用到的库(我用的是centos版本的linux系统)

yum -y install gcc automake autoconf libtool make

    yum -y install gcc gcc-c++ glibc

    yum -y install libmcrypt-devel mhash-devel libxslt-devel \

    libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \

    zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \

    ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \

    krb5 krb5-devel libidn libidn-devel openssl openssl-devel

 

2、获取php源码包

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz

 

3、解压并进入php目录

tar zvxf php-5.4.7.tar.gz

    cd php-5.4.7

    tips1000_2015-06-22_06-43-453

 

 

   4、编译并安装

    ./configure prefix=/usr/local/fastphp enable-fpm with-mcrypt \

    enable-mbstring disable-pdo with-curl disable-debug disable-rpath \

    enable-inline-optimization with-bz2 with-zlib enable-sockets \

    enable-sysvsem enable-sysvshm enable-pcntl enable-mbregex \

    with-mhash enable-zip with-pcre-regex with-mysql with-mysqli \

    with-gd with-jpeg-dir

  

    make all install

 

  5、将php-fpm复制一份
  tips1000_2015-06-22_06-43-45

  6、将php配置文件复制一份
  tips1000_2015-06-22_06-43-451

  7、修改nginx配置文件以支持php-fpm
    首先打开ngnix配置文件
      vim /usr/local/ngnix/conf/ngnix.conf
    在server段里加入以下代码
    tips1000_2015-06-22_06-43-452
    保存

  8、启动php-fpm和ngnix
    /usr/local/ngnix
    /usr/local/ngnix/sbin/php-fpm

  9、在根目录下创建测试文件test.php做个简单的测试
    tips1000_2015-06-22_06-43-461

  10、测试成功
    tips1000_2015-06-22_06-43-462



php-fpm的关闭和重启需要使用信号控制,其实就跟ngnix的关闭重启一样
  php-fpm 关闭:kill -INT `cat /usr/local/fastphp/var/run/php-fpm.pid`
  php-fpm 重启:kill -USR2 `cat /usr/local/fastphp/var/run/php-fpm.pid`
  查看php-fpm进程数:ps aux | grep -c php-fpm

72 Responses to “ 编译PHP并与Ngnix整合 ”

  1. i love pron说道:

    p0BTyB One of the hair coconut oil hair growth construction and follicles.

  2. mia pron khalifa说道:

    OduT4X Wow, this paragraph is fastidious, my younger sister is analyzing these kinds of things, therefore I am going to inform her.

  3. Thanks for sharing this first-class article. Very inspiring! (as always, btw)

  4. Kickboxing说道:

    later than having my breakfast coming again to

  5. girl drawing说道:

    We all speak a little about what you should speak about when is shows correspondence to simply because Maybe this has more than one meaning.

  6. is mca a scam说道:

    Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is great, as well as the content!

  7. we could greatly benefit from each other. If you are interested feel free

  8. Oluwadamilare说道:

    Really appreciate you sharing this article post.Much thanks again. Awesome.

  9. go说道:

    Really appreciate you sharing this article.Thanks Again. Keep writing.

  10. website说道:

    It as nearly impossible to find experienced people on this subject, but you seem like you know what you are talking about! Thanks

  11. Woking taxi说道:

    Really enjoyed this blog post.Much thanks again. Will read on

  12. Keep up the wonderful work , I read few blog posts on this web site and I think that your blog is very interesting and holds circles of excellent info.

  13. Wow, that as what I was looking for, what a stuff! present here at this website, thanks admin of this site.

  14. This is one awesome blog post.Really looking forward to read more. Great.

  15. klm promo code说道:

    Peculiar article, exactly what I was looking for.

  16. Usually I do not learn article on blogs, but I wish to say that this write-up very pressured me to try and do it! Your writing style has been surprised me. Thank you, very great article.

  17. trash cans说道:

    This very blog is really awesome additionally diverting. I have picked up many useful stuff out of it. I ad love to come back every once in a while. Cheers!

  18. Drake Mp3说道:

    site-building people, due to it as nice content

  19. yeezy boost说道:

    My husband and i felt very thrilled that Chris managed to deal with his web research through the entire precious recommendations he discovered in your blog. It is now and again perplexing to just possibly be freely giving techniques which often many others have been selling. We really acknowledge we need you to appreciate because of that. The main explanations you have made, the simple blog navigation, the friendships your site make it easier to create – it’s many fantastic, and it is helping our son and us understand this article is fun, which is extraordinarily essential. Thanks for all the pieces!

  20. si quan ao说道:

    moment this time I am browsing this website and reading very informative

  21. this website说道:

    It as not that I want to replicate your web page, but I really like the style. Could you tell me which design are you using? Or was it tailor made?

  22. Dentzz说道:

    Really informative post.Really thank you! Want more.

  23. This especially helped my examine, Cheers!

  24. There as definately a great deal to learn about this issue. I really like all the points you made.

  25. VIBRAM FIVE FINGERS OUTLET WALSH | ENDORA

  26. wow, awesome article post.Thanks Again. Fantastic.

  27. Christen说道:

    Say, you got a nice post.Really thank you! Really Cool.

  28. If at first you don at succeed, find out if the loser gets anything..

  29. No one can reject from the quality of this video posted at this web site, nice job, keep it all the time.

  30. Hosting说道:

    Only a smiling visitor here to share the love (:, btw outstanding style.

  31. Your style is really unique in comparison to other folks I have read stuff from. Thank you for posting when you ave got the opportunity, Guess I will just book mark this page.

  32. online说道:

    pretty valuable material, overall I consider this is worth a bookmark, thanks

  33. value说道:

    I truly appreciate this post.Much thanks again. Keep writing.

  34. Really informative post.Much thanks again. Will read on

  35. Waec expo说道:

    Major thankies for the post.Really looking forward to read more.

  36. Waec expo说道:

    Wow! Thank you! I permanently needed to write on my website something like that. Can I include a fragment of your post to my blog?

  37. wow, awesome blog.Really looking forward to read more. Keep writing.

  38. I went over this internet site and I conceive you have a lot of good information, saved to bookmarks (:.

  39. Nice blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol

  40. to learn more说道:

    This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your site in my social networks!

  41. Open heavens说道:

    Thank you for the good writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! By the way, how could we communicate?|

  42. Very good info. Lucky me I recently found your site by accident (stumbleupon). I ave book-marked it for later!

  43. I think this is a real great blog post.Much thanks again. Want more.

  44. Thanks for sharing, this is a fantastic blog.Thanks Again.

  45. Well I sincerely liked studying it. This post offered by you is very useful for accurate planning.

  46. tweaks说道:

    Im thankful for the post.Really looking forward to read more. Really Cool.

  47. to me. Regardless, I am certainly pleased I discovered it and I all be book-marking it

  48. That as a great post. Thank you so much.

  49. Thanks, I ave been searching for details about this subject for ages and yours is the best I ave found so far.

  50. This blog was how do you say it? Relevant!! Finally I have found something which helped me. Thank you!

  51. Thank you ever so for you article.Thanks Again. Really Cool.

  52. services offered have adequate demand. In my opinion the best craigslist personals

  53. dclm devotionals说道:

    Wow! This could be one particular of the most beneficial blogs We ave ever arrive across on this subject. Actually Fantastic. I am also a specialist in this topic so I can understand your hard work.

  54. This web site is really a walk-through for all of the info you wanted about this and didnaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžt know who to ask. Glimpse here, and youaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžll definitely discover it.

  55. You made some really good points there. I checked on the internet for more info about the issue and found most people will go along with your views on this web site.

  56. click resources说道:

    In my view, if all webmasters and bloggers made good content as you

  57. Thanks again for the blog article.Really looking forward to read more. Will read on

  58. I reckon something genuinely special in this internet site.

  59. see this website说道:

    I truly appreciate this article.Really looking forward to read more. Really Cool.

  60. ads说道:

    Really enjoyed this blog.Thanks Again. Will read on

  61. Please let me know if you are looking for a article author for your blog. You have some really good articles and I believe I would be a good asset. If you ever want to take some of the load off,

  62. Would you be eager about exchanging hyperlinks?

  63. You made some clear points there. I did a search on the topic and found most people will approve with your blog.

  64. Stella Timon说道:

    Hey, what kind of anti-spam plugin do you use for your blog.;`-~.

  65. site说道:

    pretty helpful material, overall I imagine this is really worth a bookmark, thanks

  66. Penni说道:

    Where did you get this information from ? Toronto Insurance

  67. I will right away snatch your rss as I can not in finding your email subscription link or newsletter service. Do you have any? Please let me recognize in order that I may just subscribe. Thanks.

  68. Really informative article.Really looking forward to read more.

  69. official site说道:

    Very good info. Lucky me I came across your blog by accident (stumbleupon). I ave bookmarked it for later!

  70. go there说道:

    Some genuinely good content on this internet site , regards for contribution.

  71. Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn at show up. Grrrr well I am not writing all that over again. Anyhow, just wanted to say great blog!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>