Skip to content

Instantly share code, notes, and snippets.

@dazeb
Created March 9, 2024 21:33
Show Gist options
  • Save dazeb/d1baceecc5ddc86ee50cb9629398998a to your computer and use it in GitHub Desktop.
Save dazeb/d1baceecc5ddc86ee50cb9629398998a to your computer and use it in GitHub Desktop.
Laravel server setup with user input and error handling.
#!/bin/bash
# Define color variables
GREEN='\033[1;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}Updating APT package manager repository...${NC}"
sudo apt update && sudo apt upgrade -y || {
echo -e "${RED}Failed to update APT package manager repository.${NC}"
exit 1
}
echo -e "${GREEN}Installing necessary packages...${NC}"
sudo apt install -y apache2 php php-cli php-fpm php-mysql php-xml php-mbstring mysql-server composer git || {
echo -e "${RED}Failed to install necessary packages.${NC}"
exit 1
}
# Prompt user for domain name
while true; do
read -p "Enter the domain name for your Laravel application: " domain_name
if [[ -z "$domain_name" ]]; then
echo -e "${RED}Domain name cannot be empty. Please enter a valid domain name.${NC}"
else
break
fi
done
echo -e "${GREEN}Creating Apache virtual host configuration...${NC}"
sudo tee /etc/apache2/sites-available/laravel.conf > /dev/null <<EOT
<VirtualHost *:80>
ServerName $domain_name
ServerAdmin webmaster@$domain_name
DocumentRoot /var/www/html/test-laravel/public
<Directory /var/www/html/test-laravel/public>
AllowOverride All
</Directory>
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOT
# Prompt user for Git repository URL
while true; do
read -p "Enter the Git repository URL for your Laravel project: " repo_url
if [[ -z "$repo_url" ]]; then
echo -e "${RED}Git repository URL cannot be empty. Please enter a valid URL.${NC}"
else
break
fi
done
echo -e "${GREEN}Cloning Laravel project from Git repository...${NC}"
cd /var/www/html
sudo git clone $repo_url test-laravel || {
echo -e "${RED}Failed to clone Laravel project from Git repository.${NC}"
exit 1
}
echo -e "${GREEN}Installing Laravel dependencies...${NC}"
cd test-laravel
sudo composer install || {
echo -e "${RED}Failed to install Laravel dependencies.${NC}"
exit 1
}
echo -e "${GREEN}Copying and configuring .env file...${NC}"
sudo cp .env.example .env
sudo nano .env
echo -e "${GREEN}Generating application key and running database migration...${NC}"
sudo php artisan key:generate || {
echo -e "${RED}Failed to generate application key.${NC}"
exit 1
}
sudo php artisan migrate || {
echo -e "${RED}Failed to run database migration.${NC}"
exit 1
}
echo -e "${GREEN}Setting proper permissions and ownership...${NC}"
sudo chown -R www-data /var/www/html/test-laravel || {
echo -e "${RED}Failed to set proper ownership for Laravel project.${NC}"
exit 1
}
sudo chmod -R 755 /var/www/html/test-laravel/storage || {
echo -e "${RED}Failed to set proper permissions for Laravel storage directory.${NC}"
exit 1
}
echo -e "${GREEN}Disabling default Apache configuration and enabling Laravel virtual host...${NC}"
sudo a2dissite 000-default.conf || {
echo -e "${RED}Failed to disable default Apache configuration.${NC}"
exit 1
}
sudo a2ensite laravel.conf || {
echo -e "${RED}Failed to enable Laravel virtual host.${NC}"
exit 1
}
sudo a2enmod rewrite || {
echo -e "${RED}Failed to enable Apache rewrite module.${NC}"
exit 1
}
echo -e "${GREEN}Restarting Apache...${NC}"
sudo systemctl restart apache2 || {
echo -e "${RED}Failed to restart Apache.${NC}"
exit 1
}
echo -e "${GREEN}Laravel deployment completed successfully!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment