Heroku is a Platform as a Service(PaaS) that makes it easy to deploy servers to the web. Heroku takes pride in its simplicity and ease by which users can take their local servers to production. Heroku has a very intuitive dashboard making it easy for users to deploy their servers.
After reading this guide, you will learn how to:
- Deploy servers to Heroku
- Manage Environment variables when deploying to Heroku
The first step in deploying a server to Heroku is creating a server. Since creating web servers is out of the scope of this guide, you can clone this demo web-server from Github to continue along with this tutorial
Now that you've set up a server on your local machine, we will learn how to deploy it to production.
Deploying a web server to Heroku can be done in two ways, using either the CLI or the Heroku dashboard. We will be exploring both ways of deploying a server to Heroku, starting with the Heroku CLI.
The Heroku CLI makes it easy to deploy web servers to the internet right from your terminal. To be able to use the CLI, you first need to install it on your local machine.
Installing Heroku Using The File Binaries
- Download the standalone binary file compatible with your machine from the official download page.
- Extract the downloaded file
- Follow the OS specific prompts that follow to install the Heroku CLI
You can also install the Heroku CLI via NPM if you have Node installed on your computer. It is important to note that using NPM to install the Heroku CLI is not the most recommended method. This installation method relies on your system's version of Node.Js, which may be incompatible with Heroku.
Installing Heroku Using NPM
- Ensure you have Node installed on your computer
- Run the following command
npm install -g herokuto install the Heroku CLI on your local machine
After installing the Heroku CLI using either of the above methods, you can confirm the installation was successful by running this command
$ heroku --version //Expected Result heroku/7.0.0 (darwin-x64) node-v8.0.0
If the installation was successful, you should see the installed version of Heroku printed out. If the above command failed to print any result, you should validate you have git installed on your computer and try again or head over to the official Heroku help page.
Deploying A Server Using The CLI
Now that we've set up everything, we are ready to go ahead and deploy the sample server cloned above to production. We will be deploying the server in the following steps:
- Navigate to the project directory of the file to be deployed
- Create a new file titled
Procfilein the root of the project directory — This Procfile contains necessary commands that instruct Heroku on how to start the server. Since our demo server is in NodeJS, we need to add the following line of code to the newly created Procfile
web: node index.js
- Login to your Heroku account via the terminal by running
Heroku login. This command will redirect you to a web page and ask you to authenticate.
- Create a new Heroku application by running
heroku create <unique-app-name>
- Next, initialize a git repository for your project if it doesn't exist. Remember to add a
.gitignorefile to ignore the node modules directory &
- If you made any changes, commit the files to git by running the following command
$ git add .
$ git commit -m<Commit Message>
- Push the changes to the Heroku server — In this step, Heroku builds our server and deploys it to the internet.
git push heroku master
- If everything went well, you would notice that Heroku prints a log about the deployment to your terminal and the URL of our new server. You can test it out by running
Deploying A Server Using The Dashboard
In the previous section, we deployed a server in six steps using the Heroku CLI. In this section, we will discuss how to deploy a server right from the Heroku dashboard. Using this method requires that you have your server hosted on Github or any version control system of choice. Deploying a server can be done in the following steps
- Log in to your Heroku account and navigate to the dashboard.
- Once in the dashboard, click the
new appbutton fill the menu with a unique name for your new server & the region you want to deploy your new server.
- With your newly created app page, navigate the deploy tab and click the
**connect to Github**deployment method. It will prompt you to select the repository name you would like to connect
- After connecting with the Github repository, you can decide whether you want to deploy the server automatically or manually by clicking the check box in the automatic & manual deploys section of the Heroku app page.
- After selecting the deploy form, choose the branch Heroku should deploy and click the deploy branch button — This should print out the log information during your server's deployment. If the build succeeded, you would see your server's live URL, which you can visit. ## Configuring Environment Variables
In this section, we will discuss how to add environment variables for your apps deployed on Heroku. During development, you might need to keep specific values super secure. It is generally recommended you create a
.env file to keep all sensitive information that shouldn't be committed to version control. Since version control systems don't hold your .env files, Heroku will not have access to it when you deploy your application.
If you are working with Heroku's CLI, you can configure Heroku to use environment values by simply running the following command for all the key value pairs in your
heroku config:set <key=value>
The above command will set environment values!
If you choose to deploy your app using the Heroku dashboard, head over to your apps page's settings tab, click the
reveal config vars button. You will see your previously configured environment variables and be able to create new config vars.
So far, we have learned how to deploy servers to Heroku. It has been helpful to get you started on getting your application in production.
Interested to learn more? Follow Hookdeck where I'll be sharing tutorials and guide about webhooks!
Try Hookdeck For Free. Receive, monitor and manage all your webhooks from a single place. We handle the complexity so you can focus on building your product.