How to run a simple 'Hello World' PHP script on Heroku? - php

I want to run a simple 'Hello World' PHP script to start understanding how Heroku functions in general. So I have created a git repository at Github with a simple 'Hello World' php script and an empty README file. Therefore the source code in the php file is:
<?php
echo 'Hello World';
?>
I applied step-by-step the instructions at Heroku:
I downloaded the Heroku CLI
entered heroku login and my credentials at the command line
had php, composer and git installed
entered git clone *my_git_repository_adress*
entered cd *my_git_repository_name*
entered heroku create
However, when I enter git push heroku master at the command line I get the following error: App not compatible with buildpack: https://github.com/heroku/heroku-buildpack-php.git.
Then when I try to set the buildpack by entering heroku buildpacks:set heroku/php, I get the following error:
▸ Missing required flag:
▸ -a, --app APP app to run command against
▸ See more help with --help
What am I doing wrong?

You need to tell to Heroku that it's a PHP project. So run into your folder:
$ echo '{}' > composer.json
$ git add composer.json
$ git commit -m "add composer.json for PHP app detection"
or you can set your buildpacks manually:
$ heroku buildpacks:set heroku/php
And push again.
Hope I help!

Heroku needs index.php to detect the buildpack. It seems you are just using single Hello World.php.
Try renaming the file or create an index.php and add redirect to your desired page.

Related

Heroku bash cannot find php command

I wanted to publish Laravel App on Heroku today but Heroku bash couldn't find php command.
I update database details in config/database.php and next want to run migration but when i go to heroku run bash and php artisan migrate --app myappname I only see bash: php: command not found
I'm on Windows10.
Problem solved. In composer.json i had ~7.1.3 version of PHP, after change to "php": "~7.3.0" and git push heroku master it start work

Heroku PHP and Node.js in the same app

I'm using a Node package called exec-php to execute PHP functions within Node.js app.
I would like to push that app to Heroku, but I'm wondering how to make that, and how that would be in the PHP bin locally.
I'm using /Applications/MAMP/bin/php/php7.0.0/bin/php. What would be the path to PHP bin in Heroku?
I tested vendor/bin/heroku-php-apache2 but it doesn't work, so what would be the path to PHP binary in Heroku?
You'll need to inform the build API that you'd like to use multiple buildpacks. Try:
$ heroku buildpacks:clear
$ heroku buildpacks:add heroku/php
$ heroku buildpacks:add heroku/nodejs
Then you can inspect the compiled slug via heroku run bash to verify that the bins are where you expect them to be.
The bin directory in heroku is : .heroku/php/bin/php

Yii2 App not displaying on Heroku domain

I installed a Yii2 "basic" app on a Cloud9 workspace using composer. It runs fine as you can see here https://portfolio-php--dfmmalaw.c9.io/basic/web/index.php
Problem is I am getting below error when trying to view after deploying to Heroku URL.
"Forbidden You don't have permission to access / on this server."
The below screenshot is my Cloud9 workspace
http://prntscr.com/8rrkn0
I even tried adding basic/web/index.php to the Heroku URL but I just get a blank page. What am I doing wrong?
After getting help from Herokai, David Zuelke, I decided to create a how-to on the subject of "Creating Yii2 App in Cloud9 → Pushing to Github → Deploying to Heroku". Please see below (google doc link at top if easier to read):
https://docs.google.com/document/d/15teHaGWUWSNW_VwdV3-7bVpQHNRv2G0Z8GPFbIB-ogs/edit
1) Create Yii2 “basic” app:
290795 $ composer create-project --prefer-dist yiisoft/yii2-app-basic basic
2) Move into “basic” directory
290795 $ cd basic/
3) Initialize local repo for “basic” directory
basic $ git init
4) Add and commit Yii project
basic $ git add .
basic $ git commit -m "basic Yii project"
5) Add procfile that will point to index.php (entry script) in “web” directory
basic $ echo "web: vendor/bin/heroku-php-apache2 web/" > Procfile
basic $ git add Procfile
basic $ git commit -m "Procfile for Heroku"
6) Comment out debugging stuff (I do this manually instead of from command line)
basic $ vim web/index.php # remove dev/debug env stuff (not my way)
basic $ git add web/index.php
basic $ git commit -m "remove dev/debug env"
7) This updates the dependency from v1.0.3 to v1.1 (absolute necessity)
basic $ composer require fxp/composer-asset-plugin
basic $ git add composer.json composer.lock
basic $ git commit -m "use fxp/composer-asset-plugin in project"
8) Create Heroku app and push to it. (I instead create and push to Github repo)
basic $ heroku create
basic $ git push heroku master (again I push to Github Repo synced with Heroku)
9) Add logging to Heroku for Yii app (no logging for Yii out of the box)
basic $ echo "web: vendor/bin/heroku-php-apache2 -l runtime/logs/app.log web/" > Procfile
basic $ git add Procfile
basic $ git commit -m "tail runtime/logs/app.log"
basic $ git push heroku master (again I push to Github Repo synced with Heroku)
10) This allows the “Contact” page to work (not sure why)
basic $ composer require ext-gd:* --ignore-platform-reqs
basic $ git add composer.json composer.lock
basic $ git commit -m "require GD for contact CAPTCHA"
basic $ git push heroku master (again I push to Github Repo synced with Heroku)
The easiest way is to check your apache logs. I do not have experience with Heroku but I am sure they do offer access to them. Just take a look in there and you will see what the problem is. A white page usually means that is was a catastrophic error.
Another thing to look at is if you have used short php tags. You should not and that usually is a setting more relaxed on your computer but more strict on hosting servers. But again look at the logs, they will tell you this too.
This is for the advanced template so not for you:
I guess you did not run 'php init' like the documentation tells you. The init command is not just for the first time you get the application, but for every time you set it up on a new server too. it creates index.php and other config files that you will need for sure. Just follow again the yii2 install instructions
http://www.yiiframework.com/doc-2.0/guide-start-installation.html

How can I composer update on OpenShift?

I am trying to use Slim on OpenShift with a free node. I can run composer update from the SSH sessions without any problem.
The only problem is every time I want to commit files through git I have to go to the console and run composer install again. My question is there is any easy way to workaround this? I tried a BASH script in /project/.openshift/action_hooks/post_deploy but the server is not creating the vendor folder under runtime/repo
I always do it via action hooks:
Inside my project directory I have a script called by /project/.openshift/action_hooks/post_deploy where post_deploy is a bash script.
Here goes what I have been using:
#!/bin/bash
export MY_PHPCOMPOSER=$OPENSHIFT_DATA_DIR/composer.phar
# if composer not exists, download
if [ ! -f $MY_PHPCOMPOSER ]; then
cd $OPENSHIFT_DATA_DIR
echo "Downloading composer..."
php -r "readfile('https://getcomposer.org/installer');" | php
fi
$MY_PHPCOMPOSER -n -q self-update
cd $OPENSHIFT_REPO_DIR
# install
php -dmemory_limit=1G $MY_PHPCOMPOSER install
So post_deploy script will perform every time which you push your repo to openshit. It work like a charm!
Side note
Since not always the OpenShift composer's version is updated it's safe
to download a new composer copy and use it.
Also, don't forget adjusting permissions settings.
Helpful links
Openshift builds
Openshift Default Build Lifecycle
I know that my answer is late but according to the Openshift documentation you can enable composer install after each build by just creating a marker file:
touch .openshift/markers/use_composer

How to deploy basic php site on Openshift after pushing code?

I am trying to use Openshift for the first time to host a php site(PHP 5.4 cartridge) I am working on for a school project. I followed the directions here to push my existing repo to my gear, and can see that the code is on the gear by ssh-ing into the gear. What do I have to do now to host the website? I initially thought that I would just be able to see the index.php in my repo, but when I go to the provided url it is just a blank page. I think I may need to use the deploy action hook to cp the git repo somewhere, but not sure where. Any help would be appreciated.
With the PHP 5.4 cartridge the application root is the root of your application directory. Let me try and explain a bit further. If you create an application named "myphpapp" with the following command:
$> rhc app create myphpapp php-5.4
After the application is created, the git repository will be cloned to the directory you ran the create command. Change to that directory:
$> cd myphpapp
This is your application www root directory and is where you need to place files. For example, create a new test.php file like this:
$> echo "some php code" >> test.php
Add the file to your local git repository and then commit and push to your openshift server:
$> git add test.php
$> git commit -am "Adding a new file"
$> git push
When you run the git push command, the changes will be pushed to the remote git repository on the openshift server. Once the code is pushed, a hook on the server will see that a new file has been added to the repo and then deploy it to the www root on the openshift server. Once the deploy has finished, you can access the file by pointing to:
http://yourApp-yourDomain.rhcloud.com/test.php
Hope that helps.
--
gs
I did not read the documentation clearly enough. The root for the app is yourApp/php. So your repository has to have a php subdirectory inside of it, and that will be the app root. There is no need to copy your code from the repository to the root. As you have the correct structure, when you push your code the website will be live.

Categories