ITPub博客

首页 > 应用开发 > IT综合 > 【Nginx】php+nginx快速搭建

【Nginx】php+nginx快速搭建

原创 IT综合 作者:小亮520cl 时间:2015-12-10 17:21:46 0 删除 编辑
参考文档
http://blog.csdn.net/dyllove98/article/details/41120789
http://www.cnblogs.com/skynet/p/4146083.html

安装php php-fpm

  1. 安装 开发软件包:yum -y groupinstall "Development Tools"
  2. 安装 mysql : yum -y install mysql mysql-server mysql-devel
  3. 下载php-5.6.2 wget http://cn2.php.net/distributions/php-5.6.2.tar.gz
  4.  解压 tar -zxvf php-5.6.2.tar.gz
  5.  切换到 php-5.6.2
  6.  ./configure \
  7. --prefix=/usr/local/php \
  8. --with-config-file-path=/usr/local/php/etc \
  9. --enable-fpm \
  10. --with-fpm-user=php-fpm \
  11. --with-fpm-group=php-fpm \
  12. --with-mysql=mysqlnd \ //这里mysqlnd 是内部查找命令
  13. --with-mysql-sock=/tmp/mysql.sock \
  14. --with-libxml-dir \
  15. --with-gd \
  16. --with-jpeg-dir \
  17. --with-png-dir \
  18. --with-freetype-dir \
  19. --with-iconv-dir \
  20. --with-zlib-dir \
  21. --with-mcrypt \
  22. --enable-soap \
  23. --enable-gd-native-ttf \
  24. --enable-ftp \
  25. --enable-mbstring \
  26. --enable-exif \
  27. --disable-ipv6 \
  28. --with-pear \
  29. --with-curl \
  30. --with-openssl
  31. 出现未安装的错误,直接用yum 进行安装即可 记得不要忘了装上 -devel (libcurl libpng libcrul12等等),dir报错就去掉--with-dir再configure
  32. 出现找不到文件路径的情况下 用find / -name 'name'去查找一下
  33. 出现warning 的情况大多是因为版本已经默认安装了,可以去掉该行

  34. make
  35. make install

点击(此处)折叠或打开

  1. cd php-5.6.2
  2. cp php.ini-production /usr/local/php/etc/php.ini
  3. cp /usr/local/php/etc/php-fpm.conf.default.conf php-fpm.conf
  4.  
  5.         
  6. #测试php-fpm配置
  7. /usr/local/php/sbin/php-fpm -t 或
  8. /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -t
  9. 如果出现诸如 “test is successful” 字样,说明配置没有问题
  10.         
  11. #启动php-fpm
  12. /usr/local/php/sbin/php-fpm 或
  13. /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf

  14. 启动PHP报错ERROR: [pool www] cannot get uid for user '@php_fpm_user@' 
  15. vi php-fpm.conf 
  16. 将user与group改为系统存在的用户,从新启动即可!!! 
  17. user=www 
  18. group=www
  19. 。

php5.3.3以前
我们用php-fpm来进行重新加载配置文件(如php.ini):
  1. /usr/local/php/sbin/php-fpm reload

  2. 注:/usr/local/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate

php 5.3.3 后的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:
  1. master进程可以理解以下信号

  2. INT, TERM 立刻终止
  3. QUIT 平滑终止
  4. USR1 重新打开日志文件
  5. USR2 平滑重载所有worker进程并重新载入配置和二进制模块

  6. 示例:
  7. php-fpm 关闭:
  8. kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

  9. php-fpm 重启:
  10. kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

  11. 查看php-fpm进程数:
  12. ps aux | grep -c php-fpm




搭建nginx
  1. wget http://nginx.org/download/nginx-1.6.2.tar.gz //最新稳定版哦
  2. yum安装pcre-devel

  3. 解压nginx
  4. tar zxvf nginx-1.6.2.tar.gz

  5. 配置编译参数
  6. cd nginx-1.6.2
  7. ./configure \
  8. --prefix=/usr/local/nginx \
  9. --with-http_realip_module \
  10. --with-http_sub_module \
  11. --with-http_gzip_static_module \
  12. --with-http_stub_status_module \
  13. --with-pcre

  14. 编译nginx
  15. make

  16. 安装nginx
  17. make install

添加软连接
ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin

测试
nginx -t 


修改nginx配置文件让其加载php-fpm模块
  1. server部分放开php-fpm模块
  2.  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  3.         
  4.         location ~ \.php$ {
  5.             root html;
  6.             fastcgi_pass 127.0.0.1:9000;
  7.             fastcgi_index index.php;
  8.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    ----红色是与默认配置文件不一样的地方
  9.             include fastcgi_params;
  10.         }
启动
/usr/local/nginx/sbin/nginx

重启动nginx 
nginx -s reload



访问测试:
http://125.208.14.177/index.php   ---大功告成



补充:
修改默认web路径,与多个虚拟目录
  1. [root@node3 conf]# more nginx.conf

  2. #user nobody;
  3. worker_processes 1;

  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. events {
  9.     worker_connections 1024;
  10. }


  11. http {
  12.     include mime.types;
  13.     default_type application/octet-stream;

  14.     #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15.     # '$status $body_bytes_sent "$http_referer" '
  16.     # '"$http_user_agent" "$http_x_forwarded_for"';

  17.     #access_log logs/access.log main;

  18.     sendfile on;
  19.     #tcp_nopush on;

  20.     #keepalive_timeout 0;
  21.     keepalive_timeout 65;

  22.     #gzip on;

  23.     server {
  24.         listen 80;
  25.         server_name test1;

  26.         location / {              ----location访问规则详见文档
  27.             root html;               ---nginx1域名  默认访问web路径,如果访问nginx1/index.html   则访问的是/usr/local/nginx/html/目录
  28.             index index.html index.htm;
  29.         }

  30.         error_page 500 502 503 504 /50x.html;
  31.         location = /50x.html {
  32.             root html;
  33.         }

  34.         location ~ \.php$ {
  35.             root /var/www/html;       ---nginx1域名  默认php文件访问路径.如:访问的是nginx1/index.php 访问的是 /var/www/html/路径。。。一般与上面相比较没必要配置两个不一样的目录
  36.             fastcgi_pass 127.0.0.1:9000;
  37.             fastcgi_index index.php;
  38.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  39.             include fastcgi_params;
  40.         }

  41.     }
  42.     

  43.     server {
  44.         listen 80;
  45.         server_name test2;
  46.         location / {
  47.             root /var/www/html2; #目录
  48.             index index.html index.php;
  49.         }
  50.         error_page 500 502 503 504 /50x.html;
  51.         location = /50x.html {
  52.             root html;
  53.         }
  54.  
  55.         location ~ \.php$ {
  56.             root /var/www/html2; #php文件访问路径
  57.             fastcgi_pass 127.0.0.1:9000;
  58.             fastcgi_index index.php;
  59.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  60.             include fastcgi_params;
  61.         }
  62.     }

  63.    server {
  64.         listen 8080;
  65.         server_name a.ttlsa.com;
  66.         location / {
  67.             root /var/www/a.ttlsa.com; #目录
  68.             index index.html index.php;
  69.         }
  70.         error_page 500 502 503 504 /50x.html;
  71.         location = /50x.html {
  72.             root html;
  73.         }

  74.         location ~ \.php$ {
  75.             root /var/www/a.ttlsa.com; 
  76.             fastcgi_pass 127.0.0.1:9000;
  77.             fastcgi_index index.php;
  78.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  79.             include fastcgi_params;
  80.         }
  81.     }

  82.     server {
  83.         listen 8090;
  84.         server_name b.ttlsa.com;
  85.         location / {
  86.             root /var/www/b.ttlsa.com; #目录
  87.             index index.html index.php;
  88.         }
  89.         error_page 500 502 503 504 /50x.html;
  90.         location = /50x.html {
  91.             root html;
  92.         }

  93.         location ~ \.php$ {
  94.             root /var/www/b.ttlsa.com; 
  95.             fastcgi_pass 127.0.0.1:9000;
  96.             fastcgi_index index.php;
  97.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  98.             include fastcgi_params;
  99.         }
  100.     }




  101.     # another virtual host using mix of IP-, name-, and port-based configuration
  102.     #
  103.     #server {
  104.     # listen 8000;
  105.     # listen somename:8080;
  106.     # server_name somename alias another.alias;

  107.     # location / {
  108.     # root html;
  109.     # index index.html index.htm;
  110.     # }
  111.     #}


  112.     # HTTPS server
  113.     #
  114.     #server {
  115.     # listen 443 ssl;
  116.     # server_name localhost;

  117.     # ssl_certificate cert.pem;
  118.     # ssl_certificate_key cert.key;

  119.     # ssl_session_cache shared:SSL:1m;
  120.     # ssl_session_timeout 5m;

  121.     # ssl_ciphers HIGH:!aNULL:!MD5;
  122.     # ssl_prefer_server_ciphers on;

  123.     # location / {
  124.     # root html;
  125.     # index index.html index.htm;
  126.     # }
  127.     #}


  128. include vhost/*.conf;
  129. }


[root@node3 conf]#more /etc/hosts
192.168.6.119 nginx1 nginx2 a.ttlsa.com b.ttlsa.com



如上诉配置文件:

所以访问时如下:
[root@node3 conf]# curl test1
test1
[root@node3 conf]# curl test2              
test2
[root@node3 conf]# curl a.ttlsa.com:8090
this is a.ttlsa.com
[root@node3 conf]# curl b.ttlsa.com:8090
this is b.ttlsa.com






来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-1869911/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: oom killer
请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1127020