-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-setup.sh
186 lines (153 loc) · 5.12 KB
/
server-setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
echo "Welcome to the server setup and configuration script"
echo "Updating the system..."
sudo apt-get update -y
sudo apt-get upgrade -y
echo "Installing necessary packages..."
sudo apt-get install -y git unzip nginx postgresql postgresql-contrib redis-server
echo "Installing Node.js..."
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.bashrc
nvm install node
echo "Default Node.js version set to : $(node -v)"
echo "Installing PM2..."
npm install pm2 -g
sudo snap install --classic certbot
echo "Do you want to install Docker? (y/n)"
read docker
if [ "$docker" = "y" ]; then
echo "Installing Docker..."
sudo snap install docker
echo "Docker installed successfully"
else
echo "Docker installation skipped"
fi
echo "Configuring Nginx..."
sudo systemctl start nginx
sudo systemctl enable nginx
sudo ufw allow 'Nginx Full'
echo "Do you want to configure Nginx for a new website? (y/n)"
read nginx
if [ "$nginx" = "y" ]; then
echo "Configuring Nginx for a new website..."
echo "Enter your domain name (e.g. example.com):"
read domain
sudo touch /etc/nginx/sites-available/api
sudo echo "server {
server_name domain_name.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \"upgrade\";
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
}" > /etc/nginx/sites-available/api
sudo sed -i "s/domain_name.com/$domain/" /etc/nginx/sites-available/api
sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/api
sudo systemctl reload nginx
echo "Nginx configured successfully for $domain"
else
echo "Nginx configuration skipped"
fi
echo "Starting PostgreSQL..."
sudo systemctl start postgresql
sudo systemctl enable postgresql
echo "Do you want to configure PostgreSQL? (y/n)"
read postgres
if [ "$postgres" = "y" ]; then
echo "Configuring PostgreSQL..."
read -p "Enter Database Name: " dbname
read -p "Enter Database User: " dbuser
read -p "Enter Database Password: " dbpass
echo
if [ -z "$dbname" ]; then
CREATE_DB="CREATE DATABASE $dbname;"
sudo -i -u postgres psql -c "$CREATE_DB"
echo "Database with name $dbname created successfully"
fi
if [ -z "$dbuser" ]; then
CREATE_USER="CREATE USER $dbuser WITH ENCRYPTED PASSWORD '$dbpass';"
GRANT_PRIVILEGES="GRANT ALL PRIVILEGES ON DATABASE $dbname TO $dbuser;"
sudo -i -u postgres psql -c "$CREATE_USER"
sudo -i -u postgres psql -c "$GRANT_PRIVILEGES"
echo "User with name $dbuser created successfully"
fi
sudo systemctl restart postgresql
echo "PostgreSQL configured successfully"
else
echo "PostgreSQL configuration skipped"
fi
echo "Starting Redis..."
sudo systemctl start redis
sudo systemctl enable redis
echo "Redis started successfully"
echo "Do you want to configure Redis password? (y/n)"
read redis_pass
if [ "$redis_pass" = "y" ]; then
echo "Enter Redis password:"
read password
if [ -z "$password" ]; then
sudo sed -i "s/# requirepass foobared/requirepass $password/" /etc/redis/redis.conf
sudo systemctl restart redis
echo "Redis configured successfully with password"
fi
else
echo "Redis configuration skipped"
fi
echo "Do you want to configure Certbot? (y/n)"
read certbot
if [ "$certbot" = "y" ]; then
echo "Configuring Certbot..."
echo "Enter your email address:"
read email
echo "Enter your domain name (e.g. example.com):"
read domain
sudo certbot --nginx -d $domain -m $email --agree-tos
echo "Certbot configured successfully"
else
echo "Certbot configuration skipped"
fi
echo "Do you want to install auto-deployment server? (y/n)"
read auto_deploy
mkdir -p /home/ubuntu/api
if [ "$auto_deploy" = "y" ]; then
echo "Installing auto-deployment server..."
mkdir -p /home/ubuntu/deploy
cd /home/ubuntu/deploy
git clone https://github.com/mkyai/auto-deploy.git .
npm install
touch .env
chmod +x pre.sh
chmod +x slack.sh
echo "Enter logtail key"
read logtail
if [ -n "$logtail" ]; then
echo -e "\LOGTAIL_KEY=\"$logtail\"" | sudo tee -a .env
fi
echo "Enter deployment secret"
read deployment_secret
if [ -n "$deployment_secret" ]; then
echo -e "\DEPLOYMENT_SECRET=\"$deployment_secret\"" | sudo tee -a .env
fi
echo "Enter slack chanel for updates"
read slack_channel
if [ -n "$slack_channel" ]; then
echo -e "\SLACK_CHANNEL=\"$slack_channel\"" | sudo tee -a .env
fi
echo "Enter slack token"
read slack_token
if [ -n "$slack_token" ]; then
echo -e "\SLACK_TOKEN=\"$slack_token\"" | sudo tee -a .env
fi
pm2 start index.js
pm2 save
else
echo "Auto-deployment server installation skipped"
fi
echo "Setup and configuration completed successfully"
echo "Exiting..."
exit 0