When working with Symfony applications on Ubuntu, you'll frequently need to run console commands like:
php bin/console cache:clear
php bin/console assets:install public
The issue arises when these commands are executed as root - the generated files inherit root permissions, causing Apache (running as www-data) to fail when accessing them.
Apache typically runs under the www-data user, while CLI commands often run as your current user (or root). This creates a mismatch where:
- Cache files created by root can't be modified by www-data
- Asset files may become inaccessible to the web server
- Log files can't be written by both users
The simplest approach is to run commands directly as www-data:
sudo -u www-data php bin/console cache:clear
For Symfony 4+ applications, use:
sudo -u www-data php bin/console cache:clear --env=prod
Create a permanent alias in your ~/.bashrc
or ~/.zshrc
:
alias symfony='sudo -u www-data php bin/console'
Then you can simply run:
symfony cache:clear
For automated tasks via cron or systemd, create a service file:
[Unit]
Description=Symfony Command Runner
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/var/www/your_project
ExecStart=/usr/bin/php bin/console your:command
[Install]
WantedBy=multi-user.target
If you're using PHP-FPM, configure the pool to match CLI:
[www]
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
For directories that need write access:
sudo chown -R www-data:www-data var/
sudo chmod -R 775 var/
For cache and logs in Symfony 4+:
sudo setfacl -R -m u:www-data:rwX -m u:whoami:rwX var
sudo setfacl -dR -m u:www-data:rwX -m u:whoami:rwX var
Create a bash script to handle both clearing cache and fixing permissions:
#!/bin/bash
sudo -u www-data php bin/console cache:clear --env=prod
sudo chown -R www-data:www-data var/cache/ var/log/
sudo chmod -R 775 var/cache/ var/log/
When working with Symfony applications on Ubuntu servers, developers frequently encounter permission issues with console commands. The core problem manifests when executing commands like:
php bin/console cache:clear
php bin/console assets:install
Running these as root creates files with root:root ownership, while Apache/Nginx operates under www-data:www-data. This mismatch causes web server access issues to generated files.
Here are three robust approaches to solve this permission problem:
The most straightforward method is to execute commands directly as www-data user:
sudo -u www-data php bin/console cache:clear
sudo -u www-data php bin/console assets:install
For frequent use, create an alias in your ~/.bashrc
:
alias symfony='sudo -u www-data php bin/console'
For system-wide configuration, modify PHP CLI to always run as www-data:
sudo nano /etc/systemd/system/php-cli.service
Add this configuration:
[Service]
User=www-data
Group=www-data
ExecStart=/usr/bin/php -n -c /etc/php/%i/cli/php.ini
Then enable and start the service:
sudo systemctl enable php-cli
sudo systemctl start php-cli
For more complex scenarios, create an executable wrapper:
#!/bin/bash
sudo -u www-data /usr/bin/php "$@"
Save as /usr/local/bin/php-www
and make it executable:
sudo chmod +x /usr/local/bin/php-www
Now use it for Symfony commands:
php-www bin/console cache:clear
For existing misconfigured files, use this one-time fix:
sudo chown -R www-data:www-data var/
sudo chown -R www-data:www-data public/
Set correct directory permissions:
sudo find var/ -type d -exec chmod 775 {} \;
sudo find var/ -type f -exec chmod 664 {} \;
For production environments, create a dedicated systemd service:
[Unit]
Description=Symfony Console
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/path/to/symfony
ExecStart=/usr/bin/php bin/console your:command
Restart=on-failure
[Install]
WantedBy=multi-user.target