CentOS 上 Laravel 的权限问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37257975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:49:02  来源:igfitidea点击:

Permissions Issue with Laravel on CentOS

linuxapachelaravelpermissionscentos

提问by Martin

I've cloned a laravel repo to my CentOS 7 box. When I try to run it, I get a 500 error with nothing displayed.

我已经将一个 Laravel 存储库克隆到我的 CentOS 7 机器上。当我尝试运行它时,出现 500 错误,但没有任何显示。

So I check out /var/log/httpd/error_logand I see that I've got some permissions errors:

所以我检查了一下/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

I have done the following to try to overcome the issues:

我已经做了以下尝试来克服这些问题:

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

So it now shows as so:

所以它现在显示如下:

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

But that didn't work.

但这没有用。

Interestingly enough, I mis-typed some artisancommands earlier and those seemed to add logs to the logfile...

有趣的是,我之前打错了一些artisan命令,这些命令似乎将日志添加到日志文件中......

I already read/tried:

我已经阅读/尝试过:

回答by Martin

Turns out the issue is with selinux

原来问题出在 selinux

I found this answer, which solved my problem.

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

Prove this is the problem by turning off selinuxwith 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!

通过selinux使用命令关闭来证明这是问题所在

setenforce 0

这应该允许写入,但您已关闭添加的全服务器范围的安全性。那很糟。让 SELinux 回归

setenforce 1

然后最后使用 SELinux 允许使用此命令写入文件

chcon -R -t httpd_sys_rw_content_t storage

你走了!

回答by blamb

I need to make more adjustments for SELinux than just for storage. Especially the configdir can get you this issue on Laravel bootstrapping.

我需要对 SELinux 进行更多调整,而不仅仅是对storage. 特别是config目录可以让你在 Laravel 引导上遇到这个问题。

If you sudo setenforce permissiveand it works, then turn it back sudo setenforce enforcingthen follow below.

如果你sudo setenforce permissive和它工作,然后把它转回来,sudo setenforce enforcing然后按照下面的操作。

SELinux laravel setup:

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(/.*)?"

You may not need the following one for config, but i did. It may be safest not to run this one unless you need to:

您可能不需要以下一个 for config,但我做到了。除非您需要,否则最好不要运行此程序:

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

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

Then Reset after your dir adjustments have been made:

然后在您的目录调整完成后重置:

restorecon -Rv /var/www/site/

restorecon -Rv /var/www/site/

回答by Nitish Chauhan

try these worked for me ...

试试这些对我有用...

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

回答by wast

In my case it was another unix user so this one worked:

就我而言,它是另一个 unix 用户,所以这个用户有效:

chown -R php-fpm:php-fpm storage

回答by Ramesh KC

Apply this for whole project.

将此应用于整个项目。

chcon -R -t httpd_sys_rw_content_t project_folder

回答by treanorv

On CentOS 7 I had similar permissions issue. The solution which works for me is:

在 CentOS 7 上我有类似的权限问题。对我有用的解决方案是:

Add user centos to group apache

将用户centos添加到apache组

Add user apache to group centos

将用户 apache 添加到 centos 组

Restart httpd

重启httpd

The final line is vital as otherwise user apache will not be aware it is now also in group centos!

最后一行至关重要,否则用户 apache 将不会意识到它现在也在 centos 组中!

Same as user apache logging out and back in again.

与用户 apache 注销并再次登录相同。