DevPush

How to connect to a web server using SSH

Introduction

This guide will show you how you can connect to and access web servers from your local PC or laptop. The web servers are expected to be Linux-based as that is what's used the most when it comes to web servers hosting websites.

When working on websites on your local machine you will eventually get to a point where you want to make the website available on the internet.

Amazon AWS Lightsail is used as an example web server so this guide will refer to it when setting up the SSH connection for both Putty and Unix shell methods.

When you need a web server

After you get to a point where your website developed on your local PC or laptop has been completed then the next step is to transfer it to the web server.

If you used a Backend programming language such as PHP, Python, Node.js etc to program the website then you will require a web server to host the website. This guide will focus on a Linux server since those are more commonly used rather than a Windows operating system for the server.

Frontend-only websites can easily be set up without requiring any kind of web server setup as frontend-specific web hosts such as Netlify and Vercel provide an online interface to manage your website so you don't need to access it directly.

Whether you use a cloud-based web host or a dedicated web host you will be provided with SSH details to access the web server.

The details you require are the following...

  • IP Address
  • Username
  • Password / Authentication key

An example of this would be an Amazon Lightsail server, once you set up a server then Amazon AWS provides you with login details to connect via SSH.

Putty - Windows

Please note this method is recommended if you use a Windows operating system such as Windows 11 or Windows 10, if you use MacOS or Linux then it's best to use the the SSH method below.

Alternatively if you are relatively new to working with web servers then to begin with the best option for you to transfer a website is to use an FTP client. An FTP client will allow you to drag and drop wherever your website files and folders are located on your PC or external USB device and move them to any file location on the web server.

Installation

To begin go to the Download Putty page and select the 64-bit x86 option as it's expected you would be running a new or recent version of Windows.

After the installer file has been downloaded then double click the file to run the installer for Putty. In the new Putty setup window that's popped up click the Next button to begin.

Either leave the default options as is or select/deselect the options you prefer, then click the Install button to start the installation.

After a few seconds the installation should complete successfully, click the Finish button to close the setup.

Setup SSH connection

The Putty SSH connection setup will be based on connecting to an Amazon Lightsail web server so in terms of connecting to non-Amazon web servers the below steps will vary.

Before you begin using the Putty application download the SSH key from the Amazon Lightsail dashboard or equivalent web server you use. For Amazon Lightsail click the Download default key to download the server authentication key file.

Amazon AWS uses pem files as their server authentication keys which require converting to a .ppk format for it to be used with Putty.

Go to the directory where Putty is installed in Windows which should be C:\Program Files\PuTTY and double-click the puttygen file to open the PuTTy Key Generator application.

In the Putty Key Generator application click the File option in the top menu and then select the Load private key option.

Go to where the pem file is located which will be in the Downloads folder if recently downloaded and then click the Open button.

A pop-up will show indicating that the import was successful and that the Save private key command is required to save the key in the Putty format.

As referenced in the pop-up window click the Save private key to save the private key to Putty's .ppk format.

A prompt will show that a passphrase hasn't been added, a passphrase offers extra security for the security key which prevents anyone from reading the security key. This key won't have a passphrase so click the Yes button to continue.

Now specify a place to save the key, name the new file and click the Save button.

Open the putty application which will look like the following screenshot.

In the Category left panel open the SSH menu and then the Auth menu by clicking the plus icons. Next, select the Credentials menu item to show the Credentials view.

Where it states Private key file for authentication click the Browse button.

Select the .ppk file that was recently created and click the Open button.

Now select the Session option in the Category panel. In the Session view enter the IP address of the web server in the Host Name text field. Leave the Port as 22 and the Connection type as SSH and then click the Open button at the bottom.

A terminal window will pop up requesting the username. As a LAMP image was used when setting up the web server the username will be bitnami. After entering the username press the Enter key.

The ppk file will be used with the username to authenticate the user, once authentication is successful you will be logged in as the server user.

Unix shell - MacOS / Linux

MacOS and Linux are both Unix systems which means that the terminal will work the same way for both operating systems. Both systems provide access to the terminal which can be searched for and opened.

After opening the terminal application go to the .ssh folder using the change directory command (cd) which should be inside of your user account directory.

If the .ssh folder doesn't exist then create the folder and change the current directory into it.

cd ~/.ssh

Wherever your secret key is get the full file path and use it to move the file to inside of the .ssh folder using the move command (mv).

mv ~/Documents/lightsail.pem ~/.ssh

A config file is required to store server connection details, use the nano text editor to do this.

nano config

After opening the nano editor, a host entry is required to set up a connection to the Amazon web server with the hostname, username and security key provided.

Use the following shortcuts to use the nano text editor.

  • Ctrl + Shift + V = Paste in the copied text
  • Ctrl + W = Save file
  • Ctrl + X = Exit nano text editor
Host web-server
  Hostname 35.176.81.243
  User bitnami
  IdentityFile ~/.ssh/lightsail.pem

After saving and exiting the file you can use the name of the Host as a reference when calling the SSH command to connect to the web server.

ssh web-server

You will get a prompt when connecting to the web server for the first time when the PC / laptop as the web server isn't aware of the machine you are connecting from. Type in yes and then press the Enter key to continue.

You may get the following text returned when attempting to connect to the web server.

Load key "/home/user/.ssh/lightsail.pem": bad permissions

Permission denied (publickey)

To resolve the issue change the file permissions for the security key by using the chmod command via the following command call.

sudo chmod 400 lightsail.pem

Try using the SSH web-server command again and it should work this time, you will be logged in as the web server user and see text relating to the web server if you do.

Setup has been completed and you can now connect to the web server every time to use the "ssh web-server" command.

Conclusion

At this point you will have connected to a web server no matter the operating system be it Windows, MacOS or Linux.

For Windows installing the SSH client application Putty allows you to easily connect to the web server without relying on a terminal. However, once connected to the web server, you would start making use of the terminal since web servers don't typically use GUIs.

For MacOS / Linux use the Unix shell to set up an SSH config which saves your connection details so every time you want to connect to the web server it is much simpler to connect.