Shell Scripting : Setup Web Server (Apache): Difference between revisions
(Created page with "Buat file baru denga nama konfig_webserver.sh<syntaxhighlight lang="linuxconfig"> nano konfig_webserver.sh </syntaxhighlight>setelah") |
No edit summary |
||
Line 1: | Line 1: | ||
Buat file baru | Buat file baru dengan nama konfig_apache.sh<syntaxhighlight lang="linuxconfig"> | ||
nano | nano konfig_apache.sh | ||
</syntaxhighlight> | </syntaxhighlight>isikan script berikut ini :<syntaxhighlight lang="linuxconfig" line="1"> | ||
#!/bin/bash | |||
# ============================================== | |||
# AUTO WEB SERVER SETUP SCRIPT | |||
# Debian 12 - Apache + PHP + Virtual Host | |||
# ============================================== | |||
# Colors for output | |||
RED='\033[0;31m' | |||
GREEN='\033[0;32m' | |||
YELLOW='\033[1;33m' | |||
BLUE='\033[0;34m' | |||
NC='\033[0m' # No Color | |||
# Function to print colored output | |||
print_status() { | |||
echo -e "${BLUE}[INFO]${NC} $1" | |||
} | |||
print_success() { | |||
echo -e "${GREEN}[SUCCESS]${NC} $1" | |||
} | |||
print_warning() { | |||
echo -e "${YELLOW}[WARNING]${NC} $1" | |||
} | |||
print_error() { | |||
echo -e "${RED}[ERROR]${NC} $1" | |||
} | |||
# Check if running as root | |||
if [ "$EUID" -ne 0 ]; then | |||
print_error "Script harus dijalankan dengan sudo!" | |||
exit 1 | |||
fi | |||
# Banner | |||
echo "==========================================" | |||
echo " AUTO WEB SERVER SETUP SCRIPT" | |||
echo " Apache + PHP + Virtual Host" | |||
echo "==========================================" | |||
echo "" | |||
# Step 1: System Update | |||
print_status "Step 1: Update system packages..." | |||
apt update && apt upgrade -y | |||
print_success "System update completed" | |||
# Step 2: Install Apache | |||
print_status "Step 2: Installing Apache Web Server..." | |||
apt install -y apache2 apache2-utils | |||
print_success "Apache installed successfully" | |||
# Step 3: Install PHP and Extensions | |||
print_status "Step 3: Installing PHP and extensions..." | |||
apt install -y php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath php-json php-intl | |||
# Install additional PHP extensions for web development | |||
apt install -y php-gd php-imagick php-redis php-memcached php-xdebug | |||
print_success "PHP and extensions installed successfully" | |||
# Step 4: Enable Apache Modules | |||
print_status "Step 4: Enabling Apache modules..." | |||
a2enmod rewrite | |||
a2enmod ssl | |||
a2enmod headers | |||
print_success "Apache modules enabled" | |||
# Step 5: Create Main Web Directory | |||
print_status "Step 5: Setting up web directory..." | |||
mkdir -p /var/www/html | |||
chown -R www-data:www-data /var/www/html | |||
chmod -R 755 /var/www/html | |||
print_success "Web directory configured" | |||
# Step 6: Get Domain Name for Virtual Host | |||
echo "" | |||
print_status "Step 6: Virtual Host Configuration" | |||
read -p "Masukkan nama domain untuk virtual host (contoh: mysite.local): " domain_name | |||
if [ -z "$domain_name" ]; then | |||
print_error "Domain name tidak boleh kosong!" | |||
exit 1 | |||
fi | |||
# Step 7: Create Virtual Host Configuration | |||
print_status "Membuat virtual host untuk: $domain_name" | |||
VHOST_CONF="/etc/apache2/sites-available/${domain_name}.conf" | |||
cat > "$VHOST_CONF" << EOF | |||
<VirtualHost *:80> | |||
ServerName $domain_name | |||
ServerAlias www.$domain_name | |||
ServerAdmin webmaster@$domain_name | |||
DocumentRoot /var/www/$domain_name | |||
ErrorLog \${APACHE_LOG_DIR}/$domain_name_error.log | |||
CustomLog \${APACHE_LOG_DIR}/$domain_name_access.log combined | |||
<Directory /var/www/$domain_name> | |||
Options Indexes FollowSymLinks MultiViews | |||
AllowOverride All | |||
Order allow,deny | |||
allow from all | |||
Require all granted | |||
</Directory> | |||
</VirtualHost> | |||
EOF | |||
print_success "Virtual host configuration created: $VHOST_CONF" | |||
# Step 8: Create Web Directory for Domain | |||
print_status "Membuat web directory untuk $domain_name..." | |||
mkdir -p "/var/www/$domain_name" | |||
chown -R www-data:www-data "/var/www/$domain_name" | |||
chmod -R 755 "/var/www/$domain_name" | |||
# Step 9: Create Custom Index Page | |||
print_status "Membuat file index.html custom..." | |||
cat > "/var/www/$domain_name/index.html" << EOF | |||
<!DOCTYPE html> | |||
<html lang="id"> | |||
<head> | |||
<meta charset="UTF-8"> | |||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |||
<title>Selamat Datang di $domain_name</title> | |||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> | |||
<style> | |||
.jumbotron { | |||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |||
color: white; | |||
border-radius: 15px; | |||
padding: 4rem 2rem; | |||
margin-top: 2rem; | |||
box-shadow: 0 10px 30px rgba(0,0,0,0.2); | |||
} | |||
.feature-icon { | |||
font-size: 3rem; | |||
margin-bottom: 1rem; | |||
} | |||
.tech-badge { | |||
font-size: 0.9rem; | |||
margin: 0.2rem; | |||
} | |||
</style> | |||
</head> | |||
<body> | |||
<div class="container"> | |||
<div class="jumbotron text-center"> | |||
<h1 class="display-4">🎉 Selamat Datang!</h1> | |||
<p class="lead">Website <strong>$domain_name</strong> berhasil diaktifkan</p> | |||
<hr class="my-4"> | |||
<p>Server Anda sekarang berjalan dengan:</p> | |||
<div class="row mt-4"> | |||
<div class="col-md-3"> | |||
<div class="feature-icon">🌐</div> | |||
<h5>Apache2</h5> | |||
<p>Web Server</p> | |||
</div> | |||
<div class="col-md-3"> | |||
<div class="feature-icon">🐘</div> | |||
<h5>PHP 8.2</h5> | |||
<p>Processing</p> | |||
</div> | |||
<div class="col-md-3"> | |||
<div class="feature-icon">📁</div> | |||
<h5>Virtual Host</h5> | |||
<p>Domain: $domain_name</p> | |||
</div> | |||
<div class="col-md-3"> | |||
<div class="feature-icon">⚡</div> | |||
<h5>Optimized</h5> | |||
<p>Ready to Code</p> | |||
</div> | |||
</div> | |||
<div class="mt-4"> | |||
<span class="badge tech-badge bg-primary">Apache 2.4</span> | |||
<span class="badge tech-badge bg-success">PHP 8.2</span> | |||
<span class="badge tech-badge bg-warning">Debian 12</span> | |||
<span class="badge tech-badge bg-info">Bootstrap 5</span> | |||
<span class="badge tech-badge bg-dark">Shell Script</span> | |||
</div> | |||
<div class="mt-4"> | |||
<a href="/info.php" class="btn btn-light btn-lg">🔧 PHP Info</a> | |||
<a href="/" class="btn btn-outline-light btn-lg">🔄 Refresh</a> | |||
</div> | |||
</div> | |||
<div class="row mt-5"> | |||
<div class="col-md-6"> | |||
<div class="card"> | |||
<div class="card-body"> | |||
<h5 class="card-title">📊 Server Information</h5> | |||
<p class="card-text"> | |||
<strong>Domain:</strong> $domain_name<br> | |||
<strong>Directory:</strong> /var/www/$domain_name<br> | |||
<strong>Config:</strong> /etc/apache2/sites-available/$domain_name.conf<br> | |||
<strong>Time:</strong> $(date) | |||
</p> | |||
</div> | |||
</div> | |||
</div> | |||
<div class="col-md-6"> | |||
<div class="card"> | |||
<div class="card-body"> | |||
<h5 class="card-title">🚀 Next Steps</h5> | |||
<p class="card-text"> | |||
• Upload file website Anda<br> | |||
• Setup database MySQL<br> | |||
• Configure SSL certificate<br> | |||
• Monitor server logs<br> | |||
• Enjoy coding! 🎯 | |||
</p> | |||
</div> | |||
</div> | |||
</div> | |||
</div> | |||
<footer class="text-center mt-5 mb-3"> | |||
<p class="text-muted">Auto-generated by Apache Setup Script | Debian 12</p> | |||
</footer> | |||
</div> | |||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> | |||
</body> | |||
</html> | |||
EOF | |||
print_success "Custom index.html created" | |||
# Step 10: Create PHP Info File | |||
print_status "Membuat file info.php untuk testing..." | |||
cat > "/var/www/$domain_name/info.php" << EOF | |||
<?php | |||
phpinfo(); | |||
?> | |||
EOF | |||
print_success "PHP info file created" | |||
# Step 11: Enable Virtual Host | |||
print_status "Step 7: Mengaktifkan virtual host..." | |||
a2ensite $domain_name.conf | |||
# Step 12: Disable default site (optional) | |||
print_status "Menonaktifkan default site..." | |||
a2dissite 000-default.conf | |||
# Step 13: Restart Apache | |||
print_status "Restarting Apache service..." | |||
systemctl restart apache2 | |||
# Step 14: Add to hosts file for local testing | |||
print_status "Menambahkan entry ke /etc/hosts..." | |||
echo "127.0.0.1 $domain_name" >> /etc/hosts | |||
echo "127.0.0.1 www.$domain_name" >> /etc/hosts | |||
print_success "Hosts file updated" | |||
# Step 15: Final Summary | |||
echo "" | |||
echo "==========================================" | |||
print_success "WEB SERVER SETUP COMPLETED!" | |||
echo "==========================================" | |||
echo "" | |||
echo "📋 SUMMARY:" | |||
echo "-----------" | |||
echo "• 🌐 Domain: $domain_name" | |||
echo "• 📁 Web Directory: /var/www/$domain_name" | |||
echo "• ⚙️ Config File: /etc/apache2/sites-available/$domain_name.conf" | |||
echo "• 🐘 PHP Version: $(php -v | head -n1)" | |||
echo "• 🔥 Apache Status: $(systemctl is-active apache2)" | |||
echo "" | |||
echo "🚀 TEST YOUR SETUP:" | |||
echo "------------------" | |||
echo "1. Browser: http://$domain_name" | |||
echo "2. PHP Info: http://$domain_name/info.php" | |||
echo "" | |||
echo "📝 NEXT COMMANDS:" | |||
echo "----------------" | |||
echo "• Edit website: nano /var/www/$domain_name/index.html" | |||
echo "• View logs: tail -f /var/log/apache2/${domain_name}_error.log" | |||
echo "• Add SSL: certbot --apache -d $domain_name" | |||
echo "" | |||
echo "🎉 SELAMAT! Web server Anda sudah aktif dan siap digunakan!" | |||
echo "==========================================" | |||
# Step 16: Test PHP (optional) | |||
print_status "Testing PHP configuration..." | |||
php -v > /dev/null 2>&1 | |||
if [ $? -eq 0 ]; then | |||
print_success "PHP is working correctly" | |||
else | |||
print_warning "PHP mungkin ada masalah, cek dengan: php -v" | |||
fi | |||
# Step 17: Show service status | |||
print_status "Apache service status:" | |||
systemctl status apache2 --no-pager -l | |||
</syntaxhighlight>Simpan script tersebut. | |||
beri hak akses eksekusi<syntaxhighlight lang="linuxconfig"> | |||
chmod +x konfig_apache.sh | |||
</syntaxhighlight>jalankan script<syntaxhighlight lang="linuxconfig"> | |||
./konfig_apache.sh | |||
</syntaxhighlight> |
Revision as of 22:25, 7 October 2025
Buat file baru dengan nama konfig_apache.sh
nano konfig_apache.sh
isikan script berikut ini :
#!/bin/bash
# ==============================================
# AUTO WEB SERVER SETUP SCRIPT
# Debian 12 - Apache + PHP + Virtual Host
# ==============================================
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running as root
if [ "$EUID" -ne 0 ]; then
print_error "Script harus dijalankan dengan sudo!"
exit 1
fi
# Banner
echo "=========================================="
echo " AUTO WEB SERVER SETUP SCRIPT"
echo " Apache + PHP + Virtual Host"
echo "=========================================="
echo ""
# Step 1: System Update
print_status "Step 1: Update system packages..."
apt update && apt upgrade -y
print_success "System update completed"
# Step 2: Install Apache
print_status "Step 2: Installing Apache Web Server..."
apt install -y apache2 apache2-utils
print_success "Apache installed successfully"
# Step 3: Install PHP and Extensions
print_status "Step 3: Installing PHP and extensions..."
apt install -y php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath php-json php-intl
# Install additional PHP extensions for web development
apt install -y php-gd php-imagick php-redis php-memcached php-xdebug
print_success "PHP and extensions installed successfully"
# Step 4: Enable Apache Modules
print_status "Step 4: Enabling Apache modules..."
a2enmod rewrite
a2enmod ssl
a2enmod headers
print_success "Apache modules enabled"
# Step 5: Create Main Web Directory
print_status "Step 5: Setting up web directory..."
mkdir -p /var/www/html
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
print_success "Web directory configured"
# Step 6: Get Domain Name for Virtual Host
echo ""
print_status "Step 6: Virtual Host Configuration"
read -p "Masukkan nama domain untuk virtual host (contoh: mysite.local): " domain_name
if [ -z "$domain_name" ]; then
print_error "Domain name tidak boleh kosong!"
exit 1
fi
# Step 7: Create Virtual Host Configuration
print_status "Membuat virtual host untuk: $domain_name"
VHOST_CONF="/etc/apache2/sites-available/${domain_name}.conf"
cat > "$VHOST_CONF" << EOF
<VirtualHost *:80>
ServerName $domain_name
ServerAlias www.$domain_name
ServerAdmin webmaster@$domain_name
DocumentRoot /var/www/$domain_name
ErrorLog \${APACHE_LOG_DIR}/$domain_name_error.log
CustomLog \${APACHE_LOG_DIR}/$domain_name_access.log combined
<Directory /var/www/$domain_name>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>
EOF
print_success "Virtual host configuration created: $VHOST_CONF"
# Step 8: Create Web Directory for Domain
print_status "Membuat web directory untuk $domain_name..."
mkdir -p "/var/www/$domain_name"
chown -R www-data:www-data "/var/www/$domain_name"
chmod -R 755 "/var/www/$domain_name"
# Step 9: Create Custom Index Page
print_status "Membuat file index.html custom..."
cat > "/var/www/$domain_name/index.html" << EOF
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Selamat Datang di $domain_name</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.jumbotron {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 15px;
padding: 4rem 2rem;
margin-top: 2rem;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.feature-icon {
font-size: 3rem;
margin-bottom: 1rem;
}
.tech-badge {
font-size: 0.9rem;
margin: 0.2rem;
}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron text-center">
<h1 class="display-4">🎉 Selamat Datang!</h1>
<p class="lead">Website <strong>$domain_name</strong> berhasil diaktifkan</p>
<hr class="my-4">
<p>Server Anda sekarang berjalan dengan:</p>
<div class="row mt-4">
<div class="col-md-3">
<div class="feature-icon">🌐</div>
<h5>Apache2</h5>
<p>Web Server</p>
</div>
<div class="col-md-3">
<div class="feature-icon">🐘</div>
<h5>PHP 8.2</h5>
<p>Processing</p>
</div>
<div class="col-md-3">
<div class="feature-icon">📁</div>
<h5>Virtual Host</h5>
<p>Domain: $domain_name</p>
</div>
<div class="col-md-3">
<div class="feature-icon">⚡</div>
<h5>Optimized</h5>
<p>Ready to Code</p>
</div>
</div>
<div class="mt-4">
<span class="badge tech-badge bg-primary">Apache 2.4</span>
<span class="badge tech-badge bg-success">PHP 8.2</span>
<span class="badge tech-badge bg-warning">Debian 12</span>
<span class="badge tech-badge bg-info">Bootstrap 5</span>
<span class="badge tech-badge bg-dark">Shell Script</span>
</div>
<div class="mt-4">
<a href="/info.php" class="btn btn-light btn-lg">🔧 PHP Info</a>
<a href="/" class="btn btn-outline-light btn-lg">🔄 Refresh</a>
</div>
</div>
<div class="row mt-5">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">📊 Server Information</h5>
<p class="card-text">
<strong>Domain:</strong> $domain_name<br>
<strong>Directory:</strong> /var/www/$domain_name<br>
<strong>Config:</strong> /etc/apache2/sites-available/$domain_name.conf<br>
<strong>Time:</strong> $(date)
</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">🚀 Next Steps</h5>
<p class="card-text">
• Upload file website Anda<br>
• Setup database MySQL<br>
• Configure SSL certificate<br>
• Monitor server logs<br>
• Enjoy coding! 🎯
</p>
</div>
</div>
</div>
</div>
<footer class="text-center mt-5 mb-3">
<p class="text-muted">Auto-generated by Apache Setup Script | Debian 12</p>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
EOF
print_success "Custom index.html created"
# Step 10: Create PHP Info File
print_status "Membuat file info.php untuk testing..."
cat > "/var/www/$domain_name/info.php" << EOF
<?php
phpinfo();
?>
EOF
print_success "PHP info file created"
# Step 11: Enable Virtual Host
print_status "Step 7: Mengaktifkan virtual host..."
a2ensite $domain_name.conf
# Step 12: Disable default site (optional)
print_status "Menonaktifkan default site..."
a2dissite 000-default.conf
# Step 13: Restart Apache
print_status "Restarting Apache service..."
systemctl restart apache2
# Step 14: Add to hosts file for local testing
print_status "Menambahkan entry ke /etc/hosts..."
echo "127.0.0.1 $domain_name" >> /etc/hosts
echo "127.0.0.1 www.$domain_name" >> /etc/hosts
print_success "Hosts file updated"
# Step 15: Final Summary
echo ""
echo "=========================================="
print_success "WEB SERVER SETUP COMPLETED!"
echo "=========================================="
echo ""
echo "📋 SUMMARY:"
echo "-----------"
echo "• 🌐 Domain: $domain_name"
echo "• 📁 Web Directory: /var/www/$domain_name"
echo "• ⚙️ Config File: /etc/apache2/sites-available/$domain_name.conf"
echo "• 🐘 PHP Version: $(php -v | head -n1)"
echo "• 🔥 Apache Status: $(systemctl is-active apache2)"
echo ""
echo "🚀 TEST YOUR SETUP:"
echo "------------------"
echo "1. Browser: http://$domain_name"
echo "2. PHP Info: http://$domain_name/info.php"
echo ""
echo "📝 NEXT COMMANDS:"
echo "----------------"
echo "• Edit website: nano /var/www/$domain_name/index.html"
echo "• View logs: tail -f /var/log/apache2/${domain_name}_error.log"
echo "• Add SSL: certbot --apache -d $domain_name"
echo ""
echo "🎉 SELAMAT! Web server Anda sudah aktif dan siap digunakan!"
echo "=========================================="
# Step 16: Test PHP (optional)
print_status "Testing PHP configuration..."
php -v > /dev/null 2>&1
if [ $? -eq 0 ]; then
print_success "PHP is working correctly"
else
print_warning "PHP mungkin ada masalah, cek dengan: php -v"
fi
# Step 17: Show service status
print_status "Apache service status:"
systemctl status apache2 --no-pager -l
Simpan script tersebut. beri hak akses eksekusi
chmod +x konfig_apache.sh
jalankan script
./konfig_apache.sh