shutdown57

Nginx vhost certbot

Feb 4th, 2025
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.76 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Pastikan skrip dijalankan dengan sudo
  4. if [[ $EUID -ne 0 ]]; then
  5.    echo "Harap jalankan skrip ini dengan sudo."
  6.    exit 1
  7. fi
  8.  
  9. # Minta input domain dari pengguna
  10. echo -n "Masukkan domain: "
  11. read domain
  12.  
  13. # Pastikan domain tidak kosong
  14. if [ -z "$domain" ]; then
  15.     echo "Domain tidak boleh kosong!"
  16.     exit 1
  17. fi
  18.  
  19. # Buat direktori root untuk website
  20. web_root="/var/www/$domain"
  21. mkdir -p "$web_root"
  22. chown -R www-data:www-data "$web_root"
  23. chmod -R 755 "$web_root"
  24.  
  25. # Buat file index.php default
  26. echo "<?php phpinfo(); ?>" > "$web_root/index.php"
  27.  
  28. # Buat file konfigurasi virtual host Nginx
  29. vhost_conf="/etc/nginx/sites-available/$domain"
  30. cat > "$vhost_conf" <<EOF
  31. server {
  32.     listen 80;
  33.     server_name $domain;
  34.     root $web_root;
  35.     index index.php index.html;
  36.  
  37.     location / {
  38.         try_files \$uri \$uri/ /index.php?\$query_string;
  39.     }
  40.  
  41.     location ~ \.php$ {
  42.         include snippets/fastcgi-php.conf;
  43.         fastcgi_pass unix:/run/php/php8.2-fpm.sock;
  44.         fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  45.         include fastcgi_params;
  46.     }
  47.  
  48.     location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|otf|eot|mp4|webm|ogg|mp3|wav)$ {
  49.         expires max;
  50.         log_not_found off;
  51.     }
  52. }
  53. EOF
  54.  
  55. # Aktifkan virtual host
  56. ln -s "$vhost_conf" /etc/nginx/sites-enabled/
  57. nginx -t && systemctl reload nginx
  58.  
  59. # Instal Certbot jika belum terpasang
  60. if ! command -v certbot &> /dev/null; then
  61.     apt install -y certbot python3-certbot-nginx
  62. fi
  63.  
  64. # Jalankan Certbot untuk mendapatkan SSL
  65. certbot --nginx -d "$domain" --email admin@$domain --agree-tos --non-interactive
  66.  
  67. # Restart Nginx
  68. systemctl reload nginx
  69.  
  70. echo "Virtual host untuk $domain telah dibuat dan SSL telah dikonfigurasi!"
  71.  
Advertisement
Add Comment
Please, Sign In to add comment