Programming/클라우드 컴퓨팅 & AWS

[COPY] AWS EC2 스크립트

MOONCO 2023. 3. 1. 16:11

nginx 설치 & 서비스 등록 & 시작

#!/bin/bash
yum update -y
amazon-linux-extras install nginx1 -y
systemctl enable nginx
systemctl start nginx

# check install
rpm -qa | grep nginx

 

Nodejs 설치 & 확인

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 16
node -e "console.log('Running Node.js ' + process.version)"

 

Java, Maven 설치 & 확인

# 기본 설치
yum install java java-devel maven -y
mvn --version

# 최신 버전 설치 ( java + javac + maven )
wget https://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo
yum install -y apache-maven

 

MySQL 설치 & 확인

# amazon-linux
yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
amazon-linux-extras install epel -y
yum install mysql-community-server -y

# default
yum install mysql -y
mysql --version

 

Git 설치 & 확인

yum install git -y

 

프로젝트 클론

git clone 프로젝트깃주소
cd 프로젝트

 

React 패키지 설치

npm install
npm run build

 

React 서버 백그라운드 실행 ( nohup ) & 확인

nohup npm start &
# 포트 확인
netstat -ntlp
# 서버 요청 확인
curl -v localhost:3000

 

nginx 설정 및 실행

# 설정파일 삽입
cd default.conf /etc/nginx/conf.d/

# 빌드파일 삽입 ( 질문나오면 yes 입력 )
cp -r build/* /usr/share/nginx/html/

# nginx 등록 & 실행
systemctl enable nginx
systemctl start nginx

# nginx 실행 확인
systemctl status nginx

# nginx로 React서버 접속 확인
curl -v localhost:80

 

Spring RDS 연결 설정

# application.properties
spring.datasource.url=jdbc:mysql://aws-rds-endpoint:3306/DB이름?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true

spring.datasource.username=DB계정
spring.datasource.password=DB암호

 

RDS 백엔드 연결 준비

# DB접속
mysql -u root -p -h aws-rds-endpoint

# 스키마 생성 및 확인 - backend와 매핑
create database employee;
show databases;

# DB접근 권한 설정
GRANT ALL PRIVILEGES ON employee.* TO DB계정@'%';
flush privileges;

 

Spring 실행하기

# clean build
mvn clean

# build project
mvn package

cd target
nohup java -jar ~~~~~~0.0.1-SNAPSHOT.jar &
# 8080
netstat -ntlp 
# loopback 접속
curl -v localhost:8080/api/v1/healthz
# private 접속
http://my-private-ec2:8080/api/v1/healthz
# api 접속
http://도메인주소:8080/api/v1/healthz

 

엣지 네트워크 사용하기 ( CloudFront )

버지니아 북부 인증서 생성
- 도메인
- *.도메인

기존 로드밸런서 대상그룹 수정
- 80 포트 기본대상 그룹 80
- 호스트 도메인 접속시 그룹 80

클라우드 프론트 생성
- 원본 : 로드밸런서 선택 ( 맨처음 데이터 받을 위치 )
- HTTP 요청 HTTPS로 리다이렉트 설정
- cache 설정 ( Optimize )

Rout53 라우팅 수정
- www 도메인을, 기존 로드밸런서에서, CloudFront로 수정

도메인 접속
- 개발자도구 > 네트워크 > 새로 고침 > 파일 선택 > HTTP헤더 > x-cache > Hit ( CloudFront를 사용해서 들어왔다 )

 

S3 Bucket 정책

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicRead",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": "arn:aws:s3:::BUCKET_NAME/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIP": "MY_PUBLIC_IP"
                }
            }
        }
    ]
}


// IpAddress : 해당 IP에서만 접속 허용
// NotIpAddress : 해당 IP 접속 차단

 

정책 작성 주의 사항

마지막 정책뒤에 컴마 ',' 가 있으면 구문 오류가 발생한다.
반응형