๐ง 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 ํ์ดํ๋ผ์ธ์ด ํฌ๊ฒ ๊ทธ๋ฆฌ๊ณ ๋ช ํํ๊ฒ ์คํจํ๋๋ก ํ์ฌ, ๋ฌธ์ ๋ฅผ ์กฐ๊ธฐ์ ํฌ์ฐฉํ ์ ์๊ฒ ํฉ๋๋ค.