Skip to content

Instantly share code, notes, and snippets.

@masitings
Created December 4, 2024 09:48
Show Gist options
  • Save masitings/62b6a5868b8ae726894e4e2cac878293 to your computer and use it in GitHub Desktop.
Save masitings/62b6a5868b8ae726894e4e2cac878293 to your computer and use it in GitHub Desktop.
FrankenPHP with Laravel
#!/bin/bash
# Function to check if a service is installed
is_installed() {
command -v "$1" &>/dev/null
}
# Function to check OS and package manager
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
echo $ID
elif type lsb_release >/dev/null 2>&1; then
lsb_release -si
elif [ -f /etc/issue ]; then
cat /etc/issue | awk '{print $1}'
else
echo "unknown"
fi
}
# Function to check system memory and enable swap if necessary
enable_swap_if_needed() {
RAM=$(free -m | awk '/^Mem:/{print $2}')
if [ "$RAM" -le 2048 ]; then
echo "RAM is less than or equal to 2GB, enabling swap..."
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
else
echo "RAM is greater than 2GB, no need to enable swap."
fi
}
# Function to group installation processes
group_process() {
echo "-----------------------------------------------------"
echo "$1"
echo "-----------------------------------------------------"
}
# Detect OS type
OS=$(detect_os)
echo "Detected OS: $OS"
# Prompt user for domain name, Laravel app Git URL, SSL choice, and Laravel installation
read -p "Enter your domain name (e.g., example.com): " domain_name
read -p "Enter the Git clone URL of your Laravel app (Leave empty for fresh installation): " git_url
read -p "Do you want to install Laravel? (y/n): " install_laravel
read -p "Do you want to enable SSL for your domain? (y/n): " enable_ssl
# Prompt for email address (used for SSL setup)
if [ "$enable_ssl" == "y" ]; then
read -p "Enter your email address for SSL registration (e.g., [email protected]): " ssl_email
fi
# Set app directory as the domain name
app_directory=$domain_name
# Check if Supervisor should be installed
if ! is_installed "supervisord"; then
read -p "Do you want to install Supervisor? (y/n): " install_supervisor
else
echo "Supervisor is already installed. Skipping installation."
install_supervisor="n"
fi
# Check if MariaDB should be installed
if ! is_installed "mysql"; then
read -p "Do you want to install MariaDB? (y/n): " install_mariadb
else
echo "MariaDB is already installed. Skipping installation."
install_mariadb="n"
fi
# Check if Memcached should be installed
if ! is_installed "memcached"; then
read -p "Do you want to install Memcached? (y/n): " install_memcached
else
echo "Memcached is already installed. Skipping installation."
install_memcached="n"
fi
# Check if Redis should be installed
if ! is_installed "redis-server"; then
read -p "Do you want to install Redis? (y/n): " install_redis
else
echo "Redis is already installed. Skipping installation."
install_redis="n"
fi
# Check if Beanstalk should be installed
if ! is_installed "beanstalkd"; then
read -p "Do you want to install Beanstalk? (y/n): " install_beanstalk
else
echo "Beanstalk is already installed. Skipping installation."
install_beanstalk="n"
fi
# Update the system
group_process "Updating system..."
if [ "$OS" == "ubuntu" ] || [ "$OS" == "debian" ]; then
sudo apt update -y && sudo apt upgrade -y
elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then
sudo yum update -y
else
echo "Unsupported OS. Exiting script."
exit 1
fi
# Install mandatory dependencies (NVM, PHP, Composer)
group_process "Installing mandatory dependencies..."
# Install NVM (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
echo "NVM installed successfully. Please restart your terminal or run 'source ~/.bashrc'."
# Install PHP 8.3
if [ "$OS" == "ubuntu" ] || [ "$OS" == "debian" ]; then
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update -y
sudo apt install -y php8.3 php8.3-fpm php8.3-cli php8.3-mbstring php8.3-xml php8.3-mysql php8.3-opcache php8.3-curl php8.3-bcmath php8.3-zip
elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then
sudo yum install -y epel-release
sudo yum install -y yum-utils
sudo yum install -y php83 php83-php-fpm php83-php-cli php83-php-mbstring php83-php-xml php83-php-mysqlnd php83-php-opcache php83-php-curl php83-php-bcmath php83-php-zip
else
echo "Unsupported OS for PHP installation. Exiting script."
exit 1
fi
# Set PHP 8.3 as default
echo "Setting PHP 8.3 as default..."
sudo update-alternatives --set php /usr/bin/php8.3
sudo update-alternatives --set phpize /usr/bin/phpize8.3
sudo update-alternatives --set php-config /usr/bin/php-config8.3
# Install Composer
group_process "Installing Composer..."
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# Install Nginx
group_process "Installing Nginx..."
if [ "$OS" == "ubuntu" ] || [ "$OS" == "debian" ]; then
sudo apt install -y nginx
elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then
sudo yum install -y nginx
else
echo "Unsupported OS for Nginx installation. Exiting script."
exit 1
fi
# Start and enable Nginx service
echo "Starting and enabling Nginx..."
sudo systemctl start nginx
sudo systemctl enable nginx
# Install FrankenPHP (Only PHP server choice now)
group_process "Installing FrankenPHP..."
curl https://frankenphp.dev/install.sh | sh
mv frankenphp /usr/local/bin/
echo "FrankenPHP installed successfully."
# If Git URL is provided, clone the Laravel app and install dependencies, else skip installation
group_process "Cloning and Installing Laravel Application..."
if [ -n "$git_url" ]; then
echo "Cloning Laravel application from $git_url..."
cd /var/www
git clone $git_url $app_directory
# Install Laravel dependencies
echo "Installing Laravel dependencies..."
cd /var/www/$app_directory
composer install --optimize-autoloader --no-dev
elif [ "$install_laravel" == "y" ]; then
# If no Git URL is provided, create a fresh Laravel installation
echo "Creating a fresh Laravel installation..."
cd /var/www
composer create-project --prefer-dist laravel/laravel $app_directory
# Install Laravel dependencies
echo "Installing Laravel dependencies..."
cd /var/www/$app_directory
composer install --optimize-autoloader --no-dev
else
echo "Skipping Laravel installation."
fi
# Set the environment
group_process "Configuring Environment..."
echo "Configuring .env for production..."
cd /var/www/$app_directory
cp .env.example .env
php artisan key:generate
# Set permissions
group_process "Setting file permissions..."
echo "Setting file permissions..."
sudo chown -R www-data:www-data /var/www/$app_directory/storage
sudo chown -R www-data:www-data /var/www/$app_directory/bootstrap/cache
# Configure Nginx to use FrankenPHP
group_process "Configuring Nginx for FrankenPHP..."
sudo bash -c 'cat > /etc/nginx/sites-available/'$domain_name' <<EOF
server {
listen 80;
server_name '$domain_name';
root /var/www/'$app_directory'/public;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/frankenphp.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
EOF'
# Enable the site and reload Nginx
sudo ln -s /etc/nginx/sites-available/$domain_name /etc/nginx/sites-enabled/
sudo systemctl reload nginx
# SSL installation (if enabled)
if [ "$enable_ssl" == "y" ]; then
sudo certbot --nginx -d $domain_name --email $ssl_email --agree-tos --non-interactive --redirect
echo "SSL certificate installed and configured successfully."
fi
# Install MariaDB if selected
if [ "$install_mariadb" == "y" ]; then
group_process "Installing MariaDB..."
if [ "$OS" == "ubuntu" ] || [ "$OS" == "debian" ]; then
sudo apt install -y mariadb-server
elif [ "$OS" == "centos" ] || [ "$OS" == "rhel" ]; then
sudo yum install -y mariadb-server
fi
# Generate a random password for MariaDB root
mariadb_root_password=$(openssl rand -base64 12)
# Start MariaDB service
sudo systemctl enable mariadb
sudo systemctl start mariadb
# Secure MariaDB installation and set the random password
group_process "Securing MariaDB..."
sudo mysql -e "UPDATE mysql.user SET Password = PASSWORD('$mariadb_root_password') WHERE User = 'root';"
sudo mysql -e "DROP USER IF EXISTS ''@'localhost';"
sudo mysql -e "DELETE FROM mysql.db WHERE Db = 'test' OR Db = 'test_%';"
sudo mysql -e "FLUSH PRIVILEGES;"
# MariaDB installation and setup is complete
echo "MariaDB has been installed and configured."
fi
# At the end of the script, display the MariaDB root password
echo "Installation complete!"
if [ "$install_mariadb" == "y" ]; then
echo "-----------------------"
echo "MariaDB root password: $mariadb_root_password"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment