Skip to content

Commit 52d870f

Browse files
authored
Merge pull request #2 from davidshare/readme
Update the documentation in the README.md file
2 parents 1d2b4b7 + 2139516 commit 52d870f

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
*.pem

PULL_REQUEST_TEMPLATE.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#### What does this PR do?
2+
3+
#### Description of Task to be completed?
4+
5+
#### How should this be manually tested?
6+
7+
#### Any background context you want to provide?
8+
9+
#### What are the relevant pivotal tracker stories?
10+
11+
#### Screenshots (if appropriate)
12+
13+
#### Questions:

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
# DevopsDemo
22
Simple scripts to deploy a react project to an AWS instance
3+
4+
## How to deploy
5+
* Create an AWS free tier account
6+
* Create an AWS instance using the free tier resources and download your keypair file
7+
* SSH into the AWS instance you created and clone this repository in the root directory
8+
* Copy your keypair file into the root directory of the repository
9+
* Get a domain name (you can get a free one from www.freenom.com)
10+
* Using the .env.sample file, specify your environment variables in a .env file - make sure to include your domain name and react app repository
11+
* Reserve an elastic ip address on AWS
12+
* Configure route 53 using your domain name and the IP address you reserved
13+
* On your domain name accout, configure your DNS setting with the dns addresses provided by route 53
14+
* Switch to the root directory of the project and run the following command: "bash main.sh"
15+
* On your browser, type your domain name to access your app
16+

main.sh

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/env bash
2+
# create environment variables
3+
createEnv(){
4+
sudo echo "
5+
export SITE=davidessien.com
6+
export GIT_REPO=https://github.com/andela/selene-ah-frontend.git
7+
export SITES_AVAILABLE=/etc/nginx/sites-available
8+
export SITES_ENABLED=/etc/nginx/sites-enabled
9+
export SITES_ENABLED_CONFIG=/etc/nginx/sites-enabled/selene
10+
export SITES_AVAILABLE_CONFIG=/etc/nginx/sites-available/selene
11+
export REPOSITORY_FOLDER=selene-ah-frontend
12+
13+
export GREEN='\033[0;32m'
14+
export RED='\033[0;31m'
15+
" > .env
16+
17+
# add enviroment variables to OS
18+
source .env
19+
}
20+
21+
# Ouput messages to terminal
22+
output(){
23+
echo -e "$2 ################################ $1 ################################## $(tput sgr0)"
24+
}
25+
26+
# Install node.js
27+
installNode(){
28+
output "installing node.js" $GREEN
29+
sudo apt-get update
30+
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
31+
sudo bash nodesource_setup.sh
32+
sudo apt-get install -y nodejs
33+
output "Node.js installed successfully" $GREEN
34+
}
35+
36+
# Clone the repository
37+
cloneRepository(){
38+
output "Checking if repository exists..." $GREEN
39+
if [ ! -d selene-ah-frontend ]
40+
then
41+
output "Cloning repository..." $GREEN
42+
git clone -b aws-deploy https://github.com/andela/selene-ah-frontend.git
43+
else
44+
output "Repository already exists..." $RED
45+
output "Removing repository..." $GREEN
46+
sudo rm -r selene-ah-frontend
47+
output "Cloning repository..." $GREEN
48+
git clone -b aws-deploy https://github.com/andela/selene-ah-frontend.git
49+
fi
50+
output "Repository cloned successfully" $GREEN
51+
}
52+
53+
# Setup the project
54+
setupProject(){
55+
output "installing node modules" $GREEN
56+
cd selene-ah-frontend
57+
sudo npm install -y
58+
sudo npm audit fix --force
59+
sudo npm run build
60+
output "successfully installed node modules" $GREEN
61+
}
62+
63+
# Setup nginx
64+
setupNginx(){
65+
output "installing nginx" $GREEN
66+
# Install nginx
67+
sudo apt-get install nginx -y
68+
69+
output "setting up reverse proxy" $GREEN
70+
# Setup reverse proxy with nginx
71+
nginxScript="server {
72+
listen 80;
73+
server_name $SITE "www.$SITE";
74+
75+
location / {
76+
proxy_pass http://127.0.0.1:8080;
77+
}
78+
}"
79+
80+
# Remove the default nginx proxy script
81+
if [ -f $SITES_AVAILABLE/default ]; then
82+
sudo rm $SITES_AVAILABLE/default
83+
fi
84+
85+
if [ -f $SITES_ENABLED/default ]; then
86+
sudo rm $SITES_ENABLED/default
87+
fi
88+
89+
# Create an nginx reverse proxy script
90+
sudo echo ${nginxScript} >> ${SITES_AVAILABLE_CONFIG}
91+
92+
# Create a symlink for the sites enabled and the sites available script
93+
sudo ln -s $SITES_AVAILABLE_CONFIG $SITES_ENABLED_CONFIG
94+
95+
sudo service nginx restart
96+
97+
output "successfully setup nginx" $GREEN
98+
}
99+
100+
setupSSL(){
101+
output "installing and setting up SSL" $GREEN
102+
site=davidessien.com
103+
104+
sudo apt-get update -y
105+
106+
# Get and install the SSL certificate
107+
sudo apt-get install software-properties-common -y
108+
sudo add-apt-repository ppa:certbot/certbot -y
109+
sudo apt-get update -y
110+
sudo apt-get install python-certbot-nginx -y
111+
112+
# Configure the ngix proxy file to use the SSL certificate
113+
sudo certbot --nginx -n --agree-tos --email $email --redirect --expand -d $site -d "www.$site"
114+
115+
output "successfuly setup SSL" $GREEN
116+
}
117+
118+
# Create a service to run the app in the backgroud using systemctl
119+
createAppService(){
120+
output "Creating a service for the app..." $GREEN
121+
sudo bash -c "cat > /etc/systemd/system/selene.service <<EOF
122+
[Unit]
123+
Description=Selene ah Service - Service to start the selene-ah frontend app
124+
After=network.target
125+
126+
[Service]
127+
ExecStart=/usr/bin/node /home/ubuntu/selene-ah-frontend/server.js
128+
Restart=on-failure
129+
Type=simple
130+
User=ubuntu
131+
132+
[Install]
133+
WantedBy=multi-user.target"
134+
}
135+
136+
# start the app using the service that has been created
137+
startAppService(){
138+
output "Starting the app service..." $GREEN
139+
sudo systemctl daemon-reload
140+
sudo systemctl start selene.service
141+
sudo systemctl enable selene.service
142+
}
143+
144+
145+
# Function to deploy the project
146+
main(){
147+
createEnv
148+
setupNginx
149+
installNode
150+
cloneRepository
151+
setupProject
152+
setupSSL
153+
createAppService
154+
startAppService
155+
output "Project deployed" $GREEN
156+
}
157+
158+
main

0 commit comments

Comments
 (0)