Last active
August 19, 2024 09:14
-
-
Save sharik709/9cc329854a9c05d91cf98942972565d6 to your computer and use it in GitHub Desktop.
Ubuntu Laravel Full setup
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
#!/bin/bash | |
# Color definitions | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
CYAN='\033[0;36m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' # No Color | |
# Function to display a cool header | |
display_header() { | |
clear | |
echo -e "${CYAN}" | |
echo "╔══════════════════════════════════════════════════════════════╗" | |
echo "║ ║" | |
echo "║ ██╗ █████╗ ██████╗ █████╗ ██╗ ██╗███████╗██╗ ║" | |
echo "║ ██║ ██╔══██╗██╔══██╗██╔══██╗██║ ██║██╔════╝██║ ║" | |
echo "║ ██║ ███████║██████╔╝███████║██║ ██║█████╗ ██║ ║" | |
echo "║ ██║ ██╔══██║██╔══██╗██╔══██║╚██╗ ██╔╝██╔══╝ ██║ ║" | |
echo "║ ███████╗██║ ██║██║ ██║██║ ██║ ╚████╔╝ ███████╗███████╗ ║" | |
echo "║ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═══╝ ╚══════╝╚══════╝ ║" | |
echo "║ ║" | |
echo "║ LARAVEL SETUP WIZARD v3.0 ║" | |
echo "║ ║" | |
echo "╚══════════════════════════════════════════════════════════════╝" | |
echo -e "${NC}" | |
} | |
# Function to display a spinning progress indicator | |
spinner() { | |
local pid=$1 | |
local delay=0.1 | |
local spinstr='|/-\' | |
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do | |
local temp=${spinstr#?} | |
printf " [%c] " "$spinstr" | |
local spinstr=$temp${spinstr%"$temp"} | |
sleep $delay | |
printf "\b\b\b\b\b\b" | |
done | |
printf " \b\b\b\b" | |
} | |
# Function to prompt for input | |
prompt_input() { | |
local prompt="$1" | |
echo -e "${YELLOW}$prompt${NC}" | |
read -p "$prompt > " value | |
echo "$value" | |
} | |
# Function to check if a command exists | |
command_exists() { | |
command -v "$1" >/dev/null 2>&1 | |
} | |
# Display the header | |
display_header | |
# Check Ubuntu version | |
if [ -f /etc/os-release ]; then | |
. /etc/os-release | |
OS=$NAME | |
VER=$VERSION_ID | |
else | |
echo -e "${RED}Unable to determine OS version. This script is designed for Ubuntu.${NC}" | |
exit 1 | |
fi | |
echo -e "${BLUE}Detected: $OS $VER${NC}" | |
echo -e "${BLUE}Welcome to the Laravel Setup Wizard!${NC}" | |
echo -e "${BLUE}Please provide the following information:${NC}\n" | |
DOMAIN=$(prompt_input "Enter your domain name (e.g., example.com)") | |
EMAIL=$(prompt_input "Enter your email address for Let's Encrypt SSL certificate") | |
APP_NAME=$(prompt_input "Enter your Laravel application name") | |
DB_NAME=$(prompt_input "Enter the name for your MySQL database") | |
DB_USER=$(prompt_input "Enter the username for your MySQL database") | |
DB_PASS=$(prompt_input "Enter the password for your MySQL database user") | |
# Display a summary of the information provided | |
echo -e "\n${BLUE}Please confirm the following information:${NC}" | |
echo -e "Domain: ${GREEN}$DOMAIN${NC}" | |
echo -e "Email: ${GREEN}$EMAIL${NC}" | |
echo -e "Application Name: ${GREEN}$APP_NAME${NC}" | |
echo -e "Database Name: ${GREEN}$DB_NAME${NC}" | |
echo -e "Database Username: ${GREEN}$DB_USER${NC}" | |
echo -e "Database Password: ${GREEN}[HIDDEN]${NC}" | |
# Ask for confirmation | |
echo -e "\n${YELLOW}Is this information correct? (y/n)${NC}" | |
read -r confirmation | |
if [[ $confirmation != "y" ]]; then | |
echo -e "${RED}Setup cancelled. Please run the script again with the correct information.${NC}" | |
exit 1 | |
fi | |
# Set repository path | |
REPO_PATH="/var/www/html/$APP_NAME" | |
echo -e "\n${GREEN}Updating system...${NC}" | |
(sudo apt update && sudo apt upgrade -y) &>/dev/null & | |
spinner $! | |
echo -e "\n${GREEN}Installing required packages...${NC}" | |
(sudo apt install -y software-properties-common curl zip unzip fail2ban) &>/dev/null & | |
spinner $! | |
# PHP installation | |
echo -e "\n${GREEN}Installing PHP...${NC}" | |
if ! command_exists php; then | |
( | |
sudo add-apt-repository ppa:ondrej/php -y | |
sudo apt update | |
sudo apt install -y php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-xml php8.1-curl php8.1-mbstring php8.1-zip php8.1-bcmath php8.1-gd | |
) &>/dev/null & | |
spinner $! | |
else | |
echo -e "${BLUE}PHP is already installed.${NC}" | |
fi | |
# Composer installation | |
echo -e "\n${GREEN}Installing Composer...${NC}" | |
if ! command_exists composer; then | |
( | |
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')" | |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")" | |
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ] | |
then | |
>&2 echo 'ERROR: Invalid installer checksum' | |
rm composer-setup.php | |
exit 1 | |
fi | |
php composer-setup.php --quiet | |
RESULT=$? | |
rm composer-setup.php | |
sudo mv composer.phar /usr/local/bin/composer | |
) &>/dev/null & | |
spinner $! | |
else | |
echo -e "${BLUE}Composer is already installed.${NC}" | |
fi | |
# Node.js and npm installation | |
echo -e "\n${GREEN}Installing Node.js and npm...${NC}" | |
if ! command_exists node; then | |
( | |
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - | |
sudo apt install -y nodejs | |
) &>/dev/null & | |
spinner $! | |
else | |
echo -e "${BLUE}Node.js is already installed.${NC}" | |
fi | |
# Git installation | |
echo -e "\n${GREEN}Installing Git...${NC}" | |
if ! command_exists git; then | |
(sudo apt install -y git) &>/dev/null & | |
spinner $! | |
else | |
echo -e "${BLUE}Git is already installed.${NC}" | |
fi | |
# Nginx installation | |
echo -e "\n${GREEN}Installing Nginx...${NC}" | |
if ! command_exists nginx; then | |
(sudo apt install -y nginx) &>/dev/null & | |
spinner $! | |
else | |
echo -e "${BLUE}Nginx is already installed.${NC}" | |
fi | |
# MySQL installation | |
echo -e "\n${GREEN}Installing MySQL...${NC}" | |
if ! command_exists mysql; then | |
(sudo apt install -y mysql-server) &>/dev/null & | |
spinner $! | |
echo -e "\n${GREEN}Securing MySQL installation...${NC}" | |
sudo mysql_secure_installation | |
else | |
echo -e "${BLUE}MySQL is already installed.${NC}" | |
fi | |
echo -e "\n${GREEN}Creating MySQL database and user...${NC}" | |
( | |
sudo mysql -e "CREATE DATABASE IF NOT EXISTS $DB_NAME;" | |
sudo mysql -e "CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';" | |
sudo mysql -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';" | |
sudo mysql -e "FLUSH PRIVILEGES;" | |
) &>/dev/null & | |
spinner $! | |
# ... [The rest of the script remains largely the same, including Nginx configuration, | |
# SSL setup, firewall configuration, Laravel project setup, etc.] | |
# At the end of the script: | |
echo -e "\n${CYAN}╔══════════════════════════════════════════════════════════════╗${NC}" | |
echo -e "${CYAN}║ Setup Complete! ║${NC}" | |
echo -e "${CYAN}║ ║${NC}" | |
echo -e "${CYAN}║ Your Laravel application should now be set up and running. ║${NC}" | |
echo -e "${CYAN}║ To deploy updates in the future, run: ║${NC}" | |
echo -e "${CYAN}║ ${YELLOW}sudo /var/www/deploy.sh${CYAN} ║${NC}" | |
echo -e "${CYAN}║ ║${NC}" | |
echo -e "${CYAN}╚══════════════════════════════════════════════════════════════╝${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment