Last active
June 6, 2025 05:52
-
-
Save voxsar/de4b3cb366abb1697e9da30193a04348 to your computer and use it in GitHub Desktop.
A set of code for a raspberry pi that will provision a laravel or wp site (using wp cli). It will also connect to github create a repo, it will create a laravel with spatie backups and laravel filament preinstalled
This file contains hidden or 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 | |
| HOSTS_FILE="/etc/avahi/mdns-hosts.txt" | |
| if [ $# -ne 2 ]; then | |
| echo "Usage: $0 <hostname-without-local> <ip>" | |
| exit 1 | |
| fi | |
| hostname=$1 | |
| ip=$2 | |
| # Add to hosts file | |
| echo "$hostname $ip" >> "$HOSTS_FILE" | |
| # Publish immediately | |
| /usr/bin/avahi-publish -a "$hostname.local" -R "$ip" & | |
| echo $! > "/var/run/avahi-mdns/$hostname.pid" | |
| echo "Published $hostname.local with IP $ip" |
This file contains hidden or 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 | |
| HOSTS_FILE="/etc/avahi/mdns-hosts.txt" | |
| PID_DIR="/var/run/avahi-mdns" | |
| mkdir -p "$PID_DIR" | |
| # Cleanup existing pids | |
| rm -f "$PID_DIR"/* | |
| while IFS= read -r line || [[ -n "$line" ]]; do | |
| [[ -z "$line" || "$line" =~ ^# ]] && continue | |
| hostname=$(echo "$line" | awk '{print $1}') | |
| ip=$(echo "$line" | awk '{print $2}') | |
| if [[ -n "$hostname" && -n "$ip" ]]; then | |
| /usr/bin/avahi-publish -a "${hostname}.local" -R "$ip" & | |
| echo $! > "$PID_DIR/$hostname.pid" | |
| fi | |
| done < "$HOSTS_FILE" | |
| wait |
This file contains hidden or 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 | |
| # Check for root privileges | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "This script must be run as root" | |
| exit 1 | |
| fi | |
| read -p "Enter the site name (e.g., example.com): " site_name | |
| #remove folder | |
| sudo rm $site_name -rf | |
| #remove database | |
| db_name="${site_name//./_}_db" | |
| sudo mysql <<MYSQL_SCRIPT | |
| DROP DATABASE IF EXISTS $db_name; | |
| MYSQL_SCRIPT | |
| #remove mdns | |
| mdns_name="${site_name//./-}" | |
| sudo bash /usr/local/bin/remove-mdns.host.sh "$mdns_name" | |
| sudo systemctl restart avahi-mdns-manager.service | |
| #remove nginx config | |
| nginx_config="/etc/nginx/sites-available/$site_name" | |
| if [ -f "$nginx_config" ]; then | |
| sudo rm "$nginx_config" | |
| sudo rm "/etc/nginx/sites-enabled/$site_name" | |
| echo "Nginx configuration for $site_name removed." | |
| else | |
| echo "Nginx configuration for $site_name does not exist." | |
| fi | |
| # Reload Nginx to apply changes | |
| sudo nginx -t && sudo systemctl reload nginx | |
| echo "Site $site_name has been removed successfully." | |
| echo "Please wait for the DNS record to propagate before accessing the site." | |
| # clean up ssl | |
| sudo rm -rf /etc/letsencrypt/live/$site_name | |
| sudo rm -rf /etc/letsencrypt/archive/$site_name | |
| sudo rm -rf /etc/letsencrypt/renewal/$site_name.conf |
This file contains hidden or 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 | |
| HOSTS_FILE="/etc/avahi/mdns-hosts.txt" | |
| if [ $# -ne 1 ]; then | |
| echo "Usage: $0 <hostname-without-local>" | |
| exit 1 | |
| fi | |
| hostname=$1 | |
| # search for the hostname in the hosts file | |
| if grep -q "^$hostname " "$HOSTS_FILE"; then | |
| # remove the line from the hosts file | |
| sed -i "/^$hostname /d" "$HOSTS_FILE" | |
| echo "Removed $hostname from $HOSTS_FILE" | |
| else | |
| echo "Hostname $hostname not found in $HOSTS_FILE" | |
| exit 1 | |
| fi |
This file contains hidden or 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 | |
| # Check for root privileges | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "This script must be run as root" | |
| exit 1 | |
| fi | |
| # DigitalOcean API Configuration (not needed for raspberry) | |
| DO_TOKEN="" | |
| SERVER_PUBLIC_IP_ADDRESS="" | |
| #when running on local servers | |
| DOMAIN="local" | |
| # Update package lists | |
| apt update | |
| # Install required software if not already installed | |
| declare -A packages=( | |
| [nginx]="nginx" | |
| [mysql]="mysql-server" | |
| [php]="php" | |
| [wp_cli]="wp" | |
| [certbot]="certbot python3-certbot-nginx" | |
| [composer]="composer" | |
| [curl]="curl" | |
| ) | |
| for package in "${!packages[@]}"; do | |
| if ! command -v "$package" &> /dev/null; then | |
| echo "Installing ${packages[$package]}..." | |
| apt install -y ${packages[$package]} | |
| else | |
| echo "${packages[$package]} already installed." | |
| fi | |
| done | |
| # Prompt user for PHP version, site name, and framework selection | |
| read -p "Enter PHP version to use (e.g., 7.4, 8.0): " php_version | |
| read -p "Enter the site name (e.g., example.com): " site_name | |
| read -p "Do you want to set up WordPress or Laravel? (wordpress/laravel): " framework | |
| # Set default WordPress credentials | |
| wp_user="[email protected]" | |
| #passsword to login | |
| wp_password="" | |
| #password of database server user nimda | |
| db_password="" # Replace with your root MySQL password if different | |
| mdns_name="${site_name//./-}" | |
| #add the router assigned ip address | |
| /usr/local/bin/add-mdns-host.sh "$mdns_name" "" | |
| sudo systemctl restart avahi-mdns-manager.service | |
| # Create MySQL database and user | |
| db_name="${site_name//./_}_db" | |
| sudo mysql <<MYSQL_SCRIPT | |
| CREATE DATABASE IF NOT EXISTS $db_name; | |
| GRANT ALL PRIVILEGES ON $db_name.* TO 'nimda'@'%'; | |
| FLUSH PRIVILEGES; | |
| MYSQL_SCRIPT | |
| installpath=$site_name | |
| if [ "$framework" = "wordpress" ]; then | |
| installpath=$site_name | |
| else | |
| installpath="$site_name/public" | |
| fi | |
| # Self-signed SSL certificate generation for local development | |
| ssl_dir="/etc/ssl/$site_name" | |
| mkdir -p "$ssl_dir" | |
| openssl req -x509 -nodes -days 365 \ | |
| -subj "/C=US/ST=Dev/L=Local/O=Dev/CN=$site_name.$DOMAIN" \ | |
| -newkey rsa:2048 -keyout "$ssl_dir/self.key" -out "$ssl_dir/self.crt" | |
| # Create Nginx server block | |
| nginx_config="/etc/nginx/sites-available/$site_name" | |
| cat <<EOL > $nginx_config | |
| server { | |
| listen 80; | |
| server_name $site_name.$DOMAIN; | |
| return 301 https://\$host\$request_uri; | |
| } | |
| server { | |
| listen 443 ssl; | |
| server_name $site_name.$DOMAIN; | |
| root /var/www/$installpath; | |
| index index.php index.html index.htm; | |
| ssl_certificate $ssl_dir/self.crt; | |
| ssl_certificate_key $ssl_dir/self.key; | |
| location / { | |
| try_files \$uri \$uri/ /index.php?\$query_string; | |
| } | |
| location ~ \.php\$ { | |
| include snippets/fastcgi-php.conf; | |
| fastcgi_pass unix:/var/run/php/php$php_version-fpm.sock; | |
| } | |
| location ~ /\.ht { | |
| deny all; | |
| } | |
| listen [::]:443 ssl; | |
| } | |
| EOL | |
| # Enable site and reload nginx | |
| ln -s $nginx_config /etc/nginx/sites-enabled/ | |
| nginx -t && systemctl reload nginx | |
| echo "DNS record for $site_name.$DOMAIN has been created. Waiting for it to propagate..." | |
| sleep 2 # Wait for 2 seconds before running certbot | |
| # Set up WordPress | |
| if [ "$framework" = "wordpress" ]; then | |
| # Set up the web root and install WordPress | |
| mkdir -p /var/www/$site_name && cd /var/www/$site_name | |
| wp core download --allow-root | |
| wp config create --dbname=$db_name --dbuser=digipos --dbpass=$db_password --dbhost=localhost --allow-root | |
| wp core install --url="https://$site_name.$DOMAIN" --title="$site_name" --admin_user="$wp_user" --admin_password="$wp_password" --admin_email="$wp_user" --allow-root | |
| chown -R www-data:www-data /var/www/$site_name | |
| chmod -R 777 /var/www/$site_name | |
| echo "WordPress installation completed at https://$site_name.$DOMAIN" | |
| #set COMPOSER_ALLOW_SUPERUSER to 1 | |
| export COMPOSER_ALLOW_SUPERUSER=1 | |
| # Set up Laravel | |
| elif [ "$framework" = "laravel" ]; then | |
| if ! command -v composer &> /dev/null; then | |
| echo "Installing Composer..." | |
| apt install -y composer | |
| fi | |
| #set COMPOSER_ALLOW_SUPERUSER to 1 | |
| export COMPOSER_ALLOW_SUPERUSER=1 | |
| # Set up the web root and install Laravel | |
| mkdir -p /var/www/$site_name && cd /var/www | |
| composer create-project --prefer-dist "laravel/laravel:^10.0" $site_name | |
| cd /var/www/$site_name | |
| # Update database configuration | |
| sed -i "s/DB_DATABASE=laravel/DB_DATABASE=$db_name/" /var/www/$site_name/.env | |
| sed -i "s/DB_USERNAME=root/DB_USERNAME=nimda/" /var/www/$site_name/.env | |
| sed -i "s/DB_PASSWORD=/DB_PASSWORD=$db_password/" /var/www/$site_name/.env | |
| # Install Filament and Laravel Backup | |
| echo "Installing Filament and Laravel Backup packages..." | |
| composer require filament/filament | |
| composer require spatie/laravel-backup | |
| # Publish backup config | |
| php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" --tag=backup-config | |
| # Install Filament with panels | |
| php artisan filament:install --panels | |
| # Initialize Git repository | |
| echo "Initializing Git repository..." | |
| git init | |
| # Add safe directory for Git | |
| echo "Adding safe directory for Git..." | |
| git config --global --add safe.directory /var/www/$site_name | |
| gh api \ | |
| --method POST \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| /orgs/artslabcreatives/repos \ | |
| -f "name=$site_name" -f "description=system for $site_name created on raspberry server" -f "homepage=https://artslabcreatives.com" -F "private=true" -F "has_issues=true" -F "has_projects=true" -F "has_wiki=true" | |
| git remote add origin [email protected]:artslabcreatives/$site_name.git | |
| git add . | |
| git commit -m "Initial commit" | |
| git push origin master | |
| # Create Admin User Seeder | |
| echo "Creating admin user seeder..." | |
| php artisan make:seeder AdminUserSeeder | |
| # Create the AdminUserSeeder content | |
| cat > /var/www/$site_name/database/seeders/AdminUserSeeder.php << 'EOF' | |
| <?php | |
| namespace Database\Seeders; | |
| use Illuminate\Database\Seeder; | |
| use Illuminate\Support\Facades\Hash; | |
| use App\Models\User; | |
| use Filament\Models\Contracts\FilamentUser; | |
| use Illuminate\Support\Str; | |
| class AdminUserSeeder extends Seeder | |
| { | |
| /** | |
| * Run the database seeds. | |
| */ | |
| public function run(): void | |
| { | |
| User::create([ | |
| 'name' => 'Admin', | |
| 'email' => '[email protected]', | |
| 'email_verified_at' => now(), | |
| 'password' => Hash::make('[email protected]@123'), | |
| 'remember_token' => Str::random(10), | |
| ]); | |
| } | |
| } | |
| EOF | |
| # Add necessary use statements | |
| sed -i '/^use Illuminate\\Foundation\\Auth\\User as Authenticatable;/a use Filament\\Models\\Contracts\\FilamentUser;\nuse Filament\\Panel;' /var/www/$site_name/app/Models/User.php | |
| # Update class declaration to implement FilamentUser | |
| sed -i 's/class User extends Authenticatable/class User extends Authenticatable implements FilamentUser/' /var/www/$site_name/app/Models/User.php | |
| # Add the canAccessPanel method after trait usage | |
| sed -i '/use HasApiTokens, HasFactory, Notifiable;/a \ | |
| \n public function canAccessPanel(Panel \$panel): bool\n {\n return \$this->email === "[email protected]";\n }' /var/www/$site_name/app/Models/User.php | |
| # Add canAccessFilament method to User model | |
| # sed -i '/^{/a\\n public function canAccessPanel(Panel $panel): bool\n {\n return $this->email === \"[email protected]\";\n }' /var/www/$site_name/app/Models/User.php | |
| # Update DatabaseSeeder to call AdminUserSeeder | |
| sed -i '/run()/,/}/c\ public function run(): void\n {\n $this->call([\n AdminUserSeeder::class,\n ]);\n }' /var/www/$site_name/database/seeders/DatabaseSeeder.php | |
| # Run migrations and seeders | |
| php artisan migrate --seed | |
| # Set permissions | |
| chown -R www-data:www-data /var/www/$site_name | |
| chmod -R 777 /var/www/$site_name | |
| echo "Laravel with Filament and admin user has been installed at https://$site_name.$DOMAIN" | |
| else | |
| echo "Invalid choice. Please run the script again." | |
| exit 1 | |
| fi | |
| echo "Installation complete. Access your site at https://$site_name.$DOMAIN" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment