How to run a Command Line PHP Script on Heroku App - php

I have PHP apps deployed on Heroku. How can I run a Command Line PHP Script on an app even though I don't have command line access or terminal access to the server?

( this assumes that you already have an existing PHP app deployed to Heroku )
1) Install Heroku's Command Line Tool "Heroku Toolbelt" in your local dev environment.
2) Then run this in your local terminal window:
PHP Internal Commands:
heroku run php --version
Your PHP Scripts:
sudo heroku run php path/to/file.php
( may not need to use sudo - taken from an example - I don't use sudo )
How to run the command on a specific App.
I have several apps running on Heroku and I usually add -a :
heroku run php path/to/file.php -a <app name>
A Default App can be set to avoid having to specify the app every time:
heroku git:remote -a <app name>

Related

Why despite "drush" being installed via `composer global install` during image build, I cannot find the tool from within a running PHP script?

I am developing a PHP web application inside of a Docker container. Using volumes: inside of my docker-compose.yml file, I have specified a local directory so that any files generated are dumped and persist after the container is destroyed.
volumes:
- ./docroot:/var/www/html
Inside my Dockerfile, I RUN a command that installs a command line management tool:
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer && \
ln -s /root/.composer/vendor/bin/drush /usr/local/bin/drush
RUN composer global require drush/drush:8.3.3 && \
composer global update
When the container comes up, I can use docker-compose exec -it <container> bash to get inside the container, and everything works fine. drush is in my path, and I can use it globally throughout the container to manage the app.
Now here is the strange part. Part of my application is that I have to run that command from a PHP script inside the container to help automatically manage some of the build process.
Using php, I run exec('drush dbupdate', $output, $retval); $retval returns a exit status of 127, or command not found and $output is empty. If I switch up the exec to use the full path I get an exit status 126.
If I go back into the container, I can run that command just fine. Note all other cli commands work as expected with exec (ls, whoami, etc but which drush returns exist status 1)
What am I missing? Why can I use it with no problems manually, but PHP exec() can't find it? passthru(), shell_exec(), and others have the same behavior.
composer global install will not install the command "globally" for all users, but "globally" as in "for all projects".
Generally, these packages are installed in the home directory for the user executing the command (e.g. ~/.composer), and if they are available in your path is because ~/.composer/vendor/bin is added to the session path.
But when you run composer global require (while building the image) or when you "log in" to the running container (using exec [...] bash) the user involved is root. But when your PHP script runs, it's being executed by another user (presumably www-data). And for that user, ~/.composer does not contain anything.
Maybe do not install drush using composer, but rather download the PHAR file directly or something like that while you are building the image, and put it in /usr/local/bin.
If you are using Drupal >= 8, the recommended way of installing Drush is not as a "global" dependency, but as "project" dependency, so that the appropriate drush version is installed. This comes straight from the docs:
It is recommended that Drupal 8 sites be built using Composer, with Drush listed as a dependency. That project already includes Drush in its composer.json. If your Composer project doesn't yet depend on Drush, run composer require drush/drush to add it. After this step, you may call Drush via vendor/bin/drush

PHP shell_exec git --version is different than when logged in via SSH

I have a shell script Sync.sh on a CentOS7 server that executes a git checkout and then a git pull. Each of these commands uses the -C <path> option so the script can live wherever it wants. This all works fine when logged in via SSH (PuTTY) and running the script from the command line ./Sync.sh
However, when I run this via a PHP script, it fails:
$CommandOutput = shell_exec( "./Sync.sh" );
I added a git --version to Sync.sh and found that the PHP is using git version 1.8.3, which does not include the -C <path> option (it was added in 1.8.5 if I remember right). However SSH's git --version returns 2.9.5, which explains why running Sync.sh fails via PHP but works fine when run via SSH.
So the questions are:
why is PHP getting a different git version than SSH?
how can I make PHP get the same version as SSH?

Run a database migration command when deploying a Docker container to AWS

Please bear with me. Pretty new to Docker.
I'm deploying Docker containers (detached) to an AWS EC2 registry using CodeDeploy. On deploy, the following command is run after setting some environmental variables etc:
exec docker run -d ${PORTS} -v cache-${CACHE_VOLUME} --env-file $(dirname $0)/docker.env --tty "${IMAGE}:${TAG}"
The container runs an image located and tagged in EC2 Container Service. No problems so far.
Since this is a PHP application (specifically a Symfony2 application) I would normally need to issue the following command to execute database migrations on deployment:
php app/console doctrine:migrations:migrate --no-interaction
Now, is there any to run this command during "docker run..." while keeping the container running, or do I need to run another container specifically for this command?
Many thanks!
You need create entrypoint. This script runs at the container startup.
entrypoint.sh file:
#create or update db
./waitforit.sh <DB_HOST>:<DP_PORT> -t 30
php app/console doctrine:migrations:execute
# start apache
apache2-foreground
wait for it it is a script waited when database is started up
Just leaving this here for the next one that searches for this... ;-)
When using a recent version of Doctrine, there is a pretty handy parameter for this:
php bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration
The "allow-no-migration" parameter instructs doctrine not to throw an exception, when there is nothing to do...
I do as follows:
docker-compose exec [containerID] ./app/console migrations:migrate --no-interaction

Execute PHP script on linux with PHP LAMPP service

So, I have that question. I've installed xampp on my ubuntu, and I run my web system from there ( /opt/lampp/htdocs ). I can open my php web systems from navigator with no troubles. So now I want to run a php script from linux console, like this:
php -f file.php
but ubuntu tells me to install php7.0-cli or hhvm packages to run a php script on console. The real question is, that I've installed lampp running php there. Is there any chance to run a php script on console with the lampp's php service? or I have to install those packages for ubuntu anyway?
When you run:
php -f file.php
from the linux terminal you are excecuting the system php enviroment (in ubuntu you install it using apt-get install), if you need run the script with the LAMPP php environment you must run:
/opt/lampp/bin/php -f file.php
If you run this command repeatedly, you can set alias by appending the following command in the "~/.bash-aliases" file or by directly running the command in the terminal:
alias php='/opt/lampp/bin/php'

sh: php: command not found in git bash windows 8

I'm running php composer.phar install from git bash CLI and it shows sh:php: Command not found.
Though I have set environment variable path to my wamp/bin/php/php5.4.16/
You are required to put the directory that has php.exe in your WAMP installation into environment variable PATH. It is generally like C:\wamp\bin\php.
Where \php\ is the directory containing php.exe.
Set php env. variable as mentioned here. (you can test it easily using this command:                   php -r "echo 1;" or php -v)
restart git shell
if composer.phar does not exists, run this (more info):                                                                   
php -r "readfile('https://getcomposer.org/installer');" | php
run php composer.phar install finally
Run php without git shell:
Locate C:\wamp\bin\php
Hold SHIFT and click with right mousebutton on folder phpX.Y.ZZ
Select Open command prompt window here
run any php command like php parameters
Whether you are using gitbash or cmd you should run it as an administrator. Navigate to your project then type the command "composer install" it will work. Mine worked just fine.

Categories