nginx服务配置---php服务接入

前言:
  最近要搭建一个内部的wiki系统, 网上搜了一圈, 也从知乎上搜集了一些大神的评价和推荐. 重点找了几个开源的wiki系统, 不过发现他们都是采用php来实现的. 于是乎需要配置php环境, 来配合服务正常工作. 网上多是apache+php的组合方式, 不过由于个人是nginx脑残粉, 因此决定采用nginx+php fastcgi来配置下环境.

思路梳理:
  云主机是ubuntu系统(主要觉得apt好用, 当然centos的yum也是利器). 对于php, php-fpm, 以及nginx的安装, 决定采用apt来完成. 虽然网上很多大神建议采用编译安装, 这样可以定制, 不过这边为了简洁快速, 主要还是偷懒, ^_^.
  整个思路可分为如下步骤:
  1). php环境的安装和配置
  2). nginx环境的安装和配置
  3). 两者的集成

php环境安装和配置:
  ubuntu上安装php, 非常的简单, 重要的是不要遗落php重要的插件.

  # 安装php
  sudo apt-get install php5
  # 安装php的mysql插件
  sudo apt-get install php5-mysql
  # 安装php5-fpm(Fastcgi process manager)
  sudo apt-get install php5-fpm

  然后让我们简单检测下php是否安装成功.

  

  然后在让我们简单过一下配置文件的组织结构和重要的相关项.
  
  php5-fpm的默认目录在/etc/php5/fpm中, 其修改项主要集中在pool.d/www.conf中, 让我们来看一下, 里面有那些需要注意一下.
  • 子进程的执行用户和组

#user=www-data
user=work
#group=www-data
group=work

  注: 其默认的用户:组为www-data:www-data, 这边改为work:work. 若没有, 可以通过adduser/addgroup来完成用户和组的添加.
  • 监听模式

#listen=/var/run/php5-fpm.sock
listen=127.0.0.1:9000

  注: 后续版本的php-fpm. 更推荐unix域(性能更优异), 而不是tcp的模式. 这边还是选用tcp监听端口的方式.
  • 子进程数控制

pm = dynamic
pm.max_children = 10
pm.start_server = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10 

  其具体的含义, 借助官方的注释来说话.

static - a fixed number (pm.max_children) of child processes;
dynamic - the number of child processes are set dynamically based on the
  following directives. With this process management, there will be
  always at least 1 children.
  pm.max_children - the maximum number of children that can
    be alive at the same time.
  pm.start_servers - the number of children created on startup.
  pm.min_spare_servers - the minimum number of children in 'idle'
    state (waiting to process). If the number
    of 'idle' processes is less than this
    number then some children will be created.
  pm.max_spare_servers - the maximum number of children in 'idle'
    state (waiting to process). If the number
    of 'idle' processes is greater than this
    number then some children will be killed.

  然后启动php-fpm后台服务.

service php5-fpm start|stop|restart|status

  推荐使用service来启动php5-fpm.
  启动的效果如下所示:
  

nginx环境的安装和配置:
  nginx的安装也异常的简单.

sudo apt-get install nginx

  其默认的配置文件路径为/etc/nginx/nginx.conf.

nginx和php的集成:
  现在到了最后的集成工作了, 可再/etc/nginx/nginx.conf中, 添加如下规则即可.

server {
  location ~ .*.php$ {
    root /path/to/phpwebapp;
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

  注: 由于php-fpm采用的fastcgi协议, 因此这边需要填写fastcgi_pass, 而不是之前proxy_pass那种形式.

  最后把php项目, 放入到/path/to/phpwebapp(实际为/home/work/phpwebapps)的目录中. 通过浏览器即可访问成功, nginx和php的集成工作也告一段落.

问题和追查:
  实际在配置和部署中, 并没有上述描述的一帆风顺. 遇到最多的是404和403错误.
  404错误为File Not Found, 这个一般由两种原因导致, 一种是路径真的配置错了, 另一种是访问权限(linux帐号)的问题(这个比较隐蔽, 难以一时找到). php子进程需要借助$document_root$fastcgi_script_name来获取正确的位置信息.
  403错误为权限受限(Forbidden), 还是访问权限(linux帐号)的问题. php子进程的执行用户, 需要拥有访问目录的读权限.
  php文件直接下载, 这个问题, 往往是php在nginx的配置中没设置好. 比如匹配规则没命中(未匹配, 被人优先匹配).

总结:
  nginx+php的集成, 网上资料很多, 而且介绍篇幅也不大, 让人有些飘飘然, 觉得很容易. 但真正实践起来, 却总有不少问题. 还是得沉下心来做事, 踏踏实实的做笔记, ^_^.

公众号&游戏站点:
  个人微信公众号: 木目的H5游戏世界
  
  个人游戏作品集站点, 请点击访问: www.mmxfgame.com 

原文地址:https://www.cnblogs.com/mumuxinfei/p/5000248.html