π§ EC2μ λ°°ν¬νλ©΄μ λͺ¨λ DevOps μ΄λ³΄μκ° λ°°μ°λ Linux λͺ λ Ήμ΄
Source: Dev.to
μ£μ‘ν©λλ€λ§, νμ¬ μ λ μΈλΆ μΉμ¬μ΄νΈμ λ΄μ©μ μ§μ κ°μ Έμ¬ μ μμ΅λλ€. λ²μμ΄ νμν λ³Έλ¬Έ ν μ€νΈλ₯Ό μ¬κΈ° μ±ν μ λΆμ¬ μ£Όμλ©΄, μμ²νμ λλ‘ νκ΅μ΄λ‘ λ²μν΄ λλ¦¬κ² μ΅λλ€.
π SSH & Remote Access
ssh -i DevOps.pem ec2-user@ec2-x-x-x-x.compute-1.amazonaws.com
Purpose: EC2 μΈμ€ν΄μ€μ μμ νκ² μ°κ²°ν©λλ€.
sshβ Secure Shell-i DevOps.pemβ μΈμ¦μ μ¬μ©ν κ°μΈ ν€ec2-user@hostβ λ‘κ·ΈμΈ μ¬μ©μ + EC2 νΈμ€νΈλͺ
Running a command without interactive login
ssh user@host "command"
Purpose: μ격 EC2 μΈμ€ν΄μ€μμ λͺ λ Ήμ μ§μ μ€νν©λλ€ (CI/CD νμ΄νλΌμΈμ μ μ©).
Example
ssh ec2-user@EC2_HOST "whoami && hostname"
Managing known hosts for nonβinteractive CI/CD
ssh-keyscan -H EC2_HOST >> ~/.ssh/known_hosts
- βAre you sure you want to continue connecting?β ν둬ννΈλ₯Ό λ°©μ§ν©λλ€.
- μλν μ€ν¬λ¦½νΈμ νμν©λλ€.
λ³΄λ€ μμ ν λ³ν (νμμμμ μΆκ°νκ³ νμ΄νλΌμΈμ΄ μ€ν¨νμ§ μλλ‘ ν¨):
ssh-keyscan -T 10 -H EC2_HOST >> ~/.ssh/known_hosts || true
π νμΌ μμ€ν νμ λ° κ²μ¬
pwd # Print current directory
ls # List files and directories
ls -l # Long format (permissions, owner, size)
ls -a # Include hidden files
cd # Change directory
μμ
cd DevOpsWeb
π νμΌ λ° λλ ν°λ¦¬ κΆν (λ§€μ° μ€μ)
κΆν λ³κ²½
chmod 755 directory
chmod 644 file
7= μ½κΈ°β―+β―μ°κΈ°β―+β―μ€ν (μμ μ)5= μ½κΈ°β―+β―μ€ν (κ·Έλ£Ή/κΈ°ν)4= μ½κΈ° μ μ©
μΌλ°μ μΈ μ¬μ©
- Nginxκ° νμΌμ μ½μ μ μλλ‘ νμ©
- 403β―Forbidden μ€λ₯ λ°©μ§
μμ κΆ λ³κ²½
sudo chown -R ec2-user:nginx /home/ec2-user/DevOpsWeb
- μμ μ β
ec2-user - κ·Έλ£Ή β
nginx -Rβ μ¬κ·μ
λ£¨νΈ κΆνμΌλ‘ λͺ λ Ή μ€ν
sudoλ κ΄λ¦¬ κΆνμΌλ‘ λͺ
λ Ήμ μ€ννλ©° λ€μμ νμν©λλ€:
- ν¨ν€μ§ μ€μΉ
- μμ€ν κ΅¬μ± νΈμ§
- μλΉμ€ μ¬μμ
π Nginx (μΉ μλ²)
sudo yum install nginx -y # Install
sudo systemctl start nginx # Start service
sudo systemctl enable nginx # Enable on boot
sudo systemctl status nginx # Check status
sudo nginx -t # Test configuration (run before reload)
sudo systemctl reload nginx # Reload config without downtime
π λ€νΈμνΉ λ° λλ²κΉ
curl http://localhost # Test local web server response
curl http://PUBLIC_IP # Test public access from the instance
lsof -i :80 # See what process is using port 80
curl http://localhostκ° μ μμ μΌλ‘ λμνμ§λ§ λΈλΌμ°μ μμ μ¬μ΄νΈμ μ κ·Όν μ μλ κ²½μ°, λ€μμ νμΈνμΈμ:
- 보μ κ·Έλ£Ή κ·μΉ
- μΈμ€ν΄μ€ λ°©νλ²½ μ€μ
π¦ ν¨ν€μ§ κ΄λ¦¬ (Amazon Linux)
sudo yum install -y # ν¨ν€μ§λ₯Ό λΉλνμμΌλ‘ μ€μΉ
sudo yum remove -y # ν¨ν€μ§ μ κ±°
μμ
sudo yum install httpd -y
sudo yum install docker -y
sudo yum remove httpd -y # ν¬νΈ μΆ©λμ λ°©μ§νκΈ° μν΄ Apache μ κ±°
π³ Docker κΈ°λ³Έ (μ§κΈκΉμ§)
docker run hello-world # Verify Docker installation
docker build -t devops-website . # Build image from Dockerfile
docker ps -a # List all containers
docker exec -it <container> <command> # Run a command inside a running container
μμ
docker exec -it devopsweb ls /usr/share/nginx/html
ec2-userκ° sudo μμ΄ Dockerλ₯Ό μ€ννλλ‘ νμ©ν©λλ€:
sudo usermod -aG docker ec2-user
# Log out and back in for the change to take effect
8οΈβ£ Rsync (CI/CD Deployment)
rsync -avz --delete source/ user@EC2_HOST:/path/to/dest/
-aβ μμΉ΄μ΄λΈ λͺ¨λ (κΆν, νμμ€ν¬ν λ±μ 보쑴)-vβ μμΈν μΆλ ₯-zβ μμΆ--deleteβ λμμ μ‘΄μ¬νμ§λ§ μμ€μ λ μ΄μ μλ νμΌμ μμ
μνμ§ μλ νμΌ μ μΈ
--exclude='.git*'
--exclude='.github/'
λμμ κΆν κ°μ μ μ©
--chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r
μ΄ μ΅μ λ€μ λ°°ν¬ μ€ λ°μν exit codeβ―23 λ° κΆν κ΄λ ¨ λ¬Έμ λ₯Ό ν΄κ²°νλ λ° λμμ΄ λμμ΅λλ€.
π§ͺ GitHub Actions / CI λͺ λ Ήμ΄
set -e # Exit immediately if a command fails
set -eux # -e: exit on error, -u: treat unset variables as error, -x: print commands (debug)
mkdir -p ~/.ssh
echo "$SECRET" > ~/.ssh/id_rsa # Write SSH private key from GitHub Secrets
μ΄λ¬ν μ€μ μ CI νμ΄νλΌμΈμ΄ ν¬κ² κ·Έλ¦¬κ³ λͺ ννκ² μ€ν¨νλλ‘ νμ¬, λ¬Έμ λ₯Ό μ‘°κΈ°μ ν¬μ°©ν μ μκ² ν©λλ€.