使用CentOS上的Laravel时出现权限问题

30

我已将Laravel仓库克隆到我的CentOS 7盒子上。当我尝试运行它时,出现500错误且没有显示任何内容。

所以我检查了/var/log/httpd/error_log,发现我有一些权限错误:

[Mon May 16 11:39:32.996441 2016] [:error] [pid 2434] [client 104.156.67.195:39136] PHP Fatal error:  Uncaught UnexpectedValueException: The stream or file "/var/www/html/MYSITE/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/html/MYSITE/bootstrap/cache/compiled.php:13701
Stack trace:
#0 /var/www/html/MYSITE/bootstrap/cache/compiled.php(13635): Monolog\\Handler\\StreamHandler->write(Array)
#1 /var/www/html/MYSITE/bootstrap/cache/compiled.php(13396): Monolog\\Handler\\AbstractProcessingHandler->handle(Array)
#2 /var/www/html/MYSITE/bootstrap/cache/compiled.php(13494): Monolog\\Logger->addRecord(400, Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
#3 /var/www/html/MYSITE/bootstrap/cache/compiled.php(13189): Monolog\\Logger->error(Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
#4 /var/www/html/MYSITE/bootstrap/cache/compiled.php(13160): Illuminate\\Log\\Writer->writeLog('error', Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
# in /var/www/html/MYSITE/bootstrap/cache/compiled.php on line 13701

我已经采取以下措施试图克服这些问题:

chmod -R 775 storage
chmod -R 775 vendor
chown -R apache:apache storage

现在它显示为:

-rwxrwxr-x. 1 apache apache 2156 May 16 11:41 storage/logs/laravel.log

但那没起作用。

有趣的是,我之前输错了一些artisan命令,但它们似乎仍将日志添加到日志文件中...

我已经阅读/尝试过:


可能是重复的问题:无法打开流:没有这个文件或目录 - Vic Seedoubleyew
7个回答

114

问题实际上与selinux有关

我找到了这个答案,它解决了我的问题。

Prove this is the problem by turning off selinux with the command

setenforce 0

This should allow writing, but you've turned off added security server-wide. That's bad. Turn SELinux back

setenforce 1

Then finally use SELinux to allow writing of the file by using this command

chcon -R -t httpd_sys_rw_content_t storage

And you're off!


2
我已经尝试了chmod,尝试了chown,检查了ACL,它们都不起作用...你帮我节省了很多时间...谢谢 - Snriud
2
我在这个问题上浪费了很多时间。这是让我重新开始的解决方案。 - devNoise
3
我希望你能够注意到,当我在CentOS上部署像laravel这样的PHP项目时,selinuxRHEL CentOS系统中默认的防火墙。谢谢。 - Tarek Kalaji
1
我在这个问题上花了大约2个小时...感谢你提供的解决方案... - Murat Cem YALIN
1
chcon是一个临时解决方案。使用semanage永久设置目录上下文。chcon设置的上下文将在重启后恢复为默认值。 - Eloar
显示剩余3条评论

10

除了对 storage 进行调整外,我还需要为SELinux进行更多的调整。 特别是 config 目录可能会在Laravel引导过程中出现问题。

如果您使用 sudo setenforce permissive 命令可以解决问题,则可以使用 sudo setenforce enforcing 命令将其恢复,然后按照以下步骤操作。

SELinux Laravel设置:

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/site/laravel/storage(/.*)?"
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/site/laravel/bootstrap/cache(/.*)?"

您或许不需要以下这个命令用于config,但是我需要。除非有需要否则最好不要运行此命令:

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/site/laravel/config(/.*)?"

当目录调整完成后,请重新设置:

restorecon -Rv /var/www/site/


2
这比使用chcon更好,因为如果有人调用restorecon或者文件系统重新标记,它将被重置。我同意存储和引导/缓存。然而,你真的不应该让Web服务器可写config文件夹。原始的Laravel肯定不需要config文件夹可写。 - wunch
我想对于我来说,如果我没记错的话,我写这篇文章的原因,是因为那个非常的dir。那是我的唯一问题config。因此,我不记得我的设置是什么了,很抱歉。我会说,除非你需要,否则不要进行配置。 - blamb
1
这是正确的答案,请记住 chcon 只是临时的。semanage 是永久的。 - Yohanim

4

尝试这些方法,对我来说很有效...

sudo find ./storage -type f -exec chmod 666 {} \;
sudo find ./storage -type d -exec chmod 777 {} \;

777权限是一种安全风险! - rendor9

1
在我的情况下,这是另一个unix用户,所以这个有效:
chown -R php-fpm:php-fpm storage

1

cd [laravelfolder]

chmod 777 storage -R

sudo chcon -t httpd_sys_rw_content_t storage -R

chmod 777 bootstrap

sudo chcon -t httpd_sys_rw_content_t bootstrap -R

0
在CentOS 7上,我遇到了类似的权限问题。对我有效的解决方法是:
将用户centos添加到组apache中
将用户apache添加到组centos中
重新启动httpd
最后一行非常重要,否则用户apache将不知道它现在也属于组centos!
就像用户apache退出并重新登录一样。

1
这对于任何生产服务器来说都太过宽松了。 - kurkle

0

我花了几个小时寻找一个简单的解决方案,但徒劳无功。我不得不学习SELinux的工作原理。这非常有帮助

按照以下步骤为php-fpm(httpd)提供适当的访问权限,在CentOS上为您的laravel应用程序提供服务。

由于某种原因,我无法在storage/bootstrap/cache/级别上执行此操作,因此我必须进入特定的文件夹。

0(a). sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/your_app(/.*)?"

0(b). restorecon -Rv /var/www/your_app

1(a). sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/your_app/storage/framework/views'

1(b). sudo restorecon -v /var/www/your_app/storage/framework/views'

2 (a). sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/your_app/storage/framework/cache'

2 (b). sudo restorecon -v /var/www/your_app/storage/framework/cache'

3 (a). sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/your_app/storage/framework/sessions'

3 (b). sudo restorecon -v /var/www/your_app/storage/framework/sessions'

4 (a). sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/your_app/bootstrap/cache/data'

4 (b). sudo restorecon -v /var/www/your_app/bootstrap/cache/data'

5 (a) sudo semanage fcontext -a -t httpd_log_t "/var/www/your_app/storage/logs"

5 (b) sudo restorecon -v /var/www/your_app/storage/logs'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接