PHP之Laravel框架使用问题汇总与解决方式

Laravel作为市场上最受欢迎的PHP MVC框架之一,有许多开发者用户在使用Laravel。最近刚刚接触Laravel框架,也遇到了一些问题,这里总结经验并将解决方式记录如下,希望对遇到同样问题的开发者有所帮助。

问题一:多环境下Apache服务器无法启动

使用wamp或xampp等PHP集成安装环境,遇到Apache服务器无法启动,错误提示如下:

19:30:45 [Apache] Attempting to start Apache app...
19:30:45 [Apache] Status change detected: running
19:30:50 [Apache] Status change detected: stopped
19:30:50 [Apache] Error: Apache shutdown unexpectedly.
19:30:50 [Apache] This may be due to a blocked port, missing dependencies,
19:30:50 [Apache] improper privileges, a crash, or a shutdown by another method.
19:30:50 [Apache] Press the Logs button to view error logs and check
19:30:50 [Apache] the Windows Event Viewer for more clues
19:30:50 [Apache] If you need more help, copy and post this
19:30:50 [Apache] entire log window on the forums

从上述错误提示可以看出是端口占用导致的问题,从apache/conf/httpd.conf配置文件查看监听端口,默认为80端口,即浏览器默认端口。

Windows系统下解决方式:

第一步:打开命令提示符,使用命令netstat -ano 来查看端口的占用情况。
命令:netstat -ano|findstr “80” 查看指定端口80的占用情况。

查看端口占用

从图中我们可以看到监听80端口状态为Lisening的进程PID为118856。

第二步:知道了进程号,可以通过查看任务管理器服务比对PID号找出对应进程,或者使用命令tasklist来列出进程进行确认。
命令:tasklist|findstr “118856”

查看进程号

httpd.exe是Apache相关程序,由于我机器同时安装了wamp,所以httpd.exe进程实际为wamp的Apache进程,其占用了80端口,所以xampp的Apache服务便无法再启动。

第三步:找到占用Apache端口的进程后,使用命令终止该进程。
命令:taskkill /f /t /im httpd.exe
强制终止映像名为httpd.exe的进程和任何由此启动的子进程。
或者使用命令:taskkill /pid 118856
通过pid号来终止进程。

终止进程

打开Xampp控制面板重启启动Apache服务,启动成功,如图:

Apache重启成功

Linux系统下,可以使用如下命令操作(此处用nginx举例)

查看端口占用情况:

netstat -anp | grep 80

lsof -i:80

Linux系统netstat命令

查看进程号对应进程的安装目录

ps –ef|grep 33308

查看进程的位置

查看nginx进程的端口使用情况

ps –aux|grep nginx

ps –aux|grep 42355

确定进程号后使用kill命令终止进程。命令格式:kill [信号或选项] PID(s)

kill –l可查看所有信号列表

SIGTERM - 此信号请求一个进程停止运行。此信号是可以被忽略的。进程可以用一段时间来正常关闭,一个程序的正常关闭一般需要一段时间来保存进度并释放资源。换句话说,它不是强制停止。

SIGKILL - 此信号强制进程立刻停止运行。程序不能忽略此信号,而未保存的进度将会丢失。

默认信号(当没有指定的时候)是SIGTERM。当它不起作用时,可以使用下面的命令来强制kill掉一个进程:

kill SIGKILL PID

或者

kill -9 PID

(这里”-9”代表着SIGKILL信号)

同时终止多个进程:

kill -9 PID1 PID2 PID3

上述nginx进程的终止命令为:

kill -9 42355.

除了查询占用端口后杀死进程的方式,还可以采用添加、修改监听端口的方式,这样不用终止相关端口的进程,不影响现有程序进程的正常使用。Apache需要修改的配置文件具体如下:

默认80端口,修改配置文件/apache/conf/httpd.conf
HTTPS默认443端口,修改配置文件/apache/conf/extra/httpd-ssl.conf

nginx的端口号修改、设置就更简单了,这里无须多说了。

问题二:页面出现循环重定向

首先要保证相关文件配置没有问题,依然出现循环重定向提示。先看一下配置文件。
配置文件/apache/conf/extra/httpd-vhosts.conf配置内容:

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot "F:/xampp/htdocs"
   ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webapp@dummy-host2.example.com
    DocumentRoot "F:/xampp/htdocs/bi.xxx.com/public"
    ServerName www.bitest.com
    ErrorLog "logs/webapp-host2.example.com-error.log"
    CustomLog "logs/webapp-host2.example.com-access.log" common
</VirtualHost>

接着本地host文件设置对应关系 127.0.0.1 www.bitest.com,配置完毕。然后访问该域名,报错如下图:

重定向循环

重定向循环提示

从入口文件public/index.php开始,步步跟踪程序,发现是由于权限问题导致。具体问题定位到文件位置:
F:\xampp\htdocs\bi.feiliu.com\vendor\laravel\framework\src\Illuminate\Foundation\ ProviderRepository.php

代码块:

    /**
     * Write the service manifest file to disk.
     *
     * @param  array  $manifest
     * @return array
     */
    public function writeManifest($manifest)
    {
        $path = $this->manifestPath.'/services.json';

        $this->files->put($path, json_encode($manifest, JSON_PRETTY_PRINT));

        return $manifest;
    }

文件目录不存在或者文件夹没有可写权限,使得writeManifest方法写配置失败,从而抛出异常错误(app/controllers/ErrorController),我的项目中异常错误处理方式为跳转到/error?code=201。

解决方法:

检查app\storage\meta\services.json 目录是否存在,检查目录的写权限。如果在Linux环境下首先要保证写入权限,还要确保SELinux允许在此目录写入。操作步骤如下:
1)更改 app/storage目录的属主

chown apache:apache app/storage

2)更改 app/storage 目录权限

chmod -R 775 app/storage

3)禁止 SELinux 对app/storage目录的权限限制

su -c "chcon -R -h -t httpd_sys_script_rw_t [fullpath]/app/storage"

OR

setsebool -P httpd_unified 1

问题三:Error in exception handler 或failed to open stream:permission denied in storage/meta/services.json

导致该问题的原因是文件目录权限不足的问题。有些服务器出于安全考虑可能会禁止一些目录的权限。解决方式可参考:

chgrp -R www-data app/storage (chgrp -R apache app/storage)

Or with chown.

chown -R :www-data app/storage

On Mac, the above commands did not work. However, this command did:

sudo chown -R _www app/storage 

(replace _www with your Apache server name if necessary)

Or with chmod.

chmod -R 775 app/storage

改变服务器属主(通常为apache 或 www-data,可能因操作系统不同而名称不同)的方式比将文件所有权限开放给用户安全的多,一般情况775的权限已经足够了。

From the Laravel web site : http://laravel.com/docs/4.2/installation

Laravel may require one set of permissions to be configured: folders within app/storage require write access by the web server.

最后,有一点需要特别注意的是尽量安装较新版本的 PHP安装包。Laravel 5.0对 PHP 版本的要求是 >=5.4,Laravel 5.1 要求 PHP 版本 >=5.5.9。 PHP 5.4 是最后一个支持 Windows XP 和 Windows 2003 的版本了。已证实从PHP5.5开始不再支持Windows XP了,你可能不得不升级Windows7/8了。

参考文章:

httpd_selinux
services.json failed to open stream: Permission denied in Laravel 4
Error in exception handler. - Laravel
在 Windows 上快速安装并运行 Laravel 5.x
4 Effective Methods to Disable SELinux Temporarily or Permanently
Disable SELinux for only Apache / httpd in Linux

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值