-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from vicradon/feat/nginx-config-for-other-bran…
…ches feat: add other nginx config
- Loading branch information
Showing
1 changed file
with
38 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |