vagrant的学习 之 ThinkPHP5.1
vagrant的学习 之 ThinkPHP5.1
本文根据慕课网的视频教程练习,感谢慕课网!
慕课视频学习地址:https://www.imooc.com/video/14218。
慕课的参考文档地址:https://github.com/apanly/mooc/tree/master/va
(1)下载ThinkPHP5.1的框架:
如果使用git克隆框架,需要先安装git:
sudo apt-get install git
创建web目录:
mkdir /home/www
在www目录:
cd /home/www sudo git clone https://github.com/top-think/think.git tp5
然后进入自动创建的tp5目录,进行克隆核心代码:
cd tp5 sudo git clone https://github.com/top-think/framework thinkphp
(2)如果web服务器时nginx:
配置nginx:
cd /etc/nginx/conf.d/
创建tp5.conf
server{ server_name study.tp5.com; root /home/www/tp5/public; index index.php index.html; location / { if ( -f $request_filename){ break; } if ( !-e $request_filename){ rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ \.php{ set $script $uri; set $path_info ""; if ($uri ~ "^(.+\.php)(/.+)"){ set $script $1; set $path_info $2; } include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $script; try_files $uri = 404; } }
然后重启nginx:
方式一:sudo /etc/init.d/nginx restart
方式二:sudo service nginx restart
如果重启nginx失败,例如:
sudo /etc/init.d/nginx restart
* Restarting nginx nginx [fail]
解决方法,使用 sudo nginx -t查看错误原因:
nginx: [emerg] unknown directive "if(" in /etc/nginx/conf.d/tp5.conf:6 nginx: configuration file /etc/nginx/nginx.conf test failed
可以看出是if(的地方有问题,if和(中间增加空格后错误消失。
* Restarting nginx nginx [OK]
配置虚拟域名:
sudo vim /etc/hosts
ip地址 study.tp5.com
然后测试:
vagrant@vagrant-ubuntu-trusty:/etc$ curl -I 'study.tp5.com' HTTP/1.1 502 Bad Gateway Server: nginx/1.4.6 (Ubuntu) Date: Thu, 30 Aug 2018 02:23:47 GMT Content-Type: text/html Content-Length: 181 Connection: keep-alive
发现报502错误,查看nginx的错误日志,
打开nginx的配置文件,找到错误日志的文件的位置,然后打开查看具体错误。
cat /etc/nginx/nginx.conf
查找error_log的配置:
error_log /var/log/nginx/error.log;
cat /var/log/nginx/error.log [error] 1443#0: *3 connect() failed (111: Connection refused) while connecting to upstream,
client: 192.168.8.172, server: study.tp5.com, request: "HEAD / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "study.tp5.com"
连接被拒绝,进入php的配置 :
cd /etc/php5/fpm/pool.d
编辑www.conf:
sudo vim www.conf
找到 listen监听的位置:
listen = /var/run/php5-fpm.sock
sock域方式没有端口方式稳定,修改为端口监听。
listen = 127.0.0.1:9000
重启fpm:
方式一:sudo service php5-fpm restart
方式二:sudo /etc/init.d/php5-fpm restart
再次访问域名,发现报500错误:
[error] 884#0: *3 FastCGI sent in stderr: "PHP message: PHP Parse error: syntax error, unexpected '.',
expecting '&' or variable (T_VARIABLE) in /home/www/tp5/thinkphp/library/think/Loader.php on line 391" while reading response header from upstream,
client: 192.168.8.172, server: study.tp5.com, request: "HEAD / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "study.tp5.com"
打开文件:thinkphp/library/think/Loader.php,找到391行,发现
public static function factory($name, $namespace = '', ...$args)
这个$args参数前多了三个点,可能是这个错误,于是就删除这三个点,重新访问 study.tp5.com 。
显示 ThinkPHP V5.1的界面,访问成功!
(3)如果web服务器时apache:
进入apache的配置目录,新建一个单独的tp5的配置文件,
cd /etc/apache2/sites-enabled sudo touch tp5.conf
编辑文件:
<VirtualHost *:8888> ServerName study.tp5.com DocumentRoot /home/www/tp5/public/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
然后就可以访问了,接下来配置url重写,
tp5框架下的目录public下的.htaccess默认是不生效的,需要开启apache的重写模块:
sudo a2enmod rewrite
然后修改apache的配置:
<Directory />
Options FollowSymLinks
AllowOverride None #修改成AllowOverride ALL 打开rewrite
#Require all denied #把这行屏蔽掉,拒绝一切链接
</Directory>
然后重启apache
sudo service apache2 restart 或者 sudo /etc/init.d/apache2 restart
测试 http://study.tp5.com:8888,显示:
:)
ThinkPHP V5.1
12载初心不改(2006-2018) - 你值得信赖的PHP框架
配置成功!
总结:
安装过程中出现了很多错误,有不小心敲错的,也有可能是框架里边的错误,需要一步一步耐心解决。
欢迎大家指点~