Laravel 4: Running composer when deploying to Heroku - php

I've been searching everywhere for an answer to this.
I built an app in Laravel - I chose Heroku for deployment, I included a custom Procfile and httpd.conf to rewrite the document root to public. That all works great.
However my app shows an internal 500 error upon loading because it hasn't loaded any dependencies i.e. not run composer upon deployment.
My question is: how do I make composer run upon deployment?
p.s. I'm a total noob with this kind of stuff so please provide as much detail as possible.
The only possible solution I've seen is here: http://bergie.iki.fi/blog/using_composer_to_manage_dependencies_in_heroku_php_apps/
But I'm convinced there is better way than this.

Try the following
Composer Fix
You need to generate and include composer.lock in the repo, but Laravel's default .gitignore ignores composer.lock
Remove composer.lock from .gitignore
$ composer install
Add your Procfile
Heroku knows which processes to run for your app based on a configuration file called a Procfile. The default apache2 process (if you don't use a Procfile) points to the web root, not to /public, so we need to create a custom Procfile to serve the site from /public.
Add a file with the name Procfile that contains this line:
web: vendor/bin/heroku-php-apache2 public
Deploy your code to the Heroku app
$ git push heroku master

Another possible cause is that Heroku doesn't recognize your app as a PHP app, for example because of the presence of package.json in the root. And so it doesn't do composer install for you.
Solution is to specify a buildpack, and do a push again.
heroku config:add BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-php
See also:
Heroku doesn't recognize my Laravel app as PHP app, and does not do composer install

Related

My Symfony application crashes on production because it can't find WebProfilerBundle, how can I get it working?

I have an application developed with Symfony 5, which is working fine in my local environment (Vagrant running Laravel Homestead box), but when I deploy it to the live server it crashes with an empty white page and responds with 500 Internal Server Error.
When I SSH into the server and manually change the APP_ENV variable's value to dev from prod inside my .env file, I can see the error:
Attempted to load class "WebProfilerBundle" from namespace "Symfony\Bundle\WebProfilerBundle".
Did you forget a "use" statement for another namespace?
I'm using Github Actions to deploy my application and in my action I'm installing the PHP dependencies like this
- name: Install dependencies
run: |
export APP_ENV=prod
composer install --no-dev --optimize-autoloader
So I don't want development only dependencies in my production server, hence the --no-dev flag, and if I understand correctly the WebProfilerBundle is used for inspecting the request in development, so it's indented to be used only in the dev environment.
Why does Symfony think it has to load this bundle?
Also, I supposed to have my custom error pages configured for 404 and 500 errors (adding a bundles/TwigBundle/Exception/error.html.twig file under the templates folder), why isn't my custom error page shown? Why am I getting an empty white page?
I'm suspecting it's something with the way I'm deploying (since it work on my local machine).
I've tired removing the flags from the composer install (so just running composer install as is), but that also resulted in an error.
You can view my deployment action here.
Also here's the link to the repository.
The app's supposed to run on this domain: https://thebedechkacase.com/
Check your config/bundles.php and be sure to set Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true]. If i understood correctly your app is trying to load class which is not present under composer. Also be sure to clear cache on prod (php bin/console cache:clear). Also be sure that your production server have no .env.local file (or it's APP_ENV should also be prod)
Both the comment of #floGalen and #Gordy's answer were pointing me in the right direction, so thanks for that.
But the exact anwser came from reading Symfony's guide on deployment.
If you get a “class not found” error during this step, you may need to
run export APP_ENV=prod (or export SYMFONY_ENV=prod if you’re not
using Symfony Flex) before running this command so that the
post-install-cmd scripts run in the prod environment.
Since I wasn't using Symfony Flex I also needed the SYMFONY_ENV variable set to prod.
In addition I had my .env in the repo - which was actually only the one with development data and APP_ENV=dev, the "real" .env file was created during the deployment processes on the runner, retrieving it's value from Github's secret's store. Now the additional problem was that I first checked out my code to the runner, then run composer install then created the production .env (overwriting the one checked out from the repo), so composer install actually run with the dev settings. To fix this I moved the creation of my env file right before running composer install --no-dev --optimize-autoloader, then also did export SYMFONY_ENV=prod.
The combination of these two solved my issue!

How to use Git With Laravel?

I'm pretty new to Git. I'm developing using PHP/Laravel on at least two machines; both Windows 10, let's call them office and home. I want to keep a sync environment on both. So I created an account on BitBucket.
I created my Laravel app using Laragon using the directory: d:\laragon\www\project
I created a clean remote repo in BitBucket and configured for use on the office PC, inside the project directory:
git init
git remote add origin https://...
git add .
git commit master
git push -u origin master
It copies some of the files to the remote repository. I understand this is because of the .gitignore file, and that's okay.
Now the thing is if I go to my home PC and do a:
git clone http://...
It will only get the non-ignored files. My question is, how do I have to configure the second development environment?
I tried to create a new app at the home's c:\laragon\www\project and then try to clone in this directory, but of course it says the directory is not empty, so does nothing.
Do I have to delete the .gitignore file the first time, so it syncs everything?
I'm assuming that you already have your second machine with the basic set up (php, composer, laravel, local server, node and so on..)
First of all you need to install your composer dependencies (listed in composer.json), to accomplish this run:
composer install
The .gitignore will only ignore.. well.. the desired ignored files, such as: node_modules and .env for example. To solve this, install your dependencies (listed in your package.json, that is not ignored by default) in your second machine using npm or yarn:
npm install
// or
yarn install
In the case of your .env file, I suggest you to clone the .env.example (creating the desired .env) and set your keys in every machine, because any of them can have a different setup.
Of course, your Laravel encryption key needs to be generated, so run:
php artisan key:generate
Finally, migrate your database (and populate it, in case you set up seeders) like this:
php artisan migrate --seed
// notice the '--seed' flag is used when you want to run your seeders

Can't run phpunit tests from command line

I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command:
./vendor/bin/phpunit /tests/ExampleTest.php
And then I get the following message:
You need to set up the project dependencies using the following commands:
wget http://getcomposer.org/composer,phar
php composer.phar install
I already have composer installed in may system and I install Laravel 5 using composer. Isn't phpunit installed when I install a new laravel 5 application? If not, how can I install it in a existent laravel 5 application?
I known that I can also install phpunit globaly and resolve the problem. But maybe it will be a duplication since I have all the phpunit code already in may laravel application.
I had the same problem and it had a specific solution that may apply to other people. I store my files in Dropbox, and the vendor/bin/phpunit file should be an symlink like this
$ ls -lo vendor/bin/phpunit
lrwxr-xr-x vendor/bin/phpunit -> ../phpunit/phpunit/phpunit
However, Dropbox will occasionally replace symlinks with the original file, and this will cause the error above. The behaviour is inconsistent, and with relative symlinks seems to break when 2 machines are accessing Dropbox at the same time. Fixing the symlink worked for me or you could make a new symlink directly to the vendor/phpunit/phpunit/phpunit outside of Dropbox and run that.
Edit: Nowadays I exclude Vendor and node_modules from Dropbox - and simply run composer install when necessary. This works really well, and also deals with the hassle of syncing so many files on Dropbox. What you can do is go into the folder and delete all the files. Wait for Dropbox to sync. Then mark the folder as excluded. Finally, run composer install and you get the contents as you need. (Delete + composer install often solves other issues too).
Running composer install did nothing in my case. However, removing vendor folder and then calling composer install fixed it.
You need to have Composer installed and run composer install or composer update in your application to install the different packages listed in your composer.json.
When you install your Laravel application it doesn't install the packages right away.
You can verify the packages are installed by looking in the vendor directory of your application and check that phpunit is in there.
did you install phpunit globally? I recommend you do it.
just type in your laravel's root directory (e.g. /var/www)
cd /var/www
phpunit
if you what to test just one file, you can do something like this:
phpunit tests/ExampleTest.php
Unit Test:
D:\xampp\htdocs\Samplemed\vendor\bin>
phpunit ../../tests/Unit/Company/CompanyUnitTest
Feature Test:
D:\xampp\htdocs\Samplemed\vendor\bin>phpunit
../../tests/Feature/Company/CompanyFeatureTest
Please try this. its working fine.

Heroku PHP app crash bash: vendor/bin/heroku-php-apache2: No such file or directory

I'm not very good with Heroku setup
I'm trying to put online an app in PHP (with Code Igniter) but it doesn't work well. Here is the error :
Heroku PHP app crash bash: vendor/bin/heroku-php-apache2: No such file or directory
index.php is in root folder. Vendor directory also in root folder
composer made his job
In procfile :
web: vendor/bin/heroku-php-apache2
And in my index.php:
require('vendor/autoload.php');
In the past I used the boot.sh way, so I'm not comfortable with the new way.
I followed this tutorial
https://devcenter.heroku.com/articles/getting-started-with-php#introduction
I think I missed something obvious. But I don't know what.
Thank you
Your composer.json likely re-defines the bin-dir setting to something other than vendor/bin. Run composer config bin-dir to see what it is (or look at your composer.json's config section, and adjust the path to heroku-php-apache2 in your Procfile accordingly.
You can also just change the Procfile to automatically read the right value:
web: $(composer config bin-dir)/heroku-php-apache2
The notes at https://devcenter.heroku.com/articles/php-support#web-servers also mention this bin-dir caveat.
My solution was to add the code below to composer.json.
"require-dev": {
"heroku/heroku-buildpack-php" : "dev-master"
}
Then run composer update.
Thanks To David, here is the answer :
You're using the legacy version of the buildpack - your app has BUILDPACK_URL set to https://github.com/heroku/heroku-buildpack-php.git#legacy.
Run heroku config:unset BUILDPACK_URL and push an empty change (git commit -m "new buildpack" --allow-empty will do).
Because I Copy/pasted old vars from an old project (> 1 year) using boot.sh
There was BUILDPACK_URL which was the bad URL. No need to put it now.
Thanks dzuelke !
Did you try to remove your procfile and add the base composer.json
In addition to following the steps exactly as described here :
https://devcenter.heroku.com/articles/getting-started-with-laravel
I had to remove .env from gitignore and also set
APP_KEY generated using
php artisan key:generate --show
to .env
in order for this to work.

heroku push rejected no cedar-supported app detected php - index.php present

I am trying to push my php app to Heroku and get the below error.
Heroku receiving push
! Heroku push rejected, no Cedar-supported app detected.
I have read through all the previous posts which said that the PHP app should have a index.php in the root folder...which i do. When i do a "git ls-files" i get the index.php listed (with the name in lowercase).
steps i have performed so far are.
1. i have an application created on Heroku - stack:Cedar
2. git add the relevant files
3. git commit with a comment
4. git push heroku master -- this guy gives me the error.
git ls-files lists a file "index.php".
What am i missing this time?
I'm just starting out with Heroku and was encountering the same problem. Below is a description of what I did wrong.
Procfile
Make sure that the following statement is included in your Procfile:
web: vendor/bin/heroku-php-apache2 path/to/web/
Where path/to/web/ is the relative path to the directory that you want to publicly expose on the web. Make sure that the public directory contains an index.php file (e.g. path/to/web/index.php).
composer.json
In your composer.json directory (which should be in the base directory of the repository) make sure to include the Heroku PHP buildpack.
{
"require": {
},
"require-dev": {
"heroku/heroku-buildpack-php": "*"
}
}
If this statement was missing, then run composer update (./composer.phar update for me, because I downloaded composer and placed it in the base directory of my repository) so that Composer will fetch the package and install it into your vendor directory.
the issue was with an incorrect/incomplete .git directory. Deleting the old one and recreating it (git init) solved the problem.
try to run this on you heroku toolbelt
> heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-php
> git add .
> git git commit -am "add composer.json for PHP app detection"
> git push / git push heroku master

Categories