107 lines
3.1 KiB
Bash
107 lines
3.1 KiB
Bash
#!/bin/bash
|
|
|
|
# Define required environment variables
|
|
declare -A required_vars=(
|
|
["NGINX_SERVER_NAME"]="Nginx server domain"
|
|
["NGINX_SSL_CERT"]="SSL certificate path"
|
|
["NGINX_SSL_KEY"]="SSL key path"
|
|
["NGINX_FRONTEND_ROOT"]="Frontend build file path"
|
|
["BACKEND_PORT"]="Backend service port"
|
|
)
|
|
|
|
# Validate environment variables
|
|
validate_env_vars() {
|
|
local missing_vars=()
|
|
local env_file=$1
|
|
|
|
echo "Verifying Nginx environment variable configuration..."
|
|
|
|
# Load environment variables
|
|
source "$env_file"
|
|
|
|
# Check required variables
|
|
for var in "${!required_vars[@]}"; do
|
|
if [ -z "${!var}" ]; then
|
|
missing_vars+=("$var (${required_vars[$var]})")
|
|
fi
|
|
done
|
|
|
|
# If there are missing variables, display an error message and exit
|
|
if [ ${#missing_vars[@]} -ne 0 ]; then
|
|
echo "Error: The following required Nginx variables are not set:"
|
|
printf '%s\n' "${missing_vars[@]}" | sed 's/^/ - /'
|
|
echo "Please set these variables in $env_file and try again."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Nginx production environment variables verified successfully!"
|
|
}
|
|
|
|
# Check parameters
|
|
if [ -z "$1" ]; then
|
|
echo "Usage: $0 <env_file_path>"
|
|
exit 1
|
|
fi
|
|
|
|
ENV_FILE=$1
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
echo "Nginx path: $SCRIPT_DIR"
|
|
|
|
# Check if the environment variable file exists
|
|
if [ ! -f "$ENV_FILE" ]; then
|
|
echo "Error: Environment file $ENV_FILE not found"
|
|
exit 1
|
|
fi
|
|
|
|
# Validate environment variables
|
|
validate_env_vars "$ENV_FILE"
|
|
|
|
# Read environment variables
|
|
source "$ENV_FILE"
|
|
|
|
# Configure Nginx
|
|
configure_nginx() {
|
|
echo "Configuring Nginx..."
|
|
|
|
NGINX_TEMPLATE="$SCRIPT_DIR/default"
|
|
echo "reading $NGINX_TEMPLATE ..."
|
|
TEMP_NGINX=$(mktemp)
|
|
|
|
# Read the template and replace variables
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
# Replace server_name
|
|
if [[ $line =~ ^[[:space:]]*server_name[[:space:]]+ ]]; then
|
|
echo " server_name $NGINX_SERVER_NAME www.$NGINX_SERVER_NAME;"
|
|
# Replace SSL certificate path
|
|
elif [[ $line =~ ^[[:space:]]*ssl_certificate[[:space:]]+ ]]; then
|
|
echo " ssl_certificate $NGINX_SSL_CERT;"
|
|
# Replace SSL key path
|
|
elif [[ $line =~ ^[[:space:]]*ssl_certificate_key[[:space:]]+ ]]; then
|
|
echo " ssl_certificate_key $NGINX_SSL_KEY;"
|
|
# Exactly match the frontend build path setting line
|
|
elif [[ $line =~ ^[[:space:]]*set[[:space:]]+\$frontend_build_root[[:space:]]+ ]]; then
|
|
echo " set \$frontend_build_root $NGINX_FRONTEND_ROOT;"
|
|
# Simple port number replacement
|
|
elif [[ $line =~ localhost:3001 ]]; then
|
|
echo "${line/localhost:3001/localhost:$BACKEND_PORT}"
|
|
else
|
|
echo "$line"
|
|
fi
|
|
done < "$NGINX_TEMPLATE" > "$TEMP_NGINX"
|
|
|
|
# Copy the configuration file to the target location
|
|
cp "$TEMP_NGINX" /etc/nginx/sites-available/
|
|
# cp "$TEMP_NGINX" default_temp
|
|
rm "$TEMP_NGINX"
|
|
}
|
|
|
|
# Execute configuration
|
|
configure_nginx
|
|
|
|
# Test Nginx configuration
|
|
cp nginx.conf /etc/nginx
|
|
nginx -t
|
|
|
|
/etc/init.d/nginx restart
|
|
|
|
echo "Nginx configuration completed." |