Courses

Deploy Laravel Project to AWS EC2: Step-By-Step

Connect to The Server via SSH

To launch all the Laravel-related commands later, and to install/configure some software beforehand, we will need to connect to our server via SSH, with Terminal. Let's set it up.

  1. Navigate to the > Connect to instance page.

This can be done by clicking on the Connect to your instance button from the success page

Connect to your instance

or optionally from the instances menu by selecting instance and pressing connect button on the top right corner

Instances connect

  1. The page should look like this:

Connect to instance

Optionally you can copy the public IP for later somewhere else.

  1. To connect to your instance from the terminal, we need to choose the SSH Client tab:

SSH Client

Here are exact instructions on how to connect to your server using the key you generated and downloaded when creating an EC2 instance and it works perfectly fine.

The only problem with that is it's not very convenient to have such a long command to remember or paste every time, and in addition, you need to be in a directory where the key file lies. So let's tweak this a bit.

These steps are optional

  • Create a .ssh folder in your home directory if it doesn't exist, this is where usually SSH keys are stored
user@local$ mkdir -p ~/.ssh
  • Move the downloaded key to the .ssh directory
user@local$ mv Downloads/ec2-demo-web-ubuntu-server.pem ~/.ssh
  • Modify permissions so only your user can read the key
user@local$ chmod 400 ~/.ssh/ec2-demo-web-ubuntu-server.pem
  • You can have your own IP address to URL mapping by overriding returned DNS value or trying to remember the exact IP address. Let's append the 18.195.117.231 ubuntu-aws line to the /etc/hosts file:
root@local# echo "18.195.117.231 ubuntu-aws" >> /etc/hosts

Now you can substitute the IP address with ubuntu-aws in your shell commands and it resolves into 18.195.117.231.

By default server drops all ICMP requests, which means if you ping it won't respond

user@local$ ping ubuntu-aws
PING ubuntu-aws (18.195.117.231) 56(84) bytes of data.
^C
--- ubuntu-aws ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2072ms

You can check if SSH is accessible instead

user@local$ nmap -p 22 ubuntu-aws
Starting Nmap 7.93 ( https://nmap.org ) at 2022-11-01 01:23 EET
Nmap scan report for ubuntu-aws (18.195.117.231)
Host is up (0.033s latency).
 
PORT STATE SERVICE
22/tcp open ssh
 
Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds

The host is reachable, great!

  • Connect to your server with this command from any folder in your shell:
user@local$ ssh -i ~/.ssh/ec2-demo-web-ubuntu-server.pem ubuntu@ubuntu-aws

And also you can have an alias by entering alias connect-ubuntu-aws="ssh -i ~/.ssh/ec2-demo-web-ubuntu-server.pem ubuntu@ubuntu-aws" so you can connect to your server only by typing:

user@local$ connect-ubuntu-aws

For this alias to persist add the alias connect-ubuntu-aws="ssh -i ~/.ssh/ec2-demo-web-ubuntu-server.pem ubuntu@ubuntu-aws" command to your ~/.bashrc or ~/.zshrc file, depending what shell you do use.

After a successful connection your terminal window might look similar to this:

user@local$ connect-ubuntu-aws
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-1019-aws x86_64)
 
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
 
System information as of Tue Nov 1 01:23:59 UTC 2022
 
System load: 0.0 Processes: 99
Usage of /: 25.5% of 7.57GB Users logged in: 0
Memory usage: 24% IPv4 address for eth0: 172.31.44.101
Swap usage: 0%
 
* Ubuntu Pro delivers the most comprehensive open-source security and
compliance features.
 
https://ubuntu.com/aws/pro
 
79 updates can be applied immediately.
45 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable
 
 
Last login: Tue Nov 1 01:23:59 2022 from xx.xx.xxx.xxx
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
 
ubuntu@ip-172-31-44-101:~$

Don't get confused by a different IP address on the ubuntu@ip-172-31-44-101:~$ prompt, this is the server's local IP and not the public one you used to connect.

avatar

These steps are optional user@local$ mkdir -p ~/.ssh

how can one to open ssh client and run this command. can we run this command on our local computer through command prompt window terminal or we have to open AWS CloudShell on aws to run this command. Please guide us on this step. I'm stuck here.

avatar

user@local - shows that this is entered on your local machine, not remote server.

avatar

When i try to do "mkdir -p ~/.ssh" in the command prompt. I get an error saying "C:\Users\pavis>mkdir -p ~/.ssh The syntax of the command is incorrect.". Someone please guide me .

I am working in windows.

avatar

This command needs to be run on Linux machine.

To manage SSH settings and key setup please refer to Windows documentation.

Alternative option could be Cygwin.