Ravindra BagaleCourses & study guides मराठी Track your progress

How to Use scp to Copy Files to EC2 (Upload and Download)

In the previous chapter we put images on our page from another website and with wget. But a developer doesn't hand you a web address; they hand you files on a laptop. Now we learn scp, the command that copies files from your laptop to the server and back, safely. We upload an image and a PDF, download a file, upload a whole website as one zip file, take a backup of a live site, and at the end we replace Apache with Nginx on the same server. Keep your server and your laptop terminal ready.

What you'll learn in this class

  • Why wget is not enough, and what scp does
  • Why it's called secure copy
  • The scp command: -i key, source and destination, and the colon
  • Running scp from the laptop terminal, not inside SSH
  • Uploading an image and using it on a page
  • Case-sensitive names and the ../ path
  • PDFs, spaces in names and %20
  • Downloading from the server to your laptop
  • Naming rules for files and folders
  • Uploading a whole website as one zip file, and scp -r
  • Unzipping in /var/www/html and fixing html inside html
  • Taking a backup of a live website
  • Apache, Nginx and Tomcat
  • Switching the same server from Apache to Nginx

1. Why scp: wget only downloads from websites

Why. wget runs on the server and downloads a file from a web address. It can't do two jobs you need every day:

  1. Upload: copy a file from your laptop to the server. Your laptop is not a website, so the server can't wget from it.
  2. Download: copy a file from the server to your laptop, for example a backup.

What. scp (secure copy) does both. It copies files between two machines over SSH, with the same key pair you use to log in. Any kind of file works: an image, a video, a PDF, a zip, a text file.

Classroom line

To download from the server or upload to the server: the scp command.

Why "secure". Your file travels over the public internet between your laptop and AWS. scp encrypts it on the way, so anyone who captures it in between sees only scrambled data, not your photo or your code.

Classroom line

They may see the data, but only as encrypted text; they won't understand what's inside.

wget only downloads from websites; scp copies yourown files over SSH A website https://…/beach.jpg wget URL EC2 server /home/ec2-user wget runs on the server and pulls a file from a web address. Itcannot reach your laptop. Your laptop beach.jpg site.zip upload ↓ · download ↑ EC2 server /home/ec2-user scp runs on your laptop and copies files both ways over SSH, withthe same key. The data is encrypted on the way. scp = secure copy. Anyone in between sees only scrambled data,not your file.

Figure 1. wget runs on the server and pulls a file from a website. scp runs on your laptop and copies files both ways over SSH, encrypted.

2. The scp command

Why. You already type ssh to log in. scp reuses the same key, user and IP, so there is nothing new to set up: you only add what to copy and where.

What. You already know the SSH command:

ssh -i mykey.pem ec2-user@PUBLIC_IP

scp looks almost the same, with a source and a destination added:

scp -i mykey.pem SOURCE DESTINATION
  1. scp: the program.
  2. -i mykey.pem: your key, the same one you use for SSH.
  3. SOURCE: the file you copy.
  4. DESTINATION: where the copy goes.

When one side is the server, write it as user@server:path. The colon is what tells scp "this is on another machine":

  1. ec2-user@PUBLIC_IP:. puts the file in the home folder, /home/ec2-user. The dot means "the home folder".
  2. ec2-user@PUBLIC_IP: with nothing after the colon also means the home folder.
  3. ec2-user@PUBLIC_IP:ravi puts it in the folder ravi inside the home folder.
Reading an scp command scp -i mykey.pem beach.jpg ec2-user@PUBLIC_IP:. scp the program   secure copy -i mykey.pem -i and your key   same key as ssh beach.jpg source   the file to copy ec2-user@PUBLIC_IP:. destination   user@server : path The colon decides: after it comes the path on the server ec2-user@PUBLIC_IP:. . = the home folder, /home/ec2-user ec2-user@PUBLIC_IP: colon alone = also the home folder ec2-user@PUBLIC_IP:ravi the folder ravi inside the home folder No colon at all? scp makes a copy on your laptop with thatname. Nothing goes to the server.

Figure 2. The four parts of an scp command, and what the colon and the path after it mean.

Example 1: the colon matters. If you forget the colon, scp doesn't connect to anything. It just makes a copy on your laptop, with a file name like ec2-user@PUBLIC_IP., and nothing goes to the server. Always end the server side with : and a path.

3. Upload an image and use it on a page

Why. This is the everyday job: a photo on your laptop has to appear on the website.

Where to run scp. On your laptop, in a second terminal window or tab (PowerShell on Windows, Terminal on Mac or Linux). Not inside the SSH session: the server can't see your laptop's files, so the copy has to start from the laptop.

Classroom line

Where do you run this command? Outside the server.

How. Suppose your key mykey.pem is in Downloads and the photo beach.jpg is on the Desktop.

Step 1. Open PowerShell (or Terminal). It starts in your user folder, so Downloads and Desktop are right there.

Step 2. Type the command. Use Tab to fill in the paths: type two or three letters and press Tab.

scp -i .\Downloads\mykey.pem .\Desktop\beach.jpg ec2-user@PUBLIC_IP:.

On Windows the folders in a path are separated by a backslash, \. On Mac or Linux they are separated by /: scp -i Downloads/mykey.pem Desktop/beach.jpg ec2-user@PUBLIC_IP:.

Step 3. The first time you connect to a server from this laptop, you're asked to trust it. Type yes:

The authenticity of host 'PUBLIC_IP (PUBLIC_IP)' can't be established.
ED25519 key fingerprint is SHA256:...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Step 4. Wait for 100%:

beach.jpg                         100%   ...KB   ...KB/s   00:00

Classroom line

After doing this, what should happen? It should show 100%.

Step 5. In your SSH session, check that it arrived and move it into the web root:

[ec2-user@ip-172-31-xx-xx ~]$ ls
beach.jpg
[ec2-user@ip-172-31-xx-xx ~]$ sudo mv beach.jpg /var/www/html/

The file lands in the home folder because that's where ec2-user may write. /var/www/html belongs to root, so the move needs sudo.

Step 6. Use it on a page:

cd /var/www/html
sudo nano beach.html

Type:

<img src="beach.jpg" width="600">

Step 7. Open http://PUBLIC_IP/beach.html.

Classroom line

Don't type the whole path by hand. Type one or two characters and press Tab.

If the site doesn't open after you restarted the instance.

  1. Apache is stopped after a stop and start. Run sudo service httpd start (or sudo systemctl enable httpd once, so it starts at every boot).
  2. Type http:// in front of the IP yourself. If the browser tries https:// (port 443), and the Security Group allows only port 80, the page waits and then times out.

Classroom line

When we stop the server, the web server stops too.

4. Case-sensitive names and the ../ path

Why. A file that uploaded fine can still show as a broken image. The two usual reasons are a capital letter in the name and a wrong relative path, and both are quick to check.

Example 2: a capital letter breaks the image. The uploaded file is called Beach.jpg, but the page says <img src="beach.jpg">. The page shows a broken image. Linux file names are case-sensitive: Beach.jpg and beach.jpg are two different names. Write the name exactly as ls shows it, or rename the file.

Example 3: an image one folder up. Make a sub-folder with its own page:

cd /var/www/html
sudo mkdir gallery
cd gallery
sudo nano photos.html

The image is in /var/www/html, one level above gallery. So the page goes one level up with ../:

<img src="../beach.jpg">

Open http://PUBLIC_IP/gallery/photos.html. With only src="beach.jpg", the browser would look for /gallery/beach.jpg and show a broken image.

An image one folder up: ../ /var/www/html/ ├── beach.jpg ├── index.html └── gallery/ └── photos.html Inside gallery/photos.html <img src="../beach.jpg"> .. = the folder one level up (html). Just beach.jpg would look inside gallery and give a broken image. Names are case-sensitive too: Beach.jpg andbeach.jpg are two different files on Linux.

Figure 3. photos.html is inside gallery and the image is one level up, so the src is ../beach.jpg. Linux names are case-sensitive.

5. A PDF, and spaces in names

Why. Real sites serve more than images: brochures, forms, price lists. And files from laptops often have spaces in their names, which the shell splits unless you quote them.

How. Any file type works the same way. Upload a PDF (if you have none, download a sample PDF from the internet):

Step 1. On the laptop:

scp -i .\Downloads\mykey.pem .\Downloads\notes.pdf ec2-user@PUBLIC_IP:.

Step 2. On the server:

sudo mv notes.pdf /var/www/html/

Step 3. Open http://PUBLIC_IP/notes.pdf. The browser opens the PDF.

Example 4: a space in the name. A file called my notes.pdf causes two problems:

  1. On the command line you must put the name in quotes: ".\Downloads\my notes.pdf", otherwise scp sees two names, my and notes.pdf.
  2. In a URL a space is written as %20: http://PUBLIC_IP/my%20notes.pdf.

Classroom line

If your PDF's name has a space, you get problems when writing the URL.

6. Download from the server to your laptop

Why. Sometimes you need a file from the server: a log to send to a developer, or a page someone edited on the server. Downloading is the same command with the two sides swapped.

What. Swap the two sides: the source is now the server (with its colon), and the destination is a folder on your laptop. You still run it on the laptop.

Upload and download: only sourceand destination swap Laptop terminal PowerShell or Terminal, NOT inside the SSH session Downloads\ beach.jpg EC2 server /home/ec2-user/ beach.jpg notes.txt upload download Upload: source = the file on the laptop,destination = the server with a colon. scp -i mykey.pem beach.jpgec2-user@PUBLIC_IP:. Download: source = the server with acolon, destination = a folder on the laptop. scp -i mykey.pemec2-user@PUBLIC_IP:notes.txtDownloads\

Figure 4. Animation: upload and download both run in the laptop terminal; only the source and the destination swap places.

How.

Step 1. On the server, make a small file and log out:

nano notes.txt
exit

Step 2. On the laptop:

scp -i .\Downloads\mykey.pem ec2-user@PUBLIC_IP:notes.txt .\Downloads\

notes.txt without a folder after the colon means "in the home folder". To copy into the folder you're in on the laptop, use . as the destination.

Step 3. Open Downloads\notes.txt in Notepad.

Classroom line

scp is always run outside the server.

This is true for copies between your laptop and a server. The one case where you run scp inside a server is a copy from one server to another server.

7. Naming rules that save you trouble

Why. Every rule here prevents a real error you would otherwise debug later: a broken image, a split command, a Tab that won't complete.

  1. Use small letters only. Linux is case-sensitive, and Tab completion only works if you type the first letters in the right case.
  2. No spaces. They need quotes on the command line and become %20 in URLs.
  3. Join words with an underscore or nothing: my_folder or myfolder, beach_photo.jpg.

Classroom line

Don't use capital letters at all. Always use small letters.

8. A whole website as one zip file

Why. A developer gives you a website folder with many files: pages, images, CSS, JavaScript. You can copy a folder directly with scp -r (recursive):

scp -i .\Downloads\mykey.pem -r .\Desktop\html ec2-user@PUBLIC_IP:.

For many files a zip is usually better:

  1. It's one file, so one copy, and easy to check.
  2. It's smaller. Compression stores repeated data once: ten As in a row can be stored as "A, ten times".
  3. It's faster to transfer, and nothing gets left out halfway.
A whole website as one zip file 1. Zip on the laptop Windows: right-click the folder html › Send to ›Compressed (zipped) folder One file instead of many, and smaller: repeated data is stored once. 2. scp the one file (to the home folder) scp -i mykey.pem html.zip ec2-user@PUBLIC_IP: Colon alone = the home folder. scp -r can copy a folder directly, but onezip is simpler. 3. Into the root (needs sudo) sudo mv html.zip /var/www/html/ ec2-user cannot write to /var/www/html, so sudo. 4. Unzip with sudo (the folder is root's) cd /var/www/html ; sudo unzip html.zip Without sudo: checkdir error: cannot create html, Permission denied. 5. Fix html in html (move files up one level) cd html ; sudo mv * .. ; cd .. ; ls The zip held the folder html, so the files landed in html/html. Move themup.

Figure 5. Animation: zip on the laptop, scp the one file, sudo mv into /var/www/html, sudo unzip, then move the files up out of the extra html folder.

How.

Step 1. On Windows, right-click the website folder, here called html, and choose Send to › Compressed (zipped) folder. You get html.zip. Open it once to check that every file is inside.

Step 2. Upload it. The colon alone means the home folder:

scp -i .\Downloads\mykey.pem .\Desktop\html.zip ec2-user@PUBLIC_IP:

Step 3. On the server, move it into the web root:

sudo mv html.zip /var/www/html/

If you open http://PUBLIC_IP/html.zip now, the browser just downloads it. A browser can't show a zip file, and files it can't show are downloaded.

Classroom line

The browser doesn't understand a zip file... a file it doesn't understand gets downloaded.

Step 4. Unzip it. Without sudo it fails, because the folder belongs to root:

[ec2-user@ip-172-31-xx-xx html]$ unzip html.zip
Archive:  html.zip
checkdir error:  cannot create html
                 Permission denied
...

With sudo it works:

sudo unzip html.zip

unzip comes installed on Amazon Linux.

Step 5. Windows put the folder itself into the zip, so the files are now in /var/www/html/html/. Move them up one level:

cd html
sudo mv * ..
cd ..
ls

.. is the folder one level up. * means every file here, except hidden files whose names start with a dot.

Classroom line

But html inside html doesn't look right, so let's move these files into html.

Step 6. Open http://PUBLIC_IP/ and a page or image from the zip by its name.

9. Back up a live website to your laptop

Why. Before you change a live site, keep a copy of what works. If something breaks, you can put it back.

Back up a live website to your laptop 1. Go one level up cd /var/www do not zip the folder you're standing in 2. Zip everything sudo zip -r backup.zip html 3. Move to home sudo mv backup.zip ~/ 4. Download on the laptop: scp …:backup.zip 5. Check it extract and open a few files -r takes every subfolder and hidden file. html/* without -rwould miss what is inside the subfolders. Home folder = a short, simple path for scp. A backup you never openedis not a backup yet.

Figure 6. Go up to /var/www, sudo zip -r the html folder, move the zip to the home folder, download it with scp, and open it to check.

How.

Step 1. Go one level up from the folder you want to zip. Don't zip a folder while you're standing inside it.

cd /var/www

Classroom line

Don't cut the branch you're sitting on.

Step 2. Zip the whole folder. /var/www belongs to root, so sudo; -r takes every subfolder and hidden file:

[ec2-user@ip-172-31-xx-xx www]$ sudo zip -r backup.zip html
  adding: html/ (stored 0%)
  adding: html/index.html (deflated ...%)
  ...

html/* without -r would leave out what's inside the subfolders.

Step 3. Move the zip to the home folder, so the scp path stays short and simple:

sudo mv /var/www/backup.zip /home/ec2-user/

scp only needs permission to read the file; a short path just makes the command easy to type.

Step 4. On the laptop, download it:

scp -i .\Downloads\mykey.pem ec2-user@PUBLIC_IP:backup.zip .\Downloads\

Step 5. Extract it on the laptop and open a few files, a page, an image, a video, to be sure the backup really works.

10. Apache, Nginx and Tomcat

Why. You will see all three names in job posts and on servers. Knowing which does what tells you which one to install for a given project.

What. Three names you will meet:

  1. Apache HTTP Server (httpd): the web server we used so far.
  2. Nginx: another free web server. It handles many visitors at the same time with little memory, so many busy sites use it, often in front of other servers. It serves the same HTML, images and videos.
  3. Apache Tomcat: a server that runs Java web applications. Apache httpd and Nginx serve files; they don't run Java code. They can sit in front of Tomcat and pass requests to it.

Which web server a project uses is usually decided by the architect or team lead. As a cloud engineer you should be able to set up both.

11. Switch the same server from Apache to Nginx

Why. Apache and Nginx both listen on port 80, and only one program can use a port at a time. So you stop Apache first. If you start Nginx while Apache is running, Nginx fails to start:

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.

What. The two use different web folders:

  1. Apache: /var/www/html
  2. Nginx: /usr/share/nginx/html
One port 80, one web server at a time Apache (httpd): stopped sudo service httpd stop Web root: /var/www/html Your files stay here. Copy them, don't move them, so Apache still has them if you switch back. Nginx: running on port 80 sudo yum install nginx -y sudo service nginx start Web root: /usr/share/nginx/html Comes with index.html, 404.html, 50x.html, nginx-logo.png and poweredby.png. sudo cp -r /var/www/html/*/usr/share/nginx/html/ Your index.html replaces the Nginx welcomepage. Same files, same URLs, now served byNginx.

Figure 7. One port 80: stop Apache, install and start Nginx, then copy your site from /var/www/html into /usr/share/nginx/html.

Handwritten board: sudo service httpd stop, sudo yum install nginx -y, sudo service nginx start, and sudo cp -r /var/www/html/* /usr/share/nginx/html circled; on the right 80 above /var/www/html and /usr/share/nginx/html.

Whiteboard: switching to Nginx: stop httpd, install and start nginx, then copy /var/www/html/* into /usr/share/nginx/html. Only one of them can use port 80.

How.

Step 1. Stop Apache:

sudo service httpd stop

Step 2. Install Nginx:

sudo yum install nginx -y

Step 3. Start it and check it:

[ec2-user@ip-172-31-xx-xx ~]$ sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
[ec2-user@ip-172-31-xx-xx ~]$ sudo service nginx status

Look for Active: active (running). A service runs all the time in the background and waits for requests.

Step 4. Look at Nginx's web folder:

[ec2-user@ip-172-31-xx-xx ~]$ cd /usr/share/nginx/html
[ec2-user@ip-172-31-xx-xx html]$ ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png

Step 5. Open http://PUBLIC_IP/. You may still see your old Apache page: the browser showed a saved copy from its cache. Refresh, and you see Welcome to nginx!

Browser page with the heading Welcome to nginx! and text saying nginx is successfully installed and working, with links to nginx.org and community.nginx.org.

Whiteboard: the Nginx welcome page: Nginx is installed and serving its own index.html from /usr/share/nginx/html.

Classroom line

That's called cache; that page was cached.

Step 6. Open a page that doesn't exist, like http://PUBLIC_IP/ravi.html. Now Nginx answers with its own 404 page, which comes from /usr/share/nginx/html/404.html.

Page titled nginx error! with a red bar The page you are looking for is not found, and a box saying this is the default 404 error page for nginx distributed with Amazon Linux, located at /usr/share/nginx/html/404.html.

Whiteboard: the Nginx 404 page on Amazon Linux: the page you are looking for is not found, served from /usr/share/nginx/html/404.html.

Step 7. Copy your site from Apache's folder into Nginx's folder. Copy, don't move, so Apache still has the files if you switch back:

cd /usr/share/nginx/html
sudo cp -r /var/www/html/* .

Step 8. Run ls, then refresh the browser. Your own index.html has replaced the Nginx welcome page, and every page and image opens at the same URL as before, now served by Nginx.

Classroom line

We stopped Apache, installed Nginx, started Nginx...

/var/www/html/* skips hidden files (names starting with a dot). To copy those too, copy the folder's contents with a dot instead:

sudo cp -r /var/www/html/. /usr/share/nginx/html/

Step 9. Run history to see every command you ran, in order. Keep it as your notes.

12. The whole flow on one page

  1. Upload: scp -i mykey.pem file ec2-user@PUBLIC_IP:., run on the laptop.
  2. Download: scp -i mykey.pem ec2-user@PUBLIC_IP:file ., also on the laptop.
  3. On the server: sudo mv file /var/www/html/.
  4. Pages: names exactly as ls shows them; ../ for one folder up; %20 for a space in a URL (better: no spaces).
  5. A website: zip it (or scp -r the folder), upload, sudo mv, sudo unzip, fix html/html with sudo mv * ...
  6. Backup: cd /var/www, sudo zip -r backup.zip html, move it home, download, open it.
  7. Nginx: sudo service httpd stop, sudo yum install nginx -y, sudo service nginx start, sudo cp -r /var/www/html/* /usr/share/nginx/html/.

13. Hands-on lab: upload, publish and back up with scp

Why this lab. Getting files onto a server and copies back off it is a daily job. This lab uploads an image, publishes it, downloads a page, and takes a zip backup of the whole site. Run the scp commands in your laptop terminal, not inside SSH.

  1. Upload an image from your laptop
    • Command: scp -i mykey.pem beach.jpg ec2-user@PUBLIC_IP:.
    • You should see: A progress line ending in 100%.
    • Check: In your SSH window, ls ~ shows beach.jpg.
    • Why: Source first, destination second. The colon marks the server side; . after it means ec2-user's home, the one place you can write without sudo.
  2. Publish it
    • Command: sudo mv ~/beach.jpg /var/www/html/
    • You should see: Nothing.
    • Check: http://PUBLIC_IP/beach.jpg shows the picture.
    • Why: /var/www/html belongs to root, so scp as ec2-user can't write there directly. Upload to home, then move with sudo.
  3. Upload a file with a space in its name
    • Command: scp -i mykey.pem "price list.pdf" ec2-user@PUBLIC_IP:.
    • You should see: A progress line ending in 100%.
    • Check: ls ~ shows 'price list.pdf'.
    • Why: Without quotes, the shell sees two files, price and list.pdf. Better still, rename it to price-list.pdf before uploading.
  4. Download a page to your laptop
    • Command: scp -i mykey.pem ec2-user@PUBLIC_IP:/var/www/html/index.html .
    • You should see: A progress line ending in 100%.
    • Check: index.html is now in your laptop's current folder.
    • Why: Swap the sides: the server (with its colon) is the source; . is the laptop folder you are in. Reading needs no sudo, because the file is readable by everyone.
  5. Zip the whole site on the server
    • Command: cd /var/www && sudo zip -r ~/backup.zip html
    • You should see: One adding: line per file.
    • Check: ls -l ~/backup.zip shows a size above 0.
    • Why: One zip is one file to copy, instead of hundreds. -r goes into every subfolder; going one level up keeps the html folder itself inside the zip.
  6. Download the backup
    • Command: scp -i mykey.pem ec2-user@PUBLIC_IP:backup.zip .
    • You should see: A progress line ending in 100%.
    • Check: Open the zip on your laptop: you see html/ with index.html and beach.jpg inside.
    • Why: A backup only counts when it is off the server and you have opened it once. If the server is lost, this copy is what you restore from.
  7. Clean up and stop
    • Command: rm -f ~/backup.zip ~/"price list.pdf"
    • You should see: Nothing.
    • Check: ls ~ is clean. Then stop the instance.
    • Why: Old zips on the server fill the 8 GB disk over time. Stopping saves free-tier hours.

14. Try at home

Try at home

Task 1: upload

  1. Upload a photo with scp and show it on photo.html.
  2. Upload it again with a capital letter in the name and see what happens to the page.
  3. Put a page in /var/www/html/gallery/ that shows the photo with ../.

Task 2: download

  1. Create notes.txt on the server and download it into Downloads.

Task 3: a website as a zip

  1. Zip a folder with two pages and an image, upload it, unzip it into /var/www/html and open both pages.
  2. Copy the same folder once with scp -r instead.

Task 4: backup

  1. Take a backup of /var/www/html with sudo zip -r, download it and open it on the laptop.

Task 5: Nginx

  1. Switch to Nginx, see the welcome page and the 404 page, then copy your site and check it.

When you're done, stop the instance.

Recap

In short

  1. wget only downloads from websites; scp copies files between your laptop and the server over SSH, encrypted.
  2. scp -i key SOURCE DESTINATION; the server side is ec2-user@PUBLIC_IP:path. :. or : alone = home folder, :ravi = a folder in it. No colon = only a local copy.
  3. Run scp in the laptop terminal, not inside SSH. Server-to-server copies are the exception.
  4. Files land in the home folder; sudo mv them into /var/www/html.
  5. Names are case-sensitive; ../ goes one folder up; spaces become %20. Use small letters, no spaces.
  6. Upload a website as one zip (or scp -r the folder), then sudo unzip, and sudo mv * .. if it unpacked into html/html.
  7. Backup: sudo zip -r from /var/www, move it home, download with scp, open it to check.
  8. Nginx and Apache both use port 80: stop one before starting the other. Nginx's web root is /usr/share/nginx/html. Tomcat runs Java web apps.

Ravindra Bagale, trainer: linkedin.com/in/ravindra-bagale. The file names (beach.jpg, notes.pdf, notes.txt, html.zip, backup.zip, mykey.pem) and the folders ravi and gallery are examples for learning; PUBLIC_IP stands for your instance's public IPv4 address. Progress numbers and fingerprints are shortened with "..."; the hostname in the prompt is illustrative.

Practice Tasks

About this scenario

Latur Coaching Classes is a coaching business in Latur. Pooja Jadhav looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy Restart a service after a change

Why: Config changes only apply after the service restarts or reloads.

What to do: Restart the nginx service.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check if a service is running

Why: When a site is down, first check whether the web server process is running.

What to do: Show the status of the httpd service.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Install a package on Ubuntu

Why: Ubuntu uses apt. Package names differ: on Ubuntu Apache is apache2.

What to do: Install the apache2 package on Ubuntu without being asked to confirm.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Install a package on Amazon Linux

Why: dnf is the package manager on Amazon Linux 2023. -y answers yes for you.

What to do: Install the httpd package on Amazon Linux 2023 without being asked to confirm.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Test a website from the server

Why: curl shows whether the web server answers, even before DNS is ready.

What to do: Request http://localhost and show only the response headers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Download code from GitHub

Why: git clone copies the whole repository to the server, ready to deploy.

What to do: Clone https://github.com/laturcoaching/website.git into the current folder.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Port for SSH

Why: Security group rules need the right port.

What to do: Which TCP port must the security group open for SSH?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Test the nginx config

Why: A test catches typos before a restart takes your site down.

What to do: Check the nginx configuration files for errors.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Start a service now and at boot

Why: enable makes it start after reboot; --now also starts it right away.

What to do: Enable the nginx service and start it immediately, in one command.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Open port 80 to the world

Why: Visitors can reach your website only if the security group allows HTTP.

What to do: Allow inbound TCP port 80 from 0.0.0.0/0 on security group sg-023666135aa54c3b2.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium CloudFormation: web security group

Why: Opening only 443 keeps the server safer.

What to do: Template with an AWS::EC2::SecurityGroup that allows TCP 443 from 0.0.0.0/0. Add a GroupDescription.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Hard Enable an nginx site on Ubuntu

Why: On Ubuntu, a site is enabled by a symbolic link from sites-available to sites-enabled.

What to do: Create a symbolic link to /etc/nginx/sites-available/laturcoaching inside /etc/nginx/sites-enabled/.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: Copy files with scp. wget only downloads from URLs. Your own files are on your laptop. scp copies files over SSH, encrypted. The colon marks the server side. Run it on the laptop, not inside SSH: scp -i KEY SOURCE DEST. Upload:
Revision card: Copy files with scp · Download PNG
Revision card: Download from the server. Backups and logs must come back to your laptop. Swap source and destination: server first, local path last. . means the current laptop folder. Download:
Revision card: Download from the server · Download PNG
Revision card: Upload a site as a zip. Copying 500 small files one by one is slow. Zip the folder, upload one file, unzip on the server. Unzip in /var/www/html; if you get html/html, move files up. On the server:
Revision card: Upload a site as a zip · Download PNG
Revision card: Back up a live website. Before changes, you need a way back. A backup is a dated zip of the web folder, copied off the server. zip on the server, then scp it to your laptop. Zip:
Revision card: Back up a live website · Download PNG
Comparison: wget vs scp. wget: Downloads a file from a URL onto the server. scp: Copies files between your laptop and a server over SSH.
Comparison: wget vs scp · Download PNG
Interview card: What does the colon mean in an scp command?. It marks the remote side: user@host:path. Without it scp makes a local file named after the host. Right:
Interview card: What does the colon mean in an scp command? · Download PNG
Interview card: Why run scp on the laptop, not inside SSH?. The laptop file lives on the laptop; the server cannot see it. Inside SSH, paths point to the server. Open a second terminal on the laptop for scp.
Interview card: Why run scp on the laptop, not inside SSH? · Download PNG
Interview card: A PDF with spaces in its name gives 404. Why?. Spaces become %20 in URLs, and typos creep in. Use names without spaces: lowercase and hyphens. "My Notes.pdf" › my-notes.pdf.
Interview card: A PDF with spaces in its name gives 404. Why? · Download PNG
Interview card: How do you upload a whole folder with scp?. Use scp -r, or zip it first and send one file. One zip is faster than hundreds of small files. Folder:
Interview card: How do you upload a whole folder with scp? · Download PNG

More cards: Ravi Sir's Cards.