thinkphp5部署于Linux中nginx多站点解决方案以及怎么去掉public

时间:2017/9/19 16:22:00来源:互联网 作者:flyso 点击: 2174 次

实验环境:
Linux、nginx、mysql、php(lnmp一键集成1.4测试版,更多信息lnmp.org)
本文兼顾的问题:
1.安全的配置(同一vps多站点)
2.友好的站点入口
部署问题重述:
由于thinkphp5经过重构,和thinkphp3.2有很大的不同,其中之前的入口文件index.php由/index.php改为/public/index.php,
那么问题来了:
1.如果按lnmp集成环境提供的方法(lnmp vhost add)添加虚拟站点,访问站点就要加个二级目录,如
http://xxx.com/public

所以为了可以让访问链接变得更加友好(即http://xxx.com),可以修改/usr/local/nginx/conf/vhost/xxx.com.conf(下称“专属配置”)

  1. root /home/wwwroot/xxx.com/;

改为:

  1. root /home/wwwroot/xxx.com/public;

那么问题来了,由于lnmp环境考虑到虚拟站点各自的访问目录权限问题,于是在/usr/local/nginx/conf/fastcgi.conf
里配置了

  1. fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

那么每个站点的访问权限就限制在各自的document_root里了,
于是,我们修改的root(root /home/wwwroot/xxx.com/public;)就把权限限制在public目录之后,
软件架构根目录下的application等都无法访问了,那么此时我们要解决的问题是:
2.既要保持root /home/wwwroot/xxx.com/public;,又要让虚拟站点有/home/wwwroot/xxx.com目录的访问权限
那么可以修改/usr/local/nginx/conf/fastcgi.conf配置文件:

  1. fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

改为:

  1. fastcgi_param PHP_ADMIN_VALUE "open_basedir=/home/wwwroot/xxx.com/:/tmp/:/proc/";

但问题来了,/usr/local/nginx/conf/fastcgi.conf是公共配置文件,采用这种写死的方式会导致其他站点不可用,
为了兼顾其他虚拟站点,if_not_empty派上用场了,
修改方法为:
不改变/usr/local/nginx/conf/fastcgi.conf配置文件里的原配置,而在

  1. fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

之后添加

  1. fastcgi_param PHP_ADMIN_VALUE $basedir if_not_empty;#注意nginx要在1.1.11版本之后

$basedir变量就可以在/usr/local/nginx/conf/vhost/xxx.com.conf配置文件里的include enable-php.conf前赋值:

  1. set $basedir "open_basedir=/home/wwwroot/dev.yunshare.net/:/tmp/:/proc/";

优点:这样既满足了thinkphp5的部署要求,又不影响其他一般站点的使用。
缺点:如果$basedir没有赋值(至少一个专属配置有赋值),nginx -t无法通过。

注意rewrite规则:
最后献上thinkphp的rewrite规则:
这种写法3.2是可以的,但在这里是不可以的,否则经测试pathinfo为空

  1. location / {
  2. try_files $uri $uri/ /index.php?$query_string;
  3. }

应该改为:

  1. location / {
  2.            if (!-e $request_filename){
  3.                rewrite ^/(.*)$ /index.php?s=/$1 last;
  4.            }
  5. }

好了,一篇关于thinkphp5的rewrite写到这,欢迎多多交流

总结:

  • lnmp环境
  • thinkphp框架不改变目录结构的情况下配置rewrite规则
  • Linux权限,安全问题

参考资料:
http://www.kancloud.cn/manual/thinkphp5/207415
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html


Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3