Essential Linux Commands for Deploying Apps on AWS EC2 or Lightsail

Essential Linux Commands for Deploying Apps on AWS EC2 or Lightsail

Published on: 1/13/2025

This guide lists all the necessary Linux commands you should know to effectively deploy applications on

Server Access and Management

1. Connect to the Server via SSH

ssh -i /path/to/your-key.pem ubuntu@your-server-ip

2. Switch to Root User (if necessary)

sudo -i

3. Update and Upgrade the Server

sudo apt update && sudo apt upgrade -y

4. Check System Information

- View disk space:

df -h

- View memory usage:

free -h

- Check CPU usage:

top

File and Directory Management

1. List Files and Directories

ls -l

2. Navigate Between Directories

cd /path/to/directory

3. Create a New Directory

mkdir new-directory

4. Copy Files

cp source-file destination

5. Move or Rename Files

mv old-name new-name

6. Delete Files or Directories

- Delete a file:

rm file-name

- Delete a directory:

rm -r directory-name

File Editing

1. Edit Files with Nano

nano file-name

2. Edit Files with Vim

vim file-name
Networking Commands

1. Check Network Connectivity

ping google.com

2. Check Open Ports

sudo netstat -tuln

3. Check Firewall Status

sudo ufw status

4. Allow HTTP/HTTPS Traffic Through Firewall

sudo ufw allow 80
sudo ufw allow 443

Package Management

1. Install a Package

sudo apt install package-name

2. Remove a Package

sudo apt remove package-name

3. Search for a Package

apt search package-name
Process Management

1. List Running Processes

ps aux

2. Kill a Process by PID

sudo kill -9 PID

3. Restart a Service

sudo systemctl restart service-name

4. Check the Status of a Service

sudo systemctl status service-name
User and Permissions Management

1. Change Ownership of a File/Directory

sudo chown user:group file-or-directory

2. Change Permissions

chmod 755 file-or-directory

3. Add a New User

sudo adduser new-user

4. Grant a User Sudo Privileges

sudo usermod -aG sudo user-name

Git and Code Management

1. Clone a Repository

git clone https://github.com/user/repo.git

2. Pull the Latest Changes

git pull

3. Check Git Status

git status

Application Deployment

1. Start/Stop/Restart a Web Server

- For Nginx:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

- For Apache:

sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2

2. Install Node.js and NPM

sudo apt install nodejs npm

3. Install Dependencies for a Node.js Project

npm install

4. Run a Node.js Application

node app.js

Docker Commands (if using Docker)

1. Check Docker Installation

docker --version

2. Start a Docker Container

docker run -d -p 80:80 container-name

3. List Running Containers

docker ps

4. Stop a Running Container

docker stop container-id

5. Remove a Docker Container

docker rm container-id
Monitoring and Logs

1. View Logs of a Service

sudo journalctl -u service-name

2. View Nginx or Apache Logs

- Nginx:

sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

- Apache:

sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log

These commands cover the essential tasks you will need to perform when deploying applications on AWS