Error 500 after installing Laravel 5.3 - php

I have a Laravel project (version 5.3) that works well on my localhost (wampserver).
Now I want to run this project on a subdomain of my website.I have ssh root access and a vps user access (not admin - with Directadmin - PHP version 5.4).
I uploaded project files to my server but when I try to see the project result , it gives me a 500 error.
I have tried to change 'storage' folder permissions , so I read this answer , but when I run "chchon" ssh command , I get errors like this :
can't apply partial context to unlabeled file ...
also , I have tested Laravel with version 4.2 and it worked well.
Does any boy have idea ?? Thanks

If you don't have SELinux enabled, you can set permissions simply with chmod or set the owner of the storage directory with chown to the web server's user. (which is probably www-data)
Try to run sudo chown -R www-data storage in the project directory

Try all this
// first delete the vendor folder inside your project root, then run
composer install
// laravel generates log file there
sudo chmod -R 777 storage/logs
// laravel cache the files here
sudo chmod -R 777 bootstrap/cache
// vendor folder, where laravel saves their dependencies
sudo chmod -R 777 storage/ vendor/
// generate a application key
php artisan key:generate
This might help :)

Related

Laravel - Check storage folder permissions through custom php script

I don't know how laravel handles it, but can anybody give me a quick overview of how does laravel check folder permissions (storage and bootstrap)?
I have tried using is_writable("storage") but it doesn't seem to work atleast on a laravel installation on WAMP server. It gives true every time I try to run it.
Any other suggestions?
The storage and bootstrap/cache should be writable by your web server or Laravel will not run according to documentation (Directory Permissions
section).
So it's obvious that is_writable("storage") will always return true on your running Laravel app.
Note: You can use following commands to configure directory permissions after installing Laravel:
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

Laravel move project from Ubuntu Server to Local XAMPP

I have a Laravel project that I copied from my Ubuntu server and now I am trying to run it my local machine (XAMPP on Mac) I have been struggling with this for a few days now and I feel like I am going insane.
When I paste my project in XAMPP htdocs folder I get this error:
View [welcome] not found
Which php artisan cache:clear makes that go away, then I get this error:
The bootstrap/cache directory must be present and writable
Then I do this, php artisan cache:clear which gives me a new error:
Class view does not exist
Then after that no matter what I do either in terminal or viewing the web browser, I always get the error
Class view does not exist
Then I have tried composer update still the same error.....what am I doing wrong?
This has been a nightmare.
Last time i checked Laravel doesnt run on XAMPP but rather on the PHP installed when installing XAMPP so the project can be saved anywhere on the computer.
Given this being the fact, you will need to just have an active PHP installation and then you copy only the relevant files of the project onto the new computer (such files that you will get when you push your project onto GitHub). It doesn`t come with cache issues then all you need to do afterwards is to
php artisan key:generate
then composer install or composer update to get the vendor packages from online
My money right now is on picking the relevant files and reinstall with them
According to my own installation when changing the computer this is the list you will have to copy
I just tried to reproduce your issue on my mac. So i have installed XAMPP with the PHP version 7.1.25 which is the equivalent version of my local PHP version
So I installed the XAMPP and started server.
Downloaded my laravel project folder from my ubuntu server and copied it to htdocs (XAMPP)
When i tried to run http://localhost/myproject/public it shows the exception like
There is no existing directory at "/Applications/XAMPP/xamppfiles/htdocs/myproject/storage/logs" and its not buildable: Permission denied
Then i gave full permission to the storage folder
chmod -R 777 storage
And changed ownership for the files inside myproject folder.
Here i just checked the ownership of the dashboard directory which is running perfectly and given the same user ownership of myproject directory.
chown -R root:admin .
Then following commands
composer install
php artisan cache:clear
php artisan view:clear
php artisan route:clear
After this my laravel code runs perfectly.
Class view does not exist
is probably a ownership issue of the directory
For me (when developing on xampp, what I do for all my projects) - I'd not recommend to put your stuff in the htdocs folder. Laravel expects to not be hosted on a subfodler e.g. (localhost/my-project). So you should set up a virtual host in order to make it work easily (e.g. my-project.test) which is a bit annoying.
Simple solution is using the php artisan serve command in order to simply setup a local server on port 8000.
Don't forget to start xampp for the mysql server.
Some typical tips were already mentioned:
delete vendor folder & run composer install (install composer if you haven't)
run composer dump-autoload
run php artisan key:generate
ofcourse don't forget the migration php artisan migrate
and clear your full cache php artisan cache:clear
Usually you do not need to set any file permissions afaik
chmod -R 777 storage/
If you have a different user for apache2 (usually www-data), also do:
chown -R www-data storage/
You could also check if it runs with the built-in server:
php artisan serve
You can create .htaccess file and add below data into .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
After create .htaccess file, set laravel root path in terminal and run below command in terminal
php artisan serve
Since your Apache is already serving then you have permission problems only. And since you're using Mac, the default user name and group in Apache conf is _www for Mac and not www-data that is for Ubuntu. You can see it in the httpd.conf file :
<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User _www
Group _www
</IfModule>
Now, use terminal, get in your project directory let's say cd /var/www/laravel-project1 and make sure that the group _www (or the user too in some case of your App environment logic` has access (read and write) to :
All public directory and sub-directories containing assets if you have.
sudo chgrp -R _www public
sudo chmod -R 774 public
Storage directory and sub-directories specified here (storage/framework -> all, storage/logs -> all, storage/app -> public directory only), and bootstrap/cache directory and files
sudo chgrp -R _www storage/framework storage/logs storage/app/public bootstrap/cache
sudo chmod -R 774 storage/framework storage/logs storage/app/public bootstrap/cache
That should get rid of all of your permissions problem to access pages.
BUT now if on using the page, sessions and logs files that are created you get other problems, there might be a last problem of permission which is called UMask, which tell Apache or Web Server like Nginx what permission to assign to newly created directory or files for the user _www. By default Apache umask is 0002, which give 0775 for directory and 0664 for new file. If ever umask value was changed to 0022 like it's the default in Nginx, then the equivalent permissions 0755 or 0644 will not be sufficient for your Apache group _www to write in the directories that have group _www. So you can either change umask to 0002 or change owner to _www :
sudo chown -R _www public storage/framework storage/logs storage/app/public
So that depends on your configs.
I ran into the same problem as you, but not moving from ubuntu to mac, it was from windows to linux, I was in a total mess, but only git rescued me, it might give you a bit of pain, but it is going to save you in the future.
Here is the steps you need to do.
Create empty repository on the mac using this command git init --bare.
Clone the repository to the ubuntu using git clone.
Copy your laravel code to the clone you made in step 2.
Push the files from the ubuntu to the MAC.
Test the project.
The directory you will create in the mac, it can be inside the htdocs of the xammp.
I know it might be painful task to do, but it is quite worth it.
Sources for more information:
git-scm
Getting Git on Server
If you need more help, I'll be more than happy to discuss it with you.
Note: The following works for Laravel 5.x but also 4.2, not tested with other versions
Why not using Git?
(If you are not familiar with it, have a look at the official website, there are also tons of tutorials on the web)
Usually, copy-pasting entire projects is not a good idea, because of some file/directory permissions and other not-so-good stuff.
That's what I did to move my project from Windows to my Ubuntu Server:
Put your project on a git repository (GitHub, GitLab, or whatever), the .gitignore files provided with the Laravel apps are, in most cases, good enough
On your new machine, clone your repository
Do a
composer dump-autoload
composer install
To migrate your db, do
php artisan migrate
and if you have seeding, do this
php artisan db:seed
Then, if you have problem with file/folder permissions, do not EVER do a chmod -R 777 path/, if you have to do this to solve your problem, you are doing something wrong.
This command grants all privileges to anyone to all the files and folders in the path folder.
In your case, you have to do the following:
First, find which username is apache using to run the server (usually it's www-data)
ps aux | egrep '(apache|httpd)'
Then, change the project directory owner to apache's user (example for www-data apache user)
sudo chown -R www-data:www-data /var/htdocs/your-project/
Set folders permissions
sudo find /var/htdocs/your-project/ -type d -exec chmod 755 {} \;
Set files permissions
sudo find /var/htdocs/your-project/ -type f -exec chmod 644 {} \;
To fix the bootstrap/cache and storage/ permission problem, do
sudo chown -R www-data:www-data /var/htdocs/your-project/
Laravel 5.x
sudo chmod -R ug+rwx /var/htdocs/your-project/storage /var/htdocs/your-project/bootstrap/cache
Laravel 4.2
sudo chmod -R ug+rwx /var/htdocs/your-project/storage /var/htdocs/your-project/app/bootstrap/cache
Then, you should be good.
Doing that way, you can easily move your project from a machine to another without struggling with permission fixes or anything.
More info and source for files and folders permissions command-line instructions, see Laravel 5 Files Folders Permission and Ownership Setup
Go to your project folder add run these commands from terminal
sudo chmod -R 777 your_projrct/storage
sudo chmod -R 777 your_projrct/bootstrap/cache
sudo chown -R :www-data your_project
sudo chmod -R g+s your_project
then php artisan key:generate and composer install
when user/group www-data are unkown; most Linux distributions use apache:
chown -R apache:apache dirname
while on OSX, this would be user/group _www:
chown -R _www:_www dirname
adding the current user to group _www might make life easier, in general.
To isolate your issues:
Get your code into a repository(bitbucket or github)
Clone the repository into your local environment
Run composer install
Run php artisan serve. This way you rule out xampp as an issue.
In your browser go to localhost:8000
If you already have your entire codebase on your local box(including the vendor folder) then skip steps 1 and 2. Step 3 wouldn't hurt, but you can probably skip that too.
Once you get everything working, switch to xampp.
First of all:
composer update
composer dumpautoload
php artisan cache:clear
And then just configure a virtual host
1. Create a local domain for your app
Edit hosts file and redirect all requests from your domain to 127.0.0.1:
127.0.0.1 lara.vel
2. Configure a Virtual Host
Edit \xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost lara.vel:80>
DocumentRoot "C:\xampp\htdocs\laravel\public"
ServerAdmin laravel.dev
<Directory "C:\xampp\htdocs\laravel">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Done! Open up your domain in your browser, You'll see your project there!
The other way is simple, Just run artisan serve.
Please vote up if you found this answer useful.
I think if you copied the project from Ubuntu so it is a permissions problem.
first get users on your Mac machine by typing this in terminal
users
then copy the user you just got for example (username) and use it in this command
sudo chown -R username project-directory
then check for yourproject-directory/bootstrap/cache if it not exists, go create it. else run this command:
sudo chmod -R guo+w project-directory/bootstrap/cache
then
sudo chmod -R guo+w project-directory/storage
then clear composer autoload and cache and config using artisan command
composer dump-autoload
php artisan cache:clear
php artisan view:clear
php artisan route:clear
now try to run the project if the problem still exists,
you need to check config/app.php if it contains Illuminate\View\ViewServiceProvider::class
the view service provider.
if it is not there, so add it

PHP Laravel: failed to open stream: Permission denied

I am trying to generate email but getting this error:
fopen(/tmp/4701021fcbc23c3a52dde64ccca28857/body): failed to open
stream: Permission denied
Relevant user/group have the permissions but its not working. I used lot of links but none is working.
We are running apache server on linux Ubuntu 14.04.4 LTS and using laravel framework.
Try running this commands in the console, is very important to execute them from your project root directory, if you don't it won't find the specific laravel commands:
cd your_project_path
php artisan cache:clear
chmod -R 777 app
chmod -R 777 storage
composer dump-autoload
I found a similar problem in the laravel.io/forum with further information.
Set up write permissions on storage directory inside your Laravel project directory:
chmod -R 777 storage
You need to access your Laravel project folder so you can run the following command
sudo chmod 777 -R 'your path to project'
Of course you have to replace 'your path to project' with the address of your project folder
for example /var/www/html/LaravelNiceProject
thats it
You can try to write command below to grant your apache user needed privileges:
chown www-data -R storage
Regards dear

Install Laravel on ubuntu

I am new to laravel. I am following this tutorial to install it on Ubuntu. I have successfully managed to install all the PHP dependencies and Laravel is cloned successfully but when I do chmod -R 777 /var/www/laravel/app/storage, I am getting No such file or Directory.
I checked it manually and it is not there, I added the directory manually but when I run artisan comand to create controller, I am seeing this error
/var/www/html/laravel/storage/logs/laravel.log
You might need to update composer before you can fire up artisan.
Please try with following command:
composer.phar update
Hope this helps.
Which version of laravel are you using? Later versions of Laravel don't have the storage folder inside "app" but in the project root.
Try with:
chmod -R 777 /var/www/laravel/storage
However, I would recommend that you use 755 instead of 777. Using 777 is almost always bad practice.
In cases where you need more permissions, you could try changing the user owner / group owner of the folder, with the following code:
chown -R someuser:somegroup laravel/somefolder

file_put_contents(meta/services.json): failed to open stream: Permission denied

I am new to Laravel. I was trying to open http://localhost/test/public/ and I got
Error in exception handler.
I googled around and changed the permission of storage directory using chmod -R 777 app/storage but to no avail.
I changed debug=>true in app.php and visited the page and got Error in exception handler:
The stream or file "/var/www/html/test/app/storage/logs/laravel.log"
could not be opened: failed to open stream: Permission denied in
/var/www/html/test/bootstrap/compiled.php:8423
Then I changed the permissions of storage directory using the command chmod -R 644 app/storage and the 'Error in exception handler' error was gone and a page is loaded. But in there I am getting this:
file_put_contents(/var/www/html/laravel/app/storage/meta/services.json):
failed to open stream: Permission denied
Suggestion from vsmoraes worked for me:
Laravel >= 5.4
php artisan cache:clear
chmod -R 775 storage/
composer dump-autoload
Laravel < 5.4
php artisan cache:clear
chmod -R 775 app/storage
composer dump-autoload
For those facing this problem with Laravel 5, this is a permission issue caused by different users trying to write at the same log file within the storage/logs folder with different permissions.
What happens is your Laravel config probably is setup to log errors daily and therefore your web server (Apache/nginx) might create this file under a default user depending on your environment it can be something like _www on OSX or www-data on *NIX systems, then the issue comes when you might have run some artisan commands and got some errors, so the artisan will write this file but with a different user because PHP on terminal is executed by a different user actually your login user, you can check it out by running this command:
php -i | grep USER
If your login user created that log file your web server you will not be able to write errors in it and vice-versa because Laravel writes log files with 655 permissions by default which only allows the owner to write in it.
To fix this temporary you have to manually give permissions for the group 664 to this file so both your login user and web server user can write to that log file.
To avoid this issue permanently you may want to setup a proper permissions when a new file is create within the storage/logs directory by inheriting the permissions from the directory this answer https://unix.stackexchange.com/a/115632 can help you to tackle with that.
You should not give 777 permissions. It's a security risk.
To Ubuntu users, in Laravel 5, I sugest to change owner for directory storage recursively:
Try the follow:
sudo chown -R www-data:www-data storage
In Ubuntu based systems, www-data is apache user.
For everyone using Laravel 5, Homestead and Mac try this:
mkdir storage/framework/views
some times SELINUX caused this problem;
you can disable selinux with this command.
sudo setenforce 0
NEVER GIVE IT PERMISSION 777!
go to the directory of the laravel project on your terminal and write:
sudo chown -R your-user:www-data /path/to/your/laravel/project/
sudo find /same/path/ -type f -exec chmod 664 {} \;
sudo find /same/path/ -type d -exec chmod 775 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
This way you're making your user the owner and giving privileges:
1 Execute, 2 Write, 4 Read
1+2+4 = 7 means (rwx)
2+4 = 6 means (rw)
finally, for the storage access, ug+rwx means you're giving the user and group a 7
Problem solved
php artisan cache:clear
sudo chmod -R 777 vendor storage
this enables the write permission to app , framework, logs Hope this will Help
For vagrant users, the solution is:
(in vagrant) php artisan cache:clear
(outside of vagrant) chmod -R 777 app/storage
(in vagrant) composer dump-autoload
Making sure you chmod in your local environment and not inside vagrant is important here!
Try again with chmod -R 755 /var/www/html/test/app/storage. Use with sudo for Operation not permitted in chmod. Use Check owner permission if still having the error.
As per Laravel 5.4 which is the latest as I am writing this, if you have any problem like this, you ned to change the permission.
DO NOT LISTEN TO ANYONE WHO TELLS YOU TO SET 777 FOR ANY DIRECTORY.
It has a security issue.
Change the permission of storage folder like this
sudo chmod -R 775 storage
Change bootstrap folder permission like this
sudo chmod -R 775 bootstrap/cache
Now please make sure that you're executing both commands from your application directory. You won't face problems in future regarding permission. 775 doesn't compromise any security of your machine.
Suggest the correct permission, if for Apache,
sudo chown -R apache:apache apppath/app/storage
FOR ANYONE RUNNING AN OS WITH SELINUX: The correct way of allowing httpd to write to the laravel storage folder is:
sudo semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/www/storage(/.*)?'
Then to apply the changes immediately:
sudo restorecon -F -r '/path/to/www/storage'
SELinux can be a pain to deal with, but if it's present then I'd STRONGLY ADVISE you learn it rather than bypassing it entirely.
If you have Laravel 5 and looking permanent solution , applicable both php artisan command line usage and Apache server use this:
sudo chmod -R 777 vendor storage
echo "umask 000" | sudo tee -a /etc/resolv.conf
sudo service apache2 restart
See detailed explanation here.
I had the same issue and the below steps helped me fix the issue.
Find out the apache user - created a test.php file in the public folder with the code
<?php echo exec('whoami'); ?>
And run the file from the web browser. It would give the apache user. In my case, it is ec2-user as I was using the aws with cronjob installed in /etc/cron.d/. It could be different user for others.
Run the below command on the command line.
sudo chown -R ec2-user:<usergroup> /app-path/public
You need to identify and use the right "user" and "usergroup" here.
I had the same problem but in the views directory:
file_put_contents(/var/www/app/storage/framework/views/237ecf97ac8c3cea6973b0b09f1ad97256b9079c.php): failed to open stream: Permission denied
And I solved it cleaning the views cache directory with the following artisan command:
php artisan view:clear
Xampp for use:
cd /Applications/XAMPP/htdocs
chmod -R 775 test/app/storage
From Setting Up Laravel 4.x on Mac OSX 10.8+ with XAMPP
Any time I change app.php I get a permission denied writing bootstrap/cache/services.json so I did this to fix it:
chmod -R 777 bootstrap/cache/
rm storage/logs/laravel.log
solved this for me
Setting permission to 777 is definitely terrible idea!
... but
If you are getting permission error connected with "storage" folder that's what worked for me:
1) Set "storage" and its subfolders permission to 777 with
sudo chmod -R 777 storage/
2) In browser go to laravel home page laravel/public/ (laravel will create necessary initial storage files)
3) Return safe 775 permission to storage and its subfolders
sudo chmod -R 775 storage/
If using laradock, try chown -R laradock:www-data ./storage in your workspace container
In my case solution was to change permission to app/storage/framework/views and app/storage/logs directories.
After a lot of trial and error with directory permissions I ended up with an epiphany...there was no space left on the disk's partition. Just wanted to share to make sure nobody else is stupid enough to keep looking for the solution in the wrong direction.
In Linux you can use df -h to check your disk size and free space.
This issue actually caused by different users who wants to write/read file but denied cause different ownership. maybe you as 'root' installed laravel before then you login into your site as 'laravel' user where 'laravel' the default ownership, so this is the actually real issue here. So when user 'laravel' want to read/write all file in disk as default, to be denied, cause that file has ownership by 'root'.
To solving this problem you can follow like this:
sudo chown -hR your-user-name /root /nameforlder
or in my case
sudo chown -hR igmcoid /root /sublaravel
Footnote:
root as name first ownership who installed before
your-user-name as the default ownership who actually write/read in site.
namefolder as name folder that want you change the ownership.
If you use Linux or Mac, even you can also run in ssh terminal. You can use terminal for run this command,
php artisan cache:clear
sudo chmod -R 777 storage
composer dump-autoload
If you are using windows, you can run using git bash.
php artisan cache:clear
chmod -R 777 storage
composer dump-autoload
You can download git form https://git-scm.com/downloads.
If anyone else runs into a similar issue with fopen file permissions error, but is wise enough not to blindly chmod 777 here is my suggestion.
Check the command you are using for permissions that apache needs:
fopen('filepath/filename.pdf', 'r');
The 'r' means open for read only, and if you aren't editing the file, this is what you should have it set as. This means apache/www-data needs at least read permission on that file, which if the file is created through laravel it will have read permission already.
If for any reason you have to write to the file:
fopen('filepath/filename.pdf', 'r+');
Then make sure apache also has permissions to write to the file.
http://php.net/manual/en/function.fopen.php
Just start your server using artisian
php artisian serve
Then access your project from the specified URL:
I have the same issue when running vagrant on mac. solved the problem by changing the user of Apache server in https.conf file:
# check user for php
[vagrant] ubuntu ~ $ php -i | grep USER
USER => ubuntu
$_SERVER['USER'] => ubuntu
[vagrant] ubuntu ~ $
Run apache under php user instead of user daemon to resolve file access issue with php
# change default apache user from daemon to php user
sudo sed -i 's/User daemon/User ubuntu/g' /opt/lampp/etc/httpd.conf
sudo sed -i 's/Group daemon/Group ubuntu/g' /opt/lampp/etc/httpd.conf
now, php created cache file can be read and edit by apache without showing any access permission error.
I got same errors in my project...
But found out that I forgot to put enctype in my form.
<form method="#" action="#" enctype="multipart/form-data">
Hopes it helps somewhere somehow...
While working on Windows 10 with Laragon and Laravel 4, it seemed to me there was no way to change the permissions manually, since executing chmod-commands in the Laragon-in-built-terminal had no effect.
However, it was possible in this terminal to go to the storage folder and manually add the desired folders like this:
cd app/storage
mkdir cache
mkdir meta
mkdir views
mkdir sessions
The cd-command in the terminal brings you to the folder (you might need to adjust this path to suit your file structure).
The mkdir-command will create the directory with the given name.
I did not have the opportunity to test this approach in Laravel 5, but I expect that a similar approach should work.
Of course there might be a better way, but at least this was a reasonable workaround for my situation (fixing the error: file_put_contents(/var/www/html/laravel/app/storage/meta/services.json): failed to open stream).
First, delete the storage folder then again create the storage folder.
Inside storage folder create a new folder name as framework.
Inside framework folder create three folders name as cache, sessions and views.
I have solved my problem by doing this.

Categories