Deploying A Server To Heroku
Tags: deploy, tutorial, servers, hookdeck
Published: 2021-04-05 18:09:49

---
title: Deploying A Server To Heroku
published: true
description: 
tags: Deploy, tutorial,servers, hookdeck
cover_image: https://paper-attachments.dropbox.com/s_1BF3376CA98BAFE10DB74EBE85BC06524795D6EF2E135989FDF4D3456AEDBAE8_1617414606584_Server+deployement+using+Heroku+V2.png
--- 
# Deploying A Server To Heroku

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

![]()

## Getting Started

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
`git clone`

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.

## Introduction To 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**

1. Download the standalone binary file compatible with your machine from the [official download page](https://www.heroku.com/).
2. Extract the downloaded file 
3. 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**

1. Ensure you have Node installed on your computer
2. Run the following command `npm install -g heroku` to 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](https://devcenter.heroku.com/articles/heroku-cli).

**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:

1. Navigate to the project directory of the file to be deployed
2. Create a new file titled **`Procfile`** in 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`

1. 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.
2. Create a new Heroku application by running **`heroku create <unique-app-name>`**
3. Next, initialize a git repository for your project if it doesn't exist. Remember to add a `.gitignore` file to ignore the node modules directory & `.env file.`
4. If you made any changes, commit the files to git by running the following command

`$ git add .`
`$ git commit -m<Commit Message>`

1. 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 `heroku open`.

**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

1. Log in to your Heroku account and navigate to the dashboard.
2. Once in the dashboard, click the `new app` button fill the menu with a unique name for your new server & the region you want to deploy your new server.
3. 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
4. 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. 
5. 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 `.env` 
`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.

![Image Showing Config Vars](https://paper-attachments.dropbox.com/s_856396614BA6677E22317ADB62CD59808345D50604BB2323ABF270DD9E000EEE_1617378820434_7F9FBCEC-AE18-4E36-A86D-AC917B9495D7.png)

## Conclusion

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](https://hookdeck.com/?utm_source=devto&utm_medium=forum&utm_campaign=devto). Receive, monitor and manage all your webhooks from a single place. We handle the complexity so you can focus on building your product.