Introduction
This article is created to serve as a guide for anyone looking to deploy their YII2 project to Heroku.
Requirements
In this article, I will be assuming the following:
Your YII2 project is connected to a MySQL database (If it is connected to any other type of database, you will need to adjust your code accordingly).
You have a basic understanding of Heroku (I won't be going in-depth into Heroku. Only the part necessary for us to deploy our app).
You have Heroku CLI installed on your system
You must have basic understand of git & GitHub (or other version control systems) system (e.g. git)
That said, let's dive right into it.
Deploying Your YII2 Project To Heroku
Add Procfile
Open command line and change directory to your project directory:
cd path-to-your-project
Use command line to add "Procfile" to your YII2 project root directory and configure it properly:
echo web: vendor/bin/heroku-php-apache2 web/ > Procfile
(Ensure there is space between "web:" and "vendor..." or else you might run into a "Forbidden Error" on Heroku
Prepare YII for production
Before deploying, it is advised to prevent YII from throwing errors in production.
Navigate to web/index.php. You should see something like this:
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/web.php';
(new yii\web\Application($config))->run();
Comment out those 2 lines, so you have something like this:
<?php
// comment out the following two lines when deployed to production
//defined('YII_DEBUG') or define('YII_DEBUG', true);
//defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/web.php';
(new yii\web\Application($config))->run();
Setup your project database
When you are deploying your YII2 project on Heroku, you are free to use whatever database you want, provided you connect it properly in your config file.
If you don't know how to create a live database, check this article I wrote on creating a live MySQL database to use on Heroku.
Navigate to config/db.php and it should look something like this:
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=your-live-database-host;dbname=your-live-database-name',
'username' => 'your-live-database-username',
'password' => 'your-live-database-password',
'charset' => 'utf8',
// Schema cache options (for production environment)
//'enableSchemaCache' => true,
//'schemaCacheDuration' => 60,
//'schemaCache' => 'cache',
];
Deploy the YII2 project to Heroku
To deploy the YII2 project to Heroku, you need to create a Heroku project on command line (If this is your first time using heroku on your system, you will be prompted to login):
heroku create
Note: If you have not logged into heroku on your CLI before, you'll be prompted to login before continuing.
Add your files to the Heroku repository
git add *
git commit -m "a-message-to-help-you-remember-this-deployment"
Push to heroku
git push heroku main
View your live project
Now you can login to your Heroku dashboard and view your live project