-
Notifications
You must be signed in to change notification settings - Fork 50
4. Environment
ekedonald edited this page Jul 19, 2024
·
12 revisions
This document outlines the environment setup required to run the HNG Boilerplate Golang Web application in both development (test) and production environments. It covers the necessary software, hardware, and configuration settings, including setting up Nginx as a reverse proxy.
- Version: 1.19 or later
- Installation: Follow the official Go installation guide for your operating system.
- Version: 14 or later
-
Installation: You can install PostgreSQL using the package manager of your choice:
-
Homebrew (MacOS):
brew install postgresql
-
APT (Ubuntu):
sudo apt-get update sudo apt-get install postgresql postgresql-contrib
-
YUM (CentOS):
sudo yum install postgresql-server postgresql-contrib
-
Homebrew (MacOS):
- Purpose: To run PostgreSQL instances in containers, simplifying setup and teardown.
- Installation: Follow the official Docker installation guide.
- Version: Latest version recommended.
- Installation: Follow the official Git installation guide.
- Version: Latest stable version.
-
Installation: You can install Nginx using the package manager of your choice:
-
APT (Ubuntu):
sudo apt-get update sudo apt-get install nginx
-
YUM (CentOS):
sudo yum install nginx
-
APT (Ubuntu):
- Purpose: To manage and monitor the Go application processes.
-
Installation: Follow the official PM2 installation guide:
npm install pm2@latest -g
PostgreSQL can be set up locally or using Docker for convenience.
The script shown below is used to install and set up PostgreSQL:
#! /bin/bash
# Update package lists
sudo apt-get update
# Install PostgreSQL
sudo apt-get install -y postgresql postgresql-contrib
# Start & Enable PostgreSQL
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Variables for credentials
DB_USER="postgres"
DB_PASSWORD="password"
DB_NAME="db_name"
# Check if the DB_USER is postgres and alter the password of this user else create a new user
if [ "$DB_USER" == "postgres" ]; then
sudo -i -u postgres psql <<EOF
ALTER USER postgres WITH PASSWORD '$DB_PASSWORD';
EOF
echo "Password for 'postgres' user has been updated to '$DB_PASSWORD'."
else
sudo -i -u postgres psql <<EOF
-- Create a user named '$DB_USER' with password '$DB_PASSWORD'
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
EOF
fi
# Create the database and grant the user access to it
sudo -i -u postgres psql <<EOF
-- Create a database named '$DB_NAME'
CREATE DATABASE $DB_NAME;
-- Grant all privileges on the database '$DB_NAME' to the user '$DB_USER'
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF
# Modify pg_hba.conf to allow password authentication
PG_HBA_FILE=$(sudo -i -u postgres psql -t -P format=unaligned -c 'SHOW hba_file')
sudo sed -i "s/^local\s\+all\s\+all\s\+peer/local all all md5/" $PG_HBA_FILE
sudo sed -i "s/^host\s\+all\s\+all\s\+127.0.0.1\/32\s\+ident/host all all 127.0.0.1\/32 md5/" $PG_HBA_FILE
sudo sed -i "s/^host\s\+all\s\+all\s\+::1\/128\s\+ident/host all all ::1\/128 md5/" $PG_HBA_FILE
sudo bash -c "echo 'host all all 0.0.0.0/0 md5' >> $PG_HBA_FILE"
sudo bash -c "echo 'host all all ::/0 md5' >> $PG_HBA_FILE"
# Modify postgresql.conf to listen on all addresses
POSTGRESQL_CONF=$(sudo -i -u postgres psql -t -P format=unaligned -c 'SHOW config_file')
sudo sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" $POSTGRESQL_CONF
# Restart PostgreSQL to apply changes
sudo systemctl restart postgresql
echo "PostgreSQL setup is complete. User '$DB_USER' with database '$DB_NAME' has been created. The user can connect using the password '$DB_PASSWORD'."