Skip to content

Commit

Permalink
Merge pull request #33 from vicradon/feat/nginx-config-for-other-bran…
Browse files Browse the repository at this point in the history
…ches

feat: add other nginx config
  • Loading branch information
vicradon authored Jul 19, 2024
2 parents 08006f1 + a36bdb9 commit b440af0
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions scripts/install_and_setup_nginx.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
#! /bin/bash
WEB_ROOT="/var/www/production"
CONFIG_FILE="/etc/nginx/conf.d/production.conf"
DOMAIN_OR_IP="91.229.239.238"
#!/bin/bash

sudo mkdir -p $WEB_ROOT
# Define environment configurations
declare -A environments
environments=(
["development"]="7000 deployment.api-golang.boilerplate.hng.tech"
["staging"]="8000 staging.api-golang.boilerplate.hng.tech"
["production"]="9000 api-golang.boilerplate.hng.tech"
)

# General setup
sudo apt update
sudo apt install -y nginx

# Configuring Nginx Reverse Proxy for the Production Server
content='server {
listen 80;
server_name 91.229.239.238;
location / {
proxy_pass http://127.0.0.1:8019;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}'
# Create directories and configure Nginx for each environment
for env in "${!environments[@]}"; do
# Split the value into port and domain using parameter expansion
port="${environments[$env]%% *}"
domain="${environments[$env]#* }"
web_root="~/deployments/$env"
config_file="/etc/nginx/conf.d/$env.conf"

# Create web root directory
sudo mkdir -p $web_root

echo "$content" | sudo tee $CONFIG_FILE > /dev/null
sudo chmod 664 $CONFIG_FILE
# Nginx configuration content
content="server {
listen 80;
server_name $domain;
location / {
proxy_pass http://127.0.0.1:$port;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
}
}"

# Write Nginx configuration file
echo "$content" | sudo tee $config_file > /dev/null
sudo chmod 664 $config_file
done

# Delete Default Nginx Webpage
sudo rm /etc/nginx/sites-available/default && sudo rm /etc/nginx/sites-enabled/default

# Validate the Configuration
sudo nginx -t
sudo systemctl restart nginx
echo "PRODUCTION SERVER URL: http://$DOMAIN_OR_IP"

# Output
echo "Nginx setup for the applications to reverse proxy requests to them"

0 comments on commit b440af0

Please sign in to comment.