We have installed and configured the LEMP stack in our Production Server and we almost ready to deploy our Laravel Application. Now, to make our deployment more easy we need to setup the SSH key in our local computer as well as in our Production Server.

After we done these steps we'll be able to login to our server without need to enter any password. Also we can push or pull our application changes much easier.

You can see the communication between our local computer, Github and Production Server in the following diagram.

From our local computer we need to communicate with GitHub to push or pull our application changes to or from GitHub. And we need to login from our local computer to our Production Server then pull our Remote Git Repository.


Setting up up SSH in Local Computer to connect to Remote Git Repository

Alright, in the first part we are going to setup SSH key in our local computer to connect to Remote Git Repository, in this case Github.com. If you've SSH already setup you can skip this part and continue to next one.

Let's start off by opening new terminal window. If you're on Mac and using iTerm2, just hit Shell menu and choose New Tab.

Once the new window open you can run the following command to generate ths SSH key in ~/.ssh directory.

ssh-keygen -t rsa -b 4096

When you get "Enter a file in which to save the key" prompted, just hit Enter to accepts the default file location.

Enter a file in which to save the key (/Users/you/.ssh/id_rsa):

You will then get other prompts to enter passphrase. You can hit Enter to leave the passphrase empty.

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

Now if you look inside ~/.ssh directory, you'll find SSH key pair called id_rsa and id_rsa.pub.

ls ~/.ssh

The id_rsa is the private key and you don't share to anyone. While id_rsa.pub is the public key and we can share this key to public. Now you can open the id_rsa.pub with cat or your favourite editor then copy all content to clipboard.

cat ~/.ssh/id_rsa.pub

Next, go ahead and open your GitHub account. In the top right corner, click your profile, then click Settings.

In the user settings sidebar, click the SSH and GPG keys link.

Click New SSH key button on the top right side. In the Title input, add a short description for the new key. In my case I call "SSH for my MacBook Pro". In the Key input just paste your key.

Hit Add SSH key button to save your key. If confirm password prompted just enter your GitHub password.

Now you can test the connection from you local computer to GitHub by executing this command.

ssh -T git@github.com

Make sure you get this message.

Hi edomaru! You've successfully authenticated, but GitHub does not provide shell access.

If you got this such message:

git@github.com: Permission denied (publickey).

You can add your SSH private key to the ssh-agent by running this command:

ssh-add ~/.ssh/id_rsa

Last, you can configure your account name and email in GitHub global config:

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Now that we have our local computer SSH key setup and we now can connect to GitHub without need to enter password anymore.


Setting up up SSH in Local Computer To connect to Server

In the second part we're going to setup SSH key in our local computer to connect to our Server. Like in the first part we need to generate SSH key in our local computer. Then after that we'll put the public key in our Server.

Let's head over into our terminal. Then generate the SSH key by executing ssh-keygen command. This time we'll specify the f flag to specify the file name. In my case I call it laraveldemo.

ssh-keygen -t rsa -b 4096 -f ~/.ssh/laraveldemo

Once that done you can add the SSH key to ssh-agent by running following command.

ssh-add ~/.ssh/laraveldemo

Copy the public key and copy to clipboard.

cat ~/.ssh/laraveldemo.pub

Then, head over to another terminal window where you login to your remote server. Then create .ssh directory by running this command.

mkdir ~/.ssh

Inside this folder create a new file called authorized_keys and paste the public key from your local computer inside this file.

nano ~/.ssh/authorized_keys

And now you can exit from your remote server and re-login but now without need to enter your password.

ssh laravel@128.199.161.82

You can do the same thing to root user account if necessary.


Setting up up SSH in Server to connect to GitHub

To setup SSH key in your server to connect to GitHub the steps are almost the same that explained in first part. The difference is you do need to login to your remote server.

Here's the recap

ssh-keygen -t rsa -b 4096
cat ~/.ssh/id_rsa.pub
ssh -T git@github.com
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"