Deploy PHP to Heroku
Last Updated on Feb 14, 2023
Introduction
Having your code on your local machine is one thing and deploying it on a server and sharing it with the world is another thing.
Today we will deploy our PHP program on Heroku.
According to wikipedia:
Heroku is a cloud platform as a service supporting several programming languages. Heroku has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go.
Requirements
Now let’s get started
Heroku Account and Cli
I assume you have a free account on heroku. If you don’t go and create a new account. It’s free and it takes less than 5 minutes.
Then install heroku cli
Composer
If you don’t have composer it’s a good time to install it as well. Composer is a dependency manager for PHP. We are not going to use it today and I’ll talk about it in detail later.
Files
At the moment I have a index.php file and inside it I say hello world.
<?php
echo "Hello World!";
Now create a file and name it composer.json and inside it write an empty json like this
{
}
Great. Now you have everything you need.
Git
Our project needs to be a git repository. So let’s initialize it by running
git init
Then add everything and commit
git add .
git commit -m “initialized the project”
Great!
Deploy
First login to heroku by running
heroku login
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, simply click the Login button displayed on the page.
Now run
heroku create <name-of-your-application>
If you don’t specify a name, heroku will generate a random name. Their names are so random and ugly and I always forgot which application was what. So I prefer to specify the application name myself
heroku create deploy-php-to-heroku
Then all you have to do is
git push heroku main
Sometimes the name of your main branch is master in that case run
git push heroku master
If everything goes successfully it will give a url which you can visit
You can copy the url or run heroku open to visit your page.
Now you can share that url with anyone and they can visit your website
Mine is:
https://deploy-php-to-heroku.herokuapp.com/
Conclusion
Now you know about deploying a PHP code to heroku.
I recommend you to open a PHP files and echo something simple. then try to deploy it to heroku.
If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!
Key takeaways
- how to deploy PHP to heroku
- prepare a PHP website to deploy to heroku