Skip to content

Instantly share code, notes, and snippets.

@snappytux
Last active May 23, 2025 03:23
Show Gist options
  • Save snappytux/e05bcf5c5a894e7acf2329f6f6db8a1c to your computer and use it in GitHub Desktop.
Save snappytux/e05bcf5c5a894e7acf2329f6f6db8a1c to your computer and use it in GitHub Desktop.
Ubuntu Server Setup Script for Ghost CMS - Automates user creation, system packages, MySQL, Node.js and Ghost CLI installation
#!/bin/bash
# Ghost CMS Installation Script for Ubuntu
# This script will set up a new user, install necessary packages,
# configure MySQL, and prepare the environment for Ghost CMS
# Exit on error
set -e
# Function to display usage information
function show_usage {
echo "Usage: $0 [options]"
echo "Options:"
echo " --username USERNAME Specify the new username"
echo " --user-password PWD Specify the user password"
echo " --mysql-password PWD Specify the MySQL root password"
echo " --nodejs-version VER Specify the Node.js version (e.g., 18, 20)"
echo " --help Display this help message"
exit 1
}
# Parse command line arguments
USERNAME=""
USER_PASSWORD=""
MYSQL_ROOT_PASSWORD=""
NODEJS_VERSION=""
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--username)
USERNAME="$2"
shift 2
;;
--user-password)
USER_PASSWORD="$2"
shift 2
;;
--mysql-password)
MYSQL_ROOT_PASSWORD="$2"
shift 2
;;
--nodejs-version)
NODEJS_VERSION="$2"
shift 2
;;
--help)
show_usage
;;
*)
echo "Unknown option: $1"
show_usage
;;
esac
done
# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Validate required parameters
if [[ -z "$USERNAME" ]]; then
read -p "Enter username for the new user: " USERNAME
fi
if [[ -z "$USER_PASSWORD" ]]; then
read -s -p "Enter password for the new user: " USER_PASSWORD
echo
fi
if [[ -z "$MYSQL_ROOT_PASSWORD" ]]; then
read -s -p "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
echo
fi
if [[ -z "$NODEJS_VERSION" ]]; then
read -p "Enter Node.js version (e.g., 18, 20): " NODEJS_VERSION
fi
echo "=========================================="
echo "Creating new user: $USERNAME"
echo "=========================================="
# Create user with password non-interactively
adduser --gecos "" --disabled-password $USERNAME
echo "$USERNAME:$USER_PASSWORD" | chpasswd
echo "=========================================="
echo "Adding user to sudo group"
echo "=========================================="
usermod -aG sudo $USERNAME
echo "=========================================="
echo "Updating system packages"
echo "=========================================="
apt-get update
apt-get upgrade -y
echo "=========================================="
echo "Installing Nginx and MySQL"
echo "=========================================="
apt-get install -y nginx mysql-server
echo "=========================================="
echo "Starting Nginx"
echo "=========================================="
systemctl start nginx
systemctl enable nginx
echo "=========================================="
echo "Configuring MySQL"
echo "=========================================="
# Start MySQL if not already running
systemctl start mysql
systemctl enable mysql
# Configure MySQL - handling both fresh install and existing installation cases
if mysqladmin -u root status &>/dev/null; then
# MySQL allows root access without password (fresh install)
echo "Setting up root password for MySQL (fresh install)"
mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '${MYSQL_ROOT_PASSWORD}';"
mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "FLUSH PRIVILEGES;"
else
echo "Warning: Could not configure MySQL automatically."
echo "Please configure MySQL root password manually after installation."
echo "Run the following commands:"
echo " sudo mysql"
echo " ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'your_password';"
echo " FLUSH PRIVILEGES;"
echo " exit"
fi
echo "MySQL configuration completed"
echo "=========================================="
echo "Installing Node.js version ${NODEJS_VERSION}"
echo "=========================================="
apt-get update && apt-get install -y ca-certificates curl gnupg
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODEJS_VERSION}.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
apt-get update
apt-get install -y nodejs
echo "=========================================="
echo "Installing Ghost CLI"
echo "=========================================="
npm install ghost-cli@latest -g
echo "=========================================="
echo "Preparing Ghost directory"
echo "=========================================="
mkdir -p /var/www/ghost
chown -R $USERNAME:$USERNAME /var/www/ghost
chmod 775 /var/www/ghost
echo "=========================================="
echo "Setup complete!"
echo "=========================================="
echo "To install Ghost, run the following commands:"
echo "su - $USERNAME"
echo "cd /var/www/ghost"
echo "ghost install"
echo ""
echo "Installation completed. Type 'ghost install' to proceed with Ghost installation."
@snappytux
Copy link
Author

snappytux commented May 17, 2025

Ghost CMS Server Setup Script for Ubuntu

A comprehensive bash script that automates the setup of a Ghost CMS environment on Ubuntu servers. The script handles:

  • Creating a new system user with sudo privileges
  • System updates and package installation (nginx, MySQL)
  • MySQL secure configuration
  • Node.js installation with version selection
  • Ghost CLI installation
  • Directory preparation for Ghost installation

Usage:

./setup_ghost.sh --username [user] --user-password [password] --mysql-password [mysql_pwd] --nodejs-version [e.g. 18]

After running this script, you can proceed with the actual Ghost installation by using the 'ghost install' command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment