Articles

Wednesday, 2 December 2020

Proper file permissions for Laravel

Step by step guide

  1. ssh into your server.
  2. cd into your project directory.
cd /var/www/<project-folder>
  1. Assuming your web server is www-data, assign you Laravel project to that group:
sudo chown -R <my-username>:www-data .
  1. Add your user to the www-data group (so you should also have permission to read/write):
sudo usermod -a -G www-data <my-username>

Good practice is to set all your directories to 755, and all files to 644.

  1. Set folder permissions using the following command:
sudo find . -type d -exec chmod 755 {} \;
  1. Set file permissions using the following command:
sudo find . -type f -exec chmod 644 {} \;
  1. Give the webserver permission to upload & write files to the storage & cache folders:
sudo chmod -R ug+rwx storage bootstrap/cache

Done 🥳 .

Queue worker

If you have a queue worker running, like supervisor, you should configure the user to be www-data, not your user:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/PROJECT_NAME/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=6
redirect_stderr=true
stdout_logfile=/var/www/PROJECT_NAME/worker.log
stopwaitsecs=3600