Heroku tutorials, definitions and useful commands
I followed the PHP and Python Heroku tutorials from their official website. While doing so, I saved some notes that might help you in installing the required software for using Heroku locally on your machine. These notes might assist you if you find yourself blocked while following one of the two tutorials because of an error message for example. Let's get started!
NOTE: the following notes make use of PHP 7.2.7, Python 3.6.4, Composer 1.6.5, and Heroku CLI 7.7.7
NOTE: the following notes make use of PHP 7.2.7, Python 3.6.4, Composer 1.6.5, and Heroku CLI 7.7.7
Contents
1. Notes for Tutorial 1: Getting Started on Heroku with PHP1.1 Prerequisites
1.2 Install Composer (Linux/Unix/macOS)
1.3 Install PHP dependencies locally
2. Notes for Tutorial 2: Getting Started on Heroku with Python
2.1 Prerequisites
2.2 Install Python dependencies locally
2.3 Postgres issue 1: No manual entry for psql [macOS]
2.4 Postgres issue 2: could not connect to Postgres database
3. Important Heroku definitions
4. Heroku commands
4.1 Important Heroku commands
4.2 Add a database
4.3 Deploy an app to Heroku
4.4 Run Django scripts
Notes for Tutorial 1: Getting Started on Heroku with PHP
Prerequisites
These are the prerequisites that you must satisfy before starting the Heroku-PHP tutorial:
- PHP: you could use the built-in PHP that comes with your OS (e.g. macOS High Sierra comes with PHP 7.1) or if you want the latest release of PHP, you could install PHP with your favorite package manager (see my tutorial on installing PHP with macOS's Homebrew)
- Composer: used for dependency management in PHP projects. See the section on how to install Composer.
- Git: the Heroku CLI (that you will install later in their tutorial) makes use of Git.
Open your terminal, and run the following commands to make sure that all the required prerequisites are installed (the commands must all return the number version of the corresponding component):
If Composer was installed globally:
If Composer was installed locally:
Now you are ready to tackle the Heroku-PHP tutorial!
Install Composer (Linux/Unix/macOS)
You can install Composer, either locally as part of your project, or globally as a system-wide executable.
To install Composer locally, do the following:
- Open a terminal, and change directory to your PHP project
- To run the installer, either:
- download the installer to your working directory and check its signature, and run the installer with
- or run this script which basically downloads the installer, checks the installer's signature, and runs the installer
- Now that Composer is installed (you will see a
composer.phar
file in your working directory which is the Composer binary), you can run Composer:
NOTE: when running the installer, add the options
Example:
--install-dir=bin
to install Composer in a specific directory and --filename=composer
to rename the Composer binary (default name is composer.phar
)
To install Composer globally, do the following:
- Install Composer locally following all of the above instructions
- Move
composer.phar
to a directory that is in your shellPATH
: - You can now run Composer from anywhere in your filesystem, and without directly using the
php
interpreter:
Install PHP dependencies locally
NOTE: in this section, we assume that Composer was installed globally, i.e. the command
composer
is used to run Composer, instead of php composer.phar
The file
composer.json
in the root directory tells Heroku that it is a PHP app, and it specifies the dependencies that need to be installed with the app.Two ways to add a PHP dependency:
- using Composer: NOTE:
- modifying
composer.json
:
Example: install the Monolog package
package_name
consists of the vendor name and the project's nameExample: install the Monolog package
To install the PHP dependencies locally, run the following command:
NOTE 1: see the
install
command for another command for installing PHP dependencies
NOTE 2: always check the files
composer.json
and composer.lock
in your git repo. However, include the directory vendor
(where the third-party dependencies are installed) in your .gitignore
fileNotes for Tutorial 2: Getting Started on Heroku with Python
Prerequisites
These are the prerequisites that you must satisfy before starting the Heroku-Python tutorial:
- Install Python 3.6
- Install Pipenv:
Open your terminal, and run the following commands to make sure that all the required prerequisites are installed (the commands must all return the number version of the corresponding component):
psql
should point to the correct psql
binary:For example, on macOS it should point to:
Now you are ready to tackle the Heroku-Python tutorial!
Install Python dependencies locally
The file Pipfile (or requirements.txt) in the root directory tells Heroku that it is a Python app, and it specifies the dependencies that need to be installed with the app.
Install the Python dependencies (defined in Pipfile) locally using
NOTE: Pipenv
(which creates a virtualenv where the dependencies will be installed):--three
refers to using Python 3 when creating virtualenvThen activate the virtualenv:
shell
spawns a shell within the virutalenvTo run a command inside the virtualenv:
To add one specific dependency to your application:
Example:
NOTE:
Pipfile
is updated with the new requests
dependency
Postgres issue 1: No manual entry for psql [macOS]
After configuring your
but unfortunately, you get the following1:$PATH
to include the Postgres command line tools (CLI tools) for macOS, if you type the following to get the official docs:
Then, the problem might be that the path saved in the file /etc/paths.d/postgresapp ends with a slash /, e.g. /Applications/Postgres.app/Contents/Versions/latest/bin/
This trailing slash / causes a problem with the automatic detection of the corresponding man pages. Remove this trailing slash / from the path, and enter again the following to get the official docs:
1.^ see this similar issue on GitHub; the solution is given at the end of the thread by tbussmann
Postgres issue 2: could not connect to Postgres database
If you try to connect remotely to a remote Postgres database (created through Heroku) with:
but get this error message:
Or you try to run commands on a remote Postgres database (created through Heroku) with:
but get this error message:Then, an external firewall in your local network might be preventing you from connecting remotely to the Heroku database. For example, you might be connected to a public Wi-Fi in a library. Thus, try to connect to another network such as your cell phone Wi-Fi hotspot or try a VPN service. I tried with the TunnelBear VPN app, and I was able to connect remotely to my Postgres database.
There can also be other possible causes to your Postgres connection issue such as your IP address excluded by the Postgres' access configuration file (
pg_hba.conf
). A thorough list of common Postgres error messages and their solutions was compiled by Frank Wiles @ revsys.com
As a last note, check that your Postgres database connection requires SSL (
sslmode=require
) by looking at the PG connection string:Important Heroku definitions
Procfile is a text file in the root directory of your application, that explicitly declares what command should be executed to start your app.
Dyno is a lightweight container that runs the command specified in the Procfile.
A one-off dyno has its own ephemeral filespace, populated with your app and its dependencies - once the command completes (e.g. bash), the dyno is removed.
Config vars are exposed as environment variables to the application (at runtime), and are defined in the .env file (located in the top-level directory of your project). For example, they can store encryption keys or external resource addresses.
Add-ons are third-party services that provide additional services for your application, such as Papertrail for log management, or data stores like Redis and MySQL.
Add-ons are third-party services that provide additional services for your application, such as Papertrail for log management, or data stores like Redis and MySQL.
Heroku commands
Important Heroku commands
To create an app:
NOTE 1: a git remote (called heroku) is also created
NOTE 2: if no app name is specified, then a random name is assigned
To view logs:
NOTE: to stop the log streaming, press Ctrl+C
Check number of dynos that are running:
Check number of dynos that are running:
Scale an app, i.e.change the number of dynos that are running:
NOTE: here we scale the app to one web dyno
To access the app in a web browser:
To access the app in a web browser:
Example: open https://myapp.herokuapp.com/foo
NOTE: if the app was already deployed with heroku create, no need to add the option -a
To run a Python app locally (e.g. start up a web server):
Example:
NOTE 1:
NOTE 2: you must first install locally the dependencies with
NOTE 3: open http://localhost:5000 with your web browser
To stop the app from running locally:
To run a command using one-off dynos, e.g. scripts and apps that are part of your application:
NOTE: if the app was already deployed with heroku create, no need to add the option -a
To run a Python app locally (e.g. start up a web server):
Example:
NOTE 1:
heroku local
examines the Procfile
to know what to runNOTE 2: you must first install locally the dependencies with
$ pipenv install
NOTE 3: open http://localhost:5000 with your web browser
To stop the app from running locally:
To run a command using one-off dynos, e.g. scripts and apps that are part of your application:
Example: start a console, i.e. a Python shell (launch a REPL process attached to your local terminal for experimenting in your app’s environment)
Example: start a PHP console, type quit
to close the PHP console
Example: start a bash shell, type exit
to exit the shell (the dyno is terminated also)
To set a config var:Example: set the cofig var TIMES
To view the config vars that are set:
Add a database
To list the add-ons (e.g. database) installed for your application:To add a Postgres database:
NOTE 1: a
NOTE 2: installation notes for adding other databasesDATABASE_URL
config var is added to the environment which you can read with $ heroku config
To know more about the database used by your application (e.g. plan, status, data size):
To access the database, append /db to the app's URL, e.g. https://wonderful-app-287.herokuapp.com/db
Example: connect to the remote Postgres database and see all the rows:
NOTE 1: Postgres needs to be installed locally for this command to work
NOTE 2: to know more about Heroku PostgreSQL
Deploy an app to Heroku
These are steps to deploy an app to Heroku:
- Create the app (if you have not done so yet):
- Add the modified files to the local git repository:
- Commit the changes to the repository:
- Deploy the app to Heroku:
- Check that everything is working by opening the website:
Run Django scripts
To run the Django scripts, we use the command:
Example: create tables:
Comments
Post a Comment