back
written on 2 January 2024
How to Set Up AWS Lightsail as a Virtual Machine
rcg   ·   2 January, 2024
Steps
  1. log into AWS Lightsail and click on Create instance
  2. note the zone by the Country flag (e.g. US flag zone: oregon); change if need be
  3. select Linux/Unix, OS Only, Latest Ubuntu (22.04 LTS as of this writing)
  4. scroll down and select an instance plan (I picked the least expensive since upscaling is easier)
  5. name the instance (e.g. example-com-oregon and click Create instance
  6. once created, click on the instance name to get to the instance's dashboard
  7. click on the Networking tab and then click on Attach static IP (under the Publc IPV4)
  8. give the IP a name (e.g. experiment-com-ip) and click Create and attach
  9. go to your DNS register and add records to connect your custom ip name to this static IP:

    HostnameTypeTTLData
    @A360055.55.555.555
    wwwA360055.55.555.555
    @AAAA36005555:1f55:2e7:1700:76c9:d672:4dd6:5555
    wwwAAAA36005555:1f55:2e7:1700:76c9:d672:4dd6:5555

  10. get ssh credentials by going to the Connect tab and click on Download default key
  11. save it in your ~/.ssh directory (LightsailDefaultKey-us-west-2.pem)
  12. chmod 600 ~/.ssh/LightsailDefaultKey-us-west-2.pem
  13. create an alias to ssh to your machine, replacing 55.55.555.555 with your static ip:
    alias sshExampleCom='cd $HOME;ssh -i ./.ssh/LightsailDefaultKey-us-east-2.pem [email protected]'

    or create sshExampleCom.bat
    :: sshExampleCom.bat -- ssh to example.com @echo off ssh -i ~/.ssh/LightsailDefaultKey-us-west-2.pem [email protected]

  14. if you want to copy local files to the remote machibe, use this one-liner (changing the paths):
    rsync --chmod=Du=rwx,Dgo=rx,Fu=rw,Fog=r --exclude={'*~','#*#','.#*'} --rsync-path="sudo /usr/bin/rsync" -avze "ssh -i /cygdrive/c/rcg/.ssh/LightsailDefaultKey-us-west-2.pem" /cygdrive/c/rcg/src/projects/www/www.example.com/public/ [email protected]:/var/www/public

    or create rsyncExampleCom.bat
    :: rsyncExampleCom.bat -- sync local to server @echo off set params=--chmod=Du=rwx,Dgo=rx,Fu=rw,Fog=r --exclude={'*~','#*#','.#*'} --rsync-path="sudo /usr/bin/rsync" -avze set key="ssh -i /cygdrive/c/rcg/.ssh/LightsailDefaultKey-us-west-2.pem" set from=/cygdrive/c/rcg/src/projects/www/www.example.com/public/ set to=[email protected]:/var/www/public

On the remote linux machine run the commands:
  1. sudo apt update
  2. sudo apt dist-upgrade
  3. sudo apt autoclean
  4. sudo apt autoremove
  5. sudo reboot

ssh to the rebooted instance, then
  1. sudo apt install unzip
  2. sudo apt install emacs-nox

back on your local machine copy your .emacs.d to the remote:
  1. zip ~/.emacs.d
  2. rsync -avze "ssh -i ~/.ssh/LightsailDefaultKey-us-west-2.pem" ~/.emacs.d.zip [email protected]:~/emacsConfig.zip
Then back on the remote machine:
  1. unzip emacsConfig.zip (creating ~/.emacs.d directory)
  2. Then, for example, I edited my .bashrc emacs .bashrc
    #turn off color export TERM=xterm-mono export EDITOR=emacs export PATH=~/bin:$PATH # set the prompt PS1='${debian_chroot:+($debian_chroot)}\u:\w\$ ' alias cls='clear' alias cup='find . -type f -name "*~" -delete -or -name "#*#" -delete; ls -al' alias ls='ls -al' alias ll='ls -alt' alias md='mkdir' ## maybe do these #set the ls colors #eval "`dircolors -b ~/.dircolors`" alias ls='ls --color=auto' LS_COLORS=$LS_COLORS:'di=01;32:ln=01;32:ex=00;36:' ; export LS_COLORS alias cup='find . -type f -name "*~" -delete -or -name "#*#" -delete; ls -al --color' alias ls='ls -al --color' alias ll='ls -al --color'

To prepare for a web server:
  1. In the browser, on the AWS Lightsail dashboard for the instance, click on the Networking tab
  2. scroll to the firewall rules and add HTTPS TCP 443 for both IPV4 and IPV6
  3. Add apache, node, or see How To Set Up the Caddy Web Server

You might also want to learn more about setting up a cost budget.

References
  1. An overview on Windows Terminal
  2. Domain registration in Lightsail
  3. How to control your AWS costs with the Free Tier and Budgets