Skip to content

Instantly share code, notes, and snippets.

@tarikmanoar
Created September 21, 2024 15:47
Show Gist options
  • Save tarikmanoar/7b544b2b109d5f15398f79c5419f075f to your computer and use it in GitHub Desktop.
Save tarikmanoar/7b544b2b109d5f15398f79c5419f075f to your computer and use it in GitHub Desktop.
This script help you to setup laravel prodcution ready by one click
#!/bin/bash
# Colors for output
GREEN='\033[0;32m'
NC='\033[0m'
# Function to update and upgrade system
update_system() {
echo -e "${GREEN}Updating and upgrading the system...${NC}"
sudo apt update -y && sudo apt upgrade -y
}
# Function to install NGINX
install_nginx() {
echo -e "${GREEN}Installing NGINX...${NC}"
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
}
# Function to install Apache
install_apache() {
echo -e "${GREEN}Installing Apache...${NC}"
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
}
# Function to install PHP
install_php() {
echo -e "${GREEN}Which PHP version do you want to install? (e.g., 8.0, 8.1, 7.4): ${NC}"
read php_version
echo -e "${GREEN}Installing PHP $php_version...${NC}"
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php$php_version-cli php$php_version-fpm -y
# Install PHP and required extensions
sudo apt install php$php_version \
php$php_version-mysql \
php$php_version-mbstring \
php$php_version-xml \
php$php_version-bcmath \
php$php_version-json \
php$php_version-zip \
php$php_version-curl \
php$php_version-ctype \
php$php_version-dom \
php$php_version-fileinfo \
php$php_version-filter \
php$php_version-hash \
php$php_version-openssl \
php$php_version-pcre \
php$php_version-pdo \
php$php_version-session \
php$php_version-tokenizer \
php$php_version-xml -y
sudo systemctl restart php$php_version-fpm
}
# Function to install MySQL
install_mysql() {
echo -e "${GREEN}Installing MySQL...${NC}"
sudo apt update
sudo apt install mysql-server -y
# Start and enable MySQL service
sudo systemctl start mysql
sudo systemctl enable mysql
echo -e "${GREEN}Securing MySQL installation...${NC}"
# Ask for root password to be set
echo -e "${GREEN}Please enter a strong password for the MySQL root user:${NC}"
read -s root_password
# Secure MySQL installation without manual prompts
sudo mysql -e "DELETE FROM mysql.user WHERE User='';" # Remove anonymous users
sudo mysql -e "DROP DATABASE IF EXISTS test;" # Remove the test database
sudo mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';" # Drop any test-related databases
sudo mysql -e "FLUSH PRIVILEGES;" # Reload privilege tables
# Modify the root user to use mysql_native_password and set a strong password
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$root_password';"
# Verify root user's authentication method
echo -e "${GREEN}Verifying root user's authentication method...${NC}"
sudo mysql -e "SELECT user,authentication_string,plugin,host FROM mysql.user;"
# Apply privilege changes
sudo mysql -e "FLUSH PRIVILEGES;"
echo -e "${GREEN}MySQL root password has been set and secured.${NC}"
echo -e "${GREEN}To access MySQL from now on, use the following command:${NC}"
echo "mysql -u root -p"
echo "Enter the password: $root_password"
}
# Function to setup Laravel directory
setup_laravel() {
echo -e "${GREEN}Setting up Laravel directory...${NC}"
echo -e "${GREEN}Enter your project directory name: ${NC}"
read project_name
# Navigate to /var/www
cd /var/www
sudo mkdir $project_name
cd $project_name
# Set permissions
sudo chown -R www-data:www-data /var/www/$project_name
sudo chmod -R 755 /var/www/$project_name
# Install Composer
sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
composer -V
}
# Function to get the server's public IP
get_public_ip() {
PUBLIC_IP=$(curl -s ifconfig.me)
echo $PUBLIC_IP
}
# Ask for domain or IP and use it
get_domain_or_ip() {
echo -e "${GREEN}Enter your domain name (or leave blank to use the server's public IP): ${NC}"
read domain_or_ip
if [ -z "$domain_or_ip" ]; then
domain_or_ip=$(get_public_ip)
echo -e "${GREEN}No domain provided. Using server public IP: $domain_or_ip${NC}"
else
echo -e "${GREEN}Using provided domain: $domain_or_ip${NC}"
fi
}
# Function to configure NGINX for Laravel
configure_nginx_laravel() {
echo -e "${GREEN}Configuring NGINX for Laravel...${NC}"
# Ask for domain or IP
get_domain_or_ip
# Create NGINX config file for Laravel
sudo cat > /etc/nginx/sites-available/laravel <<EOL
server {
listen 80;
listen [::]:80;
server_name $domain_or_ip;
root /var/www/$project_name/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php index.html index.htm;
charset utf-8;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php$php_version-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.ht {
deny all;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
EOL
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
}
# Function to configure Apache for Laravel
configure_apache_laravel() {
echo -e "${GREEN}Configuring Apache for Laravel...${NC}"
# Ask for domain or IP
get_domain_or_ip
# Create Apache config file for Laravel
sudo cat > /etc/apache2/sites-available/laravel.conf <<EOL
<VirtualHost *:80>
ServerName $domain_or_ip
DocumentRoot /var/www/$project_name/public
<Directory /var/www/$project_name>
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/$project_name/public>
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php$php_version-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost>
EOL
sudo a2ensite laravel.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
}
# Welcome message
echo -e "${GREEN}Welcome to the Laravel Server Setup Script${NC}"
# Update system
update_system
# Choose web server
echo -e "${GREEN}Choose your web server: ${NC}"
echo "1) NGINX"
echo "2) Apache"
read webserver
if [ "$webserver" == "1" ]; then
install_nginx
configure_nginx_laravel
elif [ "$webserver" == "2" ]; then
install_apache
configure_apache_laravel
else
echo "Invalid choice!"
exit 1
fi
# Install PHP
install_php
# Install MySQL
echo -e "${GREEN}Do you want to install MySQL? (y/n): ${NC}"
read install_mysql_choice
if [ "$install_mysql_choice" == "y" ]; then
install_mysql
fi
# Set up Laravel
setup_laravel
echo -e "${GREEN}Laravel setup is complete!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment