단계별 EC2 시작 템플릿
로컬 컴퓨터에서 개발하는 것은 더 이상 번거롭지 않으며 컴퓨터에 코드를 배포하는 것도 더 이상 문제가 되지 않지만 프로세스 자동화는 일부 사람들에게 위협적일 수 있습니다.
이 기사에서는 EC2 시작 템플릿을 생성하여 새 서버 인스턴스 할당을 자동화하고, 코드를 풀고, 적절한 구성을 만들고, 애플리케이션을 시작하는 데 필요한 구성을 살펴보겠습니다.
요구 사항
먼저 EC2 대시보드로 이동하여 측면 탐색 모음에서 시작 템플릿을 클릭합니다.
![](https://s1.md5.ltd/image/01eb0053e9641b260ed76f1f7c0c25ed.png)
시작 템플릿 생성
사용자 데이터는 일부 패키지 설치, 코드 풀링, 빌드 및 시작과 같이 인스턴스가 초기화된 후 실행할 스크립트를 보유할 수 있습니다.
![](https://s1.md5.ltd/image/71e2d8e168b27717621e36ba6e175401.png)
다음 코드를 추가하세요. 각 줄 앞에 무엇을 하는지 설명하는 주석을 추가하겠습니다. ##으로 시작하는 줄은 주석입니다.
## 1. This line is important for AWS EC2 to understand the context of this file
#!/bin/bash -ex
## 2. Log all incidents in a user-data.log file
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
## 3. Install nodejs
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt-get -y install nodejs
apt-get -y install build-essential
## 4. Install nginx for network routing
apt-get update
apt-get -y install nginx
ufw allow 'Nginx Full'
## 5. Remove nginx default configuration
cd /etc/nginx/sites-available
unlink default
## 6. Add your custom nginx configuration
tee -a myapp.com <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name myapp.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
}
}
EOF
## 7. Remove nginx other configuration file
cd /etc/nginx/sites-enabled
unlink default
## 8. Link both configurations and restart nginx
ln -s /etc/nginx/sites-available/myapp.com /etc/nginx/sites-enabled/myapp.com
systemctl restart nginx
cd /home/ubuntu
## 8. Install pm2 for forever running an application
npm install pm2 -g
mkdir app
cd app
## 8. Clone your repository and you may use the password in the url to skip password check step
git clone https://<username>:<password>@bitbucket.org/<project-name>/<repo-name>.git
cd repo-name
## 9. Add .env file and the necessary environment variables
tee -a .env <<EOF
NODE_ENV=development
PORT=3000
EOF
npm install
npm run build
## 10. Use pm2 to start the application
pm2 start npm --name "repo-name" -- run start
이제 서버가 초기화 및 구성되었으며 코드가 최신 변경 사항으로 풀링되어 실행됩니다.
이동 EC2 대시보드 및 시작 및 인스턴스를 확인하려면 이번에는 시작 템플릿에서
![](https://s1.md5.ltd/image/ba4200a4a8174f959e12561433e18607.png)
당신은 2분에서 5분까지 세어야 하고 비올라 당신은 당신 자신의 새로운 인스턴스를 가지고 있고, 퍼블릭 IP 또는 브라우저의 인스턴스 정보에 제공된 퍼블릭 dns를 사용합니다. 이제 애플리케이션의 응답이 표시되어야 합니다.
Reference
이 문제에 관하여(단계별 EC2 시작 템플릿), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/omardiaa48/ec2-launch-template-step-by-step-1kac텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)