免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2170 | 回复: 1

centos环境下nginx+php搭建 [复制链接]

论坛徽章:
0
发表于 2012-03-01 20:12 |显示全部楼层
centos环境下nginx+php搭建

我是在centos5环境下搭建的nginx服务器,使用php-fpm方式来驱动php,下面描述下使用配置过程.

环境:
操作系统 : centos 5
nginx-1.0.12
php-5.3.10

1. 安装php-5.3.10
注 : php-fpm已经作为一个模块添加到了php代码中,这里只需要在php编译的时候增加
--enable-fpm


wget http://nginx.org/download/nginx-1.0.12.tar.gz
tar -zxvf nginx-1.0.12.tar.gz
cd nginx-1.0.12
./configure --prefix=nginx-root
make
make install

3. 配置php-fpm
先拷贝配置文件,在进行编辑
Java代码  
cp phproot/etc/php-fpm.conf.default -> phproot/etc/php-fpm.conf   
vi phproot/etc/php-fpm.conf  

cp phproot/etc/php-fpm.conf.default -> phproot/etc/php-fpm.conf
vi phproot/etc/php-fpm.conf


这里只需要修改用户和你想监听的端口即可
Java代码  
;
  1. Unix user/group of processes   
  2. ; Note: The user is mandatory. If the group is not set, the default user's group   
  3. ;       will be used.   
  4. user = webadmin   
  5. group = webadmin   
  6.   
  7. ; The address on which to accept FastCGI requests.   
  8. ; Valid syntaxes are:   
  9. ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on   
  10. ;                            a specific port;   
  11. ;   'port'                 - to listen on a TCP socket to all addresses on a   
  12. ;                            specific port;   
  13. ;   '/path/to/unix/socket' - to listen on a unix socket.   
  14. ; Note: This value is mandatory.   
  15. listen = 127.0.0.1:9000  

  16. ; Unix user/group of processes
  17. ; Note: The user is mandatory. If the group is not set, the default user's group
  18. ;       will be used.
  19. user = webadmin
  20. group = webadmin

  21. ; The address on which to accept FastCGI requests.
  22. ; Valid syntaxes are:
  23. ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
  24. ;                            a specific port;
  25. ;   'port'                 - to listen on a TCP socket to all addresses on a
  26. ;                            specific port;
  27. ;   '/path/to/unix/socket' - to listen on a unix socket.
  28. ; Note: This value is mandatory.
  29. listen = 127.0.0.1:9000
复制代码
可根据需求进行优化设置

4. 制作fpm启动服务
复制下面的代码,vi /etc/init.d/php-fpm,保存
修改可执行权限  chmod +x /etc/init.d/php-fpm
启动   /etc/init.d/php-fpm start
停止   /etc/init.d/php-fpm stop
重启   /etc/init.d/php-fpm restart

Java代码
  1. #!/bin/bash   
  2. # php-fpm Startup script for php-fpm, a FastCGI implementation   
  3. # this script was created by tony at 2010.07.21, based on jackbillow's nginx script.   
  4. # it is v.0.0.1 version.   
  5. # if you find any errors on this scripts,please contact tony.   
  6. # by sending mail to tonytzhou at gmail dot com.   
  7. #   
  8. # chkconfig: - 85 15  
  9. # description: php-fpm is an alternative FastCGI implementation, with some additional features useful for sites of any size, especially busier sites.   
  10. #   
  11. # processname: phpfpm   
  12. # pidfile: /usr/local/var/run/phpfpm.pid   
  13. # config: /usr/local/etc/phpfpm.conf   
  14.   
  15. phpfpm=/home/programs/php/sbin/php-fpm   
  16. config=/home/programs/php/lib/php.ini   
  17. pid=/home/programs/php/run/php-fpm.pid   
  18.   
  19. RETVAL=0  
  20. prog="phpfpm"  
  21.   
  22. # Source function library.   
  23. . /etc/rc.d/init.d/functions   
  24.   
  25. # Source networking configuration.   
  26. . /etc/sysconfig/network   
  27.   
  28. # Check that networking is up.   
  29. [ ${NETWORKING} = "no" ] && exit 0  
  30.   
  31. [ -x $phpfpm ] || exit 0  
  32.   
  33. # Start phpfpm daemons functions.   
  34. start() {   
  35.   
  36. if [ -e $pid ];then   
  37.    echo "phpfpm is already running...."  
  38.    exit 1  
  39. fi   
  40.   
  41.    echo -n $"Starting $prog: "  
  42.    daemon $phpfpm -c ${config}   
  43.    RETVAL=$?   
  44.    echo   
  45.    [ $RETVAL = 0 ] && touch /var/lock/subsys/phpfpm   
  46.    return $RETVAL   
  47.   
  48. }   
  49.   
  50. # Stop phpfpm daemons functions.   
  51. stop() {   
  52.         echo -n $"Stopping $prog: "  
  53.         killproc $phpfpm   
  54.         RETVAL=$?   
  55.         echo   
  56.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/phpfpm /var/run/phpfpm.pid   
  57. }   
  58.   
  59. # reload phpfpm service functions.   
  60. reload() {   
  61.   
  62.     echo -n $"Reloading $prog: "  
  63.     #kill -HUP `cat ${pid}`   
  64.     killproc $phpfpm -HUP   
  65.     RETVAL=$?   
  66.     echo   
  67.   
  68. }   
  69.   
  70. # See how we were called.   
  71. case "$1" in   
  72. start)   
  73.         start   
  74.         ;;   
  75.   
  76. stop)   
  77.         stop   
  78.         ;;   
  79.   
  80. reload)   
  81.         reload   
  82.         ;;   
  83.   
  84. restart)   
  85.         stop   
  86.         start   
  87.         ;;   
  88.   
  89. status)   
  90.         status $prog   
  91.         RETVAL=$?   
  92.         ;;   
  93. *)   
  94.         echo $"Usage: $prog {start|stop|restart|reload|status|help}"  
  95.         exit 1  
  96. esac   
  97.   
  98. exit $RETVAL  

  99. #!/bin/bash
  100. # php-fpm Startup script for php-fpm, a FastCGI implementation
  101. # this script was created by tony at 2010.07.21, based on jackbillow's nginx script.
  102. # it is v.0.0.1 version.
  103. # if you find any errors on this scripts,please contact tony.
  104. # by sending mail to tonytzhou at gmail dot com.
  105. #
  106. # chkconfig: - 85 15
  107. # description: php-fpm is an alternative FastCGI implementation, with some additional features useful for sites of any size, especially busier sites.
  108. #
  109. # processname: phpfpm
  110. # pidfile: /usr/local/var/run/phpfpm.pid
  111. # config: /usr/local/etc/phpfpm.conf

  112. phpfpm=/home/programs/php/sbin/php-fpm
  113. config=/home/programs/php/lib/php.ini
  114. pid=/home/programs/php/run/php-fpm.pid

  115. RETVAL=0
  116. prog="phpfpm"

  117. # Source function library.
  118. . /etc/rc.d/init.d/functions

  119. # Source networking configuration.
  120. . /etc/sysconfig/network

  121. # Check that networking is up.
  122. [ ${NETWORKING} = "no" ] && exit 0

  123. [ -x $phpfpm ] || exit 0

  124. # Start phpfpm daemons functions.
  125. start() {

  126. if [ -e $pid ];then
  127.    echo "phpfpm is already running...."
  128.    exit 1
  129. fi

  130.    echo -n $"Starting $prog: "
  131.    daemon $phpfpm -c ${config}
  132.    RETVAL=$?
  133.    echo
  134.    [ $RETVAL = 0 ] && touch /var/lock/subsys/phpfpm
  135.    return $RETVAL

  136. }

  137. # Stop phpfpm daemons functions.
  138. stop() {
  139.         echo -n $"Stopping $prog: "
  140.         killproc $phpfpm
  141.         RETVAL=$?
  142.         echo
  143.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/phpfpm /var/run/phpfpm.pid
  144. }

  145. # reload phpfpm service functions.
  146. reload() {

  147.     echo -n $"Reloading $prog: "
  148.     #kill -HUP `cat ${pid}`
  149.     killproc $phpfpm -HUP
  150.     RETVAL=$?
  151.     echo

  152. }

  153. # See how we were called.
  154. case "$1" in
  155. start)
  156.         start
  157.         ;;

  158. stop)
  159.         stop
  160.         ;;

  161. reload)
  162.         reload
  163.         ;;

  164. restart)
  165.         stop
  166.         start
  167.         ;;

  168. status)
  169.         status $prog
  170.         RETVAL=$?
  171.         ;;
  172. *)
  173.         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
  174.         exit 1
  175. esac

  176. exit $RETVAL
复制代码
5. 配置nginx
使用80端口,域名为www.demo.com


Java代码
  1. #user  nobody;   
  2. user webadmin webadmin;   
  3. worker_processes  4;   
  4. #error_log  logs/error.log;   
  5. #error_log  logs/error.log  notice;   
  6. #error_log  logs/error.log  info;   
  7. pid        logs/nginx.pid;   
  8. # 指定文件描述符数量   
  9. worker_rlimit_nofile 51200;   
  10. events {   
  11.     # 使用网络I/O模型,linux推荐使用epoll, FressBSD推荐私用kqueue   
  12.     use epoll;   
  13.     # 允许链接数   
  14.     worker_connections  51200;   
  15. }   
  16. http {   
  17.     include       mime.types;   
  18.     default_type  application/octet-stream;   
  19.       
  20.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.                       '$status $body_bytes_sent "$http_referer" '  
  22.                       '"$http_user_agent" "$http_x_forwarded_for"';   
  23.     access_log  logs/access.log  main;   
  24.       
  25.     #autoindex off;   
  26.     # 设置字符集,如果多种字符集,不要设置   
  27.     #charset utf-8;   
  28.       
  29.     sendfile        on;   
  30.     keepalive_timeout  65;   
  31.       
  32.     fastcgi_connect_timeout 300;   
  33.     fastcgi_send_timeout 300;   
  34.     fastcgi_read_timeout 300;   
  35.     fastcgi_buffer_size 64k;   
  36.     fastcgi_buffers 4 64k;   
  37.     fastcgi_busy_buffers_size 128k;   
  38.     fastcgi_temp_file_write_size 128k;   
  39.       
  40.     #开启gzip   
  41.     gzip on;   
  42.     gzip_min_length 1k;   
  43.     gzip_buffers 4 16k;   
  44.     gzip_http_version 1.1;   
  45.     gzip_com_level 2;   
  46.     gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;   
  47.     gzip_vary on;   
  48.       
  49.     server {   
  50.         listen 80;   
  51.         server_name www.demo.com;   
  52.         index index.html index.htm index.php;   
  53.         root web-root;   
  54.            
  55.         # 图片缓存   
  56.         location ~* \.(?:ico|gif|jpe?g|png|bmp|swf)$ {   
  57.                 # Some basic cache-control for static files to be sent to the browser   
  58.                 expires max;   
  59.                 add_header Pragma public;   
  60.                 add_header Cache-Control "public, must-revalidate, proxy-revalidate";   
  61.         }   
  62.   
  63.         # 静态资源缓存   
  64.         location ~.*\.(js|css)?$   
  65.         {   
  66.             expires 1h;   
  67.         }   
  68.            
  69.         error_page   500 502 503 504  /50x.html;   
  70.         location = /50x.html {   
  71.             root   html;   
  72.         }   
  73.            
  74.         #   
  75.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  76.         #   
  77.         location ~ \.php$ {   
  78.             include /home/programs/nginx/conf/fastcgi_params;   
  79.             if ($uri !~ "^/statics/") {   
  80.                fastcgi_pass 127.0.0.1:9000; # fpm监听的端口和ip   
  81.             }   
  82.             fastcgi_index  index.php;   
  83.             fastcgi_param  SCRIPT_FILENAME  web-root$fastcgi_script_name;   
  84.             include        fastcgi_params;   
  85.         }   
  86.   
  87.     }   
  88.       
  89. }  

  90. #user  nobody;
  91. user webadmin webadmin;
  92. worker_processes  4;
  93. #error_log  logs/error.log;
  94. #error_log  logs/error.log  notice;
  95. #error_log  logs/error.log  info;
  96. pid        logs/nginx.pid;
  97. # 指定文件描述符数量
  98. worker_rlimit_nofile 51200;
  99. events {
  100.     # 使用网络I/O模型,linux推荐使用epoll, FressBSD推荐私用kqueue
  101.     use epoll;
  102.     # 允许链接数
  103.     worker_connections  51200;
  104. }
  105. http {
  106.     include       mime.types;
  107.     default_type  application/octet-stream;
  108.    
  109.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  110.                       '$status $body_bytes_sent "$http_referer" '
  111.                       '"$http_user_agent" "$http_x_forwarded_for"';
  112.     access_log  logs/access.log  main;
  113.    
  114.     #autoindex off;
复制代码
# 设置字符集,如果多种字符集,不要设置
  1.   #charset utf-8;
  2.    
  3.     sendfile        on;
  4.     keepalive_timeout  65;
  5.    
  6.     fastcgi_connect_timeout 300;
  7.     fastcgi_send_timeout 300;
  8.     fastcgi_read_timeout 300;
  9.     fastcgi_buffer_size 64k;
  10.     fastcgi_buffers 4 64k;
  11.     fastcgi_busy_buffers_size 128k;
  12.     fastcgi_temp_file_write_size 128k;
  13.    
  14.     #开启gzip
  15.     gzip on;
  16.     gzip_min_length 1k;
  17.     gzip_buffers 4 16k;
  18.     gzip_http_version 1.1;
  19.     gzip_com_level 2;
  20.     gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  21.     gzip_vary on;
  22.    
  23.     server {
  24.         listen 80;
  25.         server_name www.demo.com;
  26.         index index.html index.htm index.php;
  27.         root web-root;
  28.         
  29.         # 图片缓存
  30.         location ~* \.(?:ico|gif|jpe?g|png|bmp|swf)$ {
  31.                 # Some basic cache-control for static files to be sent to the browser
  32.                 expires max;
  33.                 add_header Pragma public;
  34.                 add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  35.         }

  36.         # 静态资源缓存
  37.         location ~.*\.(js|css)?$
  38.         {
  39.             expires 1h;
  40.         }
  41.         
  42.         error_page   500 502 503 504  /50x.html;
  43.         location = /50x.html {
  44.             root   html;
  45.         }
  46.         
  47.         #
  48.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  49.         #
  50.         location ~ \.php$ {
  51.             include /home/programs/nginx/conf/fastcgi_params;
  52.             if ($uri !~ "^/statics/") {
  53.                fastcgi_pass 127.0.0.1:9000; # fpm监听的端口和ip
  54.             }
  55.             fastcgi_index  index.php;
  56.             fastcgi_param  SCRIPT_FILENAME  web-root$fastcgi_script_name;
  57.             include        fastcgi_params;
  58.         }

  59.     }
  60.    
  61. }
复制代码
配置好后保存nginx.conf,

6. 启动nginx

Java代码
  1. nginx-root/bin/nginx -c nginx-root/conf/nginx.conf  

  2. nginx-root/bin/nginx -c nginx-root/conf/nginx.conf
复制代码
访问http://www.demo.com就可以了
注: www.demo.com需要绑定到hosts中

后续会增加rewrite的一些自己的理解和总结

论坛徽章:
0
发表于 2012-03-01 22:40 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP