- Published on
Laravel project file permission on server
- Authors
- Name
- Jignesh Sanghani
- @jignesh19961020
File permission is most important while you making your project live. because if you give file permission 777 means anyone can read-write-execute your file. this is the thing hacker might want and add the hacky script on your file and steal your users' data whoever runs that file.
so, i directly get to the point. there's two to give your file permission in such a way that your project runs on public domain and no public user can write those files.
- Give ownership of your all files and folders to the webserver
- Make root/your-user as an owner of all files and folder and then give necessary permission to the webserver to access those files
Let's see some of the basic things about permission and ownership:
In Linux, we can set ownership of the folder by chown
command
sudo chown -R user:group yourfiles/yourfolder
here -R
is it's mean you are recursively giving ownership to the folder. so all files and folder get you to specify user ownership if you do not put -R
option then only mention folder or file get that ownership.
another command is chmod.
sudo chmod options mod filename
mod can be either 777(all permission), or other permission. you can check out more info about that from here https://help.ubuntu.com/community/FilePermissions
Let's see both methods to set file permission. start with first one, Give ownership of your all files and folders to the webserver assuming apache2 is your webserver user
sudo chown -R apache2:apache2 /path-to-your-laravel/root
now all of your files belong to web server user and also add in the group. sometime may you have an issue while uploading files via FTP. if your FTP logged in user is not admin/root user. because your file owns by the webserver user. so you have to add your user to the webserver group by below command.
sudo usermod -a -G apache2 jignesh
Now, other methods make your user as owner
sudo chown -R jignesh:apache2 /path-to-your-laravel/root Give file and folders appropriate permission
sudo find /path-to-your-laravel/root -type f -exec chmod 664 {} ;
sudo find /path-to-your-laravel/root -type d -exec chmod 775 {} ;
as we need cache directory and it's file readable and writable too, we give special permission for it to the webserver
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache