Trouble deploying Symfony3 on a new server - php

I have used Symfony a lot in the past and haven't had many problems. I have been trying to deploy it on a different server (Ubuntu 16 w/ Apache) and am having a lot of issues.
I am getting 500 error now (https://bereanboard.com/app_dev.php). What I have tried is to go to *.com/config.php and *.com/app_dev.php to see issues (I had to temporarily comment out the section that forbids access from non localhost). I have also been checking the Apache error.log and Symfony's var/logs/prod.log.
Keep in mind it works fine on local MAMP and on my previous CentOS.
First, I am for the first time encountering permission problems for var/cache and var/logs. This was pretty easy to fix with commands like chmod -R 777 var/cache. But why did this issue come up only on this new server? Anyway, config.php on the new server is now telling me "All checks passed".
Second, in var/logs/prod.log I am getting all sorts of 'Can't find class, did you forget a "use" statement?' errors. Some of these are for a twig extension AppBundle/Twig/AppExtension.php.
Here are some Critical erros:
request.CRITICAL: Uncaught PHP Exception InvalidArgumentException:
"Class "AppBundle\Controller\HomeController" does not exist." at
/home/travis/var/cache/prod/classes.php line 4872
{"exception":"[object] (InvalidArgumentException(code: 0): Class
\"AppBundle\Controller\HomeController\" does not exist. at
/home/travis/var/cache/prod/classes.php:4872)"} []
and
php.CRITICAL: Class 'AppBundle\Twig\AppExtension' not found
{"exception":"[object]
(Symfony\Component\Debug\Exception\FatalThrowableError(code: 0):
Class 'AppBundle\Twig\AppExtension' not found at
/home/travis/var/cache/prod/appProdProjectContainer.php:2881)"} []
[2017-01-19 06:35:03] request.CRITICAL: Uncaught PHP Exception
Symfony\Component\Debug\Exception\ClassNotFoundException: "Attempted
to load class "AppExtension" from namespace "AppBundle\Twig". Did you
forget a "use" statement for another namespace?" at
/home/travis/var/cache/prod/appProdProjectContainer.php line 2881
{"exception":"[object]
(Symfony\Component\Debug\Exception\ClassNotFoundException(code:
0): Attempted to load class \"AppExtension\" from namespace
\"AppBundle\Twig\".\nDid you forget a \"use\" statement for another
namespace? at
/home/travis/var/cache/prod/appProdProjectContainer.php:2881)"} []
[2017-01-19 06:35:03] request.CRITICAL: Exception thrown when handling
an exception
(Symfony\Component\Cache\Exception\InvalidArgumentException: Cache
directory is not writable
(/home/travis/var/cache/prod/pools/CtBqbs+VrF) at
/home/travis/var/cache/prod/classes.php line 1194)
{"exception":"[object]
(Symfony\Component\Cache\Exception\InvalidArgumentException(code:
0): Cache directory is not writable
(/home/travis/var/cache/prod/pools/CtBqbs+VrF) at
/home/travis/var/cache/prod/classes.php:1194)"} []
Also, it's worth mentioning that that a fresh, empty project made from the command symfony new project does work.
*EDIT
I have consulted these Symfony docs. I have tried every way of clearing cache that I could find.
*EDIT
Here is services.yml
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
arguments: ["#doctrine.orm.entity_manager", "#security.token_storage"]
public: false
tags:
- { name: twig.extension }

It seems that there is an access problem for your web server.
Check user which launching apache on Centos by
# egrep -i '^user|^group' /etc/httpd/conf/httpd.conf
It should return apache
Then give the good privilege to /home/travis directory:
Option 1: You could have a look on Stack Echange / AskUbuntu and adapt it with apache user instead of www-data
Option 2: With first option, if you've created a folder, Apache cannot write to it. This can be "fixed" by setting umask 0007 and adding yourself (travis) to the Apache group, so that newly created files and folders are writeable by the group.
Option 2 is better if ou have enough privileges to change mask.

Try to clear cache :
php bin/console cache:clear --env=prod
and set files permissions :
chmod -R 777 var/cache
chmod -R 777 var/logs
chmod -R 777 var/sessions
hope this help you.

I had faced the same problem before once. You will have to complete remove the vendor folder
rm -R vendor/
and freshly install all the dependencies again.
php composer.phar install
This will resolve the issue.

Related

Deploying using ansible resets symfony cache file permissions on production environment

I'm using Symfony 5.0.7
My live deploy ansible after-symlink-shared.yaml file:
---
- name: Set up infrastructure-related parameters
template:
src: '{{ playbook_dir }}/templates/.env_live.dist'
dest: '{{ ansistrano_release_path.stdout }}/.env'
- name: Install Composer dependencies
composer:
command: install
arguments: --classmap-authoritative
no_dev: no
optimize_autoloader: yes
working_dir: '{{ ansistrano_release_path.stdout }}'
- name: Clear the cache
command: 'php {{ release_console_path }} cache:clear --no-warmup --env=prod'
- name: Warm up the cache
command: 'php {{ release_console_path }} cache:warmup --env=prod'
- name: Create DB if not exists
command: 'php {{ release_console_path }} doctrine:database:create --if-not-exists --env=prod'
register: create_db_output
changed_when: create_db_output.stdout is not search('already exists. Skipped')
- name: Run migrations
command: 'php {{ release_console_path }} doctrine:migrations:migrate --no-interaction --env=prod'
register: run_migrations_output
changed_when: run_migrations_output.stdout is not search('No migrations to execute')
- name: Install bundle assets
command: 'php {{ release_console_path }} assets:install --symlink --env=prod {{ ansistrano_release_path.stdout }}/public'
- name: Copy build directory
command: 'cp -a {{ ansistrano_release_path.stdout }}/public/build /var/www/project/public'
tags:
- deploy
The deployment works perfectly, however every time I deploy to the server, my production environment hits a 500 error.
I take a look at my prod.log file to understand what is causing the error and I get the following:
[2020-05-09 21:40:59] request.CRITICAL: Uncaught PHP Exception RuntimeException: "Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler)." at /var/www/project/symfony/releases/20200509213543Z/vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php line 43 {"exception":"[object] (RuntimeException(code: 0): Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler). at /var/www/project/symfony/releases/20200509213543Z/vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php:43)"} []
[2020-05-09 21:40:59] php.CRITICAL: Uncaught Exception: Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler). {"exception":"[object] (RuntimeException(code: 0): Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler). at /var/www/project/symfony/releases/20200509213543Z/vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php:43)"} []
[2020-05-09 21:40:59] request.CRITICAL: Uncaught PHP Exception RuntimeException: "Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler)." at /var/www/project/symfony/releases/20200509213543Z/vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php line 43 {"exception":"[object] (RuntimeException(code: 0): Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler). at /var/www/project/symfony/releases/20200509213543Z/vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php:43)"} []
[2020-05-09 21:40:59] php.CRITICAL: Uncaught Exception: Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler). {"exception":"[object] (RuntimeException(code: 0): Unable to create the storage directory (/var/www/project/symfony/releases/20200509213543Z/var/cache/prod/profiler). at /var/www/project/symfony/releases/20200509213543Z/vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php:43)"}
This looks like a permissions issue because every time I deploy, this error comes up. Is is possible I can do something from the NGINX perspective to ensure this functions properly? Or is this an ansible solution? I'm trying to avoid manually fixing these errors with each deployment.
The user running the deployment seems to be different than the one that runs php, that's why the user doesn't have permissions to the cache directory after cache:warmup is run.
You have several options:
Change the user the app runs under:
Since you say you are using nginx I guess you are running php-fpm. You can change the user and group parameters in your php-fpm.conf to the one running the deployment. If you are running multiple applications, create a new pool. (Here's slightly outdated guide to fpm and pools to get you going, and the FPM reference).
Adjust primary groups and umask or acls so both users have access.
Run the cache tasks as the php user using become.
Fix permissions as part of the deploy process by running an ansible task. You can use the file module for this. You can either use open permissions or use become.
Keep in mind that using become (the last two options) requires changing the sudoers file so the deploy user can act as the php user.
If you have an uploads directory under public that needs write access by the php process, you need to fix those permissions too.
I figured out the issue.
It was purely a permissions issue that I had to firstly go to the highest level folder of where the var prod folder was and updated the permissions using chown then updated the permissions for all folder below recursively using chmod to ensure everything else moving forward would be written just as the parent was.
Issue happens probably because ansible runs on root user, but PHP server didn't
Try to add the last step for change ownership of directory in the playbook
- name: Fix user rights
command: 'chown -R www-data: /var/www/project/public'
tags:
- deploy
in my example ownership grants to user www-data (default), but in your case it can be different.
For check right user, use command
cd /var/www/project && ls -la

Twig Cache Error

I'm getting a Twig error ...
Uncaught exception 'RuntimeException' with message 'Unable to write in the cache directory (./cache/twig/69).
but only ever in that 1 hexadecimal directory listed above. If I then set the permissions on that folder to 0777 and also set the permissions on the files it contains, it works correctly but only for a few days. I then have to do the same fix and so on and so on. None of the other directories in the twig folder are affected. Any ideas?
Check the permission of the folder. It should have write access.
Check SELinux policy
sestatus
To disable SELinux
sudo setenforce 0
Are you running commands from the CLI with a different user than the webserver? This often occurs if you run console commands as e.g. root and they create cache directories. Try clearing the cache completely after each CLI call.

Symfony delete a bundle manually

I tried to delete a bundle manually, but an error appeared...
I guess I forgot an essential edition, but I don't know what.
I deleted the file of that bundle, I delete its line in the AppKernel, and I deleted it's lines in the app/routing.yml.
Here is the error :
FileLoaderLoadException in FileLoader.php line 118:
Bundle "DbToYmlBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your AppKernel.php file? in #DbToYmlBundle/Resources/config/services.yml (which is being imported from "/opt/lampp/htdocs/dublin/app/config/config.yml"). Make sure the "DbToYmlBundle" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "#DbToYmlBundle/Resources/config/services.yml" is not empty.
Could someone help to find what is wrong ?
The error message states that the #DbToYmlBundle/Resources/config/services.yml is imported from /opt/lampp/htdocs/dublin/app/config/config.yml
Please check your config.yml's or provide some more information/codes, maybe the output for your prod.log /opt/lampp/htdocs/dublin/app/logs/prod.log
You can try to clear the cache with the console.
For the Dev environment:
php bin/console cache:clear
And for production environment :
php bin/console cache:clear --env=prod

How to deploy Laravel 5.1 application to CentOS 6.4 VPS (or any!) server (PHP 5.5.32)

I have a Laravel 5.1 application that I'm trying to deploy on my CentOS 6.4 VPS server. The problem is that I am getting Error 500 when trying to load the page. When I check the error log it shows me this:
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/home/pams/pams_todo_list/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied' in /home/pams/pams_todo_list/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:87
I have changed the permissions in pams_todo_list/storage and pams_todo_list/vendors to 777 instead of 755 as a test, but no love.
This problem has been solved, thanks to FFForever and lagbox on the Laravel IRC channel.
My owner permissions were wrong. I had inadvertently uploaded the project using the root account. The fix was two fold. First, I setup suPHP as my php handler (I do have root access to this server) through the EasyApache3 UI in WHM. Then I set the owner:
chown -R pams:pams pams_todo_list
Now it works. I cannot say if it would have worked without setting up suPHP, however it was strongly recommended to set that up, so I did.

phabricator on redhat's openshift

I installed phabricator on openshift using a quickstart from github
https://github.com/CodeBlock/phabricator-openshift-quickstart
I got it running up fine, but I now have two issues:
1.) A setup issue that says apc.stat is enabled and that must be disabled in
/var/lib/openshift/my-user-hash/php/configuration/etc/php.ini
however I cannot access that with sudo command(sudo permission denied), even if I open it normally, I didn't find any apc.stat settings in the php.ini
2.) I can't figure out how set the local path for tracking repositories for diffusion. It says
I must give a path which should be read-writable by phabricator, I tried to give the persistent storage location ..data/ , but it gives me an exception as follows :
Unhandled Exception ("CommandException")
Command failed with error #1!
COMMAND
(cd '../data/' && HOME='/var/lib/openshift/my-user-hash/app-root/runtime/repo/phabricator/support/empty/' git cat-file --batch)
STDOUT
(empty)
STDERR
sh: line 0: cd: ../data/: No such file or directory
how do I fix this?
1) According to Num Duong answer, it seems like u currently could not resolve this issue, probably u should wait for openshift php.ini permissions policy changes.
Anyway this is minor non blocking issue.
2) Modify config file and re-deploy to openshift.
Look for available phabricator options here
U need smth like this: 'repository.default-local-path' => getenv('OPENSHIFT_DATA_DIR'),
P.S. Anyway there is one particular issue with phabricator on openshift that possibly never got resolved: cloning git repositories from OpenShift instances into Phabricator do not
work by SSH, due to permissions regarding the ssh configuration.

Categories