Skip to content

Instantly share code, notes, and snippets.

@davidisnotnull
Created July 15, 2025 09:35
Show Gist options
  • Save davidisnotnull/1b27dfc725a7a152f16319e1a97ae415 to your computer and use it in GitHub Desktop.
Save davidisnotnull/1b27dfc725a7a152f16319e1a97ae415 to your computer and use it in GitHub Desktop.
Ubuntu Webhost Setup with MariaDB
#!/bin/bash
set -e
log_error() {
echo -e "$1"
exit 1
}
# Check for Root Privileges
if [ "$(id -u)" -ne 0 ]; then
log_error "This script must be run as root. Please use sudo."
fi
# 1. Update package list
sudo apt update
sudo apt upgrade -y
# 2. Install PHP 8.3 and common extensions
sudo apt-get install -y software-properties-common lsb-release ca-certificates apt-transport-https wget gnupg2
sudo apt-get install -y php8.3 php8.3-cli php8.3-fpm php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring php8.3-zip php8.3-gd php8.3-bcmath
# 3. Disable and remove apache2 (only if installed)
if dpkg -l | grep -qw apache2; then
echo "Apache2 is installed. Removing..."
sudo systemctl stop apache2 || true
sudo systemctl disable apache2 || true
sudo apt-get remove --purge -y apache2 apache2-utils apache2-bin apache2.2-common || true
sudo apt-get autoremove -y
sudo apt-get autoclean
else
echo "Apache2 is not installed. Skipping removal."
fi
# 4. Install nginx and enable at startup
sudo apt-get install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
# 5. Install latest MariaDB
# Add MariaDB official repository
sudo apt-get install -y curl
curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash
# Install MariaDB server and client
sudo apt-get update
sudo apt-get install -y mariadb-server mariadb-client
# Enable and start MariaDB
sudo systemctl enable mariadb
sudo systemctl start mariadb
# 6. Set MariaDB root password and apply secure settings
# Replace 'YourStrongRootPassword' with your desired root password
MARIADB_ROOT_PASSWORD="YourStrongPassword123!"
sudo mysql -u root <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MARIADB_ROOT_PASSWORD}';
FLUSH PRIVILEGES;
EOF
# 7. Configure UFW firewall to allow HTTP, HTTPS, and Webmin
sudo apt-get install -y ufw
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw --force enable
# 8. Install Certbot for Let's Encrypt SSL (nginx plugin)
sudo apt-get install -y certbot python3-certbot-nginx
# Installation feedback
echo "Setup complete: PHP 8.3, Nginx, latest MariaDB (with root password set), Apache2 removed, UFW configured, Certbot installed."
echo "MariaDB root password is: ${MARIADB_ROOT_PASSWORD}"
echo "UFW is enabled. Allowed ports: 22 (SSH), 80 (HTTP), 443 (HTTPS)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment