Tools: Commonly used Linux commands by DevOps engineers - Version 2

Tools: Commonly used Linux commands by DevOps engineers - Version 2

Source: Dev.to

Host Your Static Website and Node.js API on EC2 Using Nginx ## Table of Contents ## 1️⃣ File Permissions and SSH Keys ## 2️⃣ SSH into EC2 ## 3️⃣ Update Packages (Amazon Linux) ## 4️⃣ Install Nginx ## 5️⃣ Start & Enable Nginx ## 6️⃣ Test with curl ## Basic test ## Check HTTP headers ## 7️⃣ Copy Files from WSL to EC2 ## 8️⃣ Move Files to Nginx Root ## 9️⃣ Install Node.js Using NVM ## 10️⃣ Install PM2 ## 11️⃣ Start Node.js API with PM2 ## 12️⃣ Configure Nginx Reverse Proxy ## 13️⃣ Next Steps / Tips ## ✅ Key Categories Learn how to deploy a static website and a Node.js backend API on an AWS EC2 instance using Nginx and PM2. This step-by-step guide covers everything from SSH access to process management, making your setup production-ready. File Permissions and SSH Keys Copy Files from WSL to EC2 Move Files to Nginx Root Install Node.js Using NVM Start Node.js API with PM2 Configure Nginx Reverse Proxy What it does: Sets permissions so only the owner can read the private key. Purpose: SSH requires strict key permissions for security. Tip: Without this, SSH will refuse to use your .pem key. ssh → connects securely to your EC2 instance. -i ~/DevOps.pem → specifies the private key for authentication. Purpose: Gain access to manage your server remotely start → starts Nginx immediately. enable → ensures Nginx runs after reboot. Purpose: Keep your web server running persistently. scp → securely copies files over SSH -r → recursive copy (entire folder) Purpose: Deploy your local project to the EC2 instance mv → moves files/folders chmod -R 755 → owner can write, everyone else can read/execute Purpose: Make static site accessible via Nginx curl -o- URL | bash → downloads and installs NVM source ~/.bashrc → loads NVM into current shell nvm install --lts → installs latest Node.js LTS nvm alias default lts/* → makes LTS version the default in all new shells Purpose: Safely manage Node.js versions; ensures your backend always runs on the latest stable LTS. npm → Node.js package manager Purpose: Install PM2, a process manager for Node.js, to keep your API running in the background. pm2 start → runs your Node.js API under PM2 supervision --name → gives the process a friendly name for easy management Purpose: Keep your API running continuously; auto-restarts if it crashes. ⚠️ Note: server.js must exist at ~/api/server.js before running this command. PM2 cannot create the file for you. tee → writes the config file nginx -t → tests for syntax errors systemctl reload nginx → reloads configuration without downtime Purpose: Serve your static site and forward /api requests to your Node.js backend Use pm2 save and pm2 startup to ensure Node.js API restarts after EC2 reboot. Add HTTPS using Certbot + Nginx for a secure website. For frequent deployments, consider rsync -avz instead of scp to sync only changed files. Monitor logs with pm2 logs my-api to troubleshoot your backend. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK: chmod 400 ~/DevOps.pem Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: chmod 400 ~/DevOps.pem CODE_BLOCK: chmod 400 ~/DevOps.pem COMMAND_BLOCK: ssh -i ~/DevOps.pem ec2-user@YOUR_PUBLIC_IP Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: ssh -i ~/DevOps.pem ec2-user@YOUR_PUBLIC_IP COMMAND_BLOCK: ssh -i ~/DevOps.pem ec2-user@YOUR_PUBLIC_IP COMMAND_BLOCK: sudo yum update -y Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo yum update -y COMMAND_BLOCK: sudo yum update -y COMMAND_BLOCK: sudo yum install nginx -y Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo yum install nginx -y COMMAND_BLOCK: sudo yum install nginx -y COMMAND_BLOCK: sudo systemctl start nginx sudo systemctl enable nginx Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo systemctl start nginx sudo systemctl enable nginx COMMAND_BLOCK: sudo systemctl start nginx sudo systemctl enable nginx COMMAND_BLOCK: curl http://localhost Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: curl http://localhost COMMAND_BLOCK: curl http://localhost COMMAND_BLOCK: curl -I http://localhost Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: curl -I http://localhost COMMAND_BLOCK: curl -I http://localhost COMMAND_BLOCK: scp -i ~/DevOps.pem -r ~/DevOps ec2-user@YOUR_PUBLIC_IP:/home/ec2-user/ Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: scp -i ~/DevOps.pem -r ~/DevOps ec2-user@YOUR_PUBLIC_IP:/home/ec2-user/ COMMAND_BLOCK: scp -i ~/DevOps.pem -r ~/DevOps ec2-user@YOUR_PUBLIC_IP:/home/ec2-user/ COMMAND_BLOCK: sudo mv ~/DevOps /usr/share/nginx/html/ sudo chmod -R 755 /usr/share/nginx/html/DevOps Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo mv ~/DevOps /usr/share/nginx/html/ sudo chmod -R 755 /usr/share/nginx/html/DevOps COMMAND_BLOCK: sudo mv ~/DevOps /usr/share/nginx/html/ sudo chmod -R 755 /usr/share/nginx/html/DevOps COMMAND_BLOCK: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bash source ~/.bashrc nvm install --lts nvm alias default lts/* Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bash source ~/.bashrc nvm install --lts nvm alias default lts/* COMMAND_BLOCK: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bash source ~/.bashrc nvm install --lts nvm alias default lts/* COMMAND_BLOCK: npm install -g pm2 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: npm install -g pm2 COMMAND_BLOCK: npm install -g pm2 CODE_BLOCK: pm2 start ~/api/server.js --name my-api Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: pm2 start ~/api/server.js --name my-api CODE_BLOCK: pm2 start ~/api/server.js --name my-api COMMAND_BLOCK: sudo tee /etc/nginx/conf.d/reverse.conf > /dev/null <<'EOF' server { listen 80; location / { root /usr/share/nginx/html/DevOps; index index.html; } location /api/ { proxy_pass http://localhost:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } EOF sudo nginx -t sudo systemctl reload nginx Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: sudo tee /etc/nginx/conf.d/reverse.conf > /dev/null <<'EOF' server { listen 80; location / { root /usr/share/nginx/html/DevOps; index index.html; } location /api/ { proxy_pass http://localhost:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } EOF sudo nginx -t sudo systemctl reload nginx COMMAND_BLOCK: sudo tee /etc/nginx/conf.d/reverse.conf > /dev/null <<'EOF' server { listen 80; location / { root /usr/share/nginx/html/DevOps; index index.html; } location /api/ { proxy_pass http://localhost:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } EOF sudo nginx -t sudo systemctl reload nginx - File Permissions and SSH Keys - SSH into EC2 - Update Packages - Install Nginx - Start & Enable Nginx - Test with curl - Copy Files from WSL to EC2 - Move Files to Nginx Root - Install Node.js Using NVM - Install PM2 - Start Node.js API with PM2 - Configure Nginx Reverse Proxy - Next Steps / Tips - What it does: Sets permissions so only the owner can read the private key. - Purpose: SSH requires strict key permissions for security. - Tip: Without this, SSH will refuse to use your .pem key. - ssh → connects securely to your EC2 instance. - -i ~/DevOps.pem → specifies the private key for authentication. - Purpose: Gain access to manage your server remotely - Purpose: Keeps the system secure and prepares it for new software. - Purpose: Installs Nginx web server to serve static content and act as a reverse proxy. - start → starts Nginx immediately. - enable → ensures Nginx runs after reboot. - Purpose: Keep your web server running persistently. - Checks if Nginx is serving content. - -I → fetch only HTTP headers to verify status code (200 OK). - scp → securely copies files over SSH - -r → recursive copy (entire folder) - Purpose: Deploy your local project to the EC2 instance - mv → moves files/folders - chmod -R 755 → owner can write, everyone else can read/execute - Purpose: Make static site accessible via Nginx - curl -o- URL | bash → downloads and installs NVM - source ~/.bashrc → loads NVM into current shell - nvm install --lts → installs latest Node.js LTS - nvm alias default lts/* → makes LTS version the default in all new shells - Purpose: Safely manage Node.js versions; ensures your backend always runs on the latest stable LTS. - npm → Node.js package manager - -g → global install - Purpose: Install PM2, a process manager for Node.js, to keep your API running in the background. - pm2 start → runs your Node.js API under PM2 supervision - --name → gives the process a friendly name for easy management - Purpose: Keep your API running continuously; auto-restarts if it crashes. - tee → writes the config file - nginx -t → tests for syntax errors - systemctl reload nginx → reloads configuration without downtime - Purpose: Serve your static site and forward /api requests to your Node.js backend - Use pm2 save and pm2 startup to ensure Node.js API restarts after EC2 reboot. - Add HTTPS using Certbot + Nginx for a secure website. - For frequent deployments, consider rsync -avz instead of scp to sync only changed files. - Monitor logs with pm2 logs my-api to troubleshoot your backend.