shutdown57

Apache2 Virtualhost creator

Jan 26th, 2025 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## This script automated create virtual host.
  4. ## Make sure your server was installed apache2 with php and mysql server.
  5.  
  6.  
  7. # Check if script is run as root
  8. if [[ $EUID -ne 0 ]]; then
  9.    echo "This script must be run as root"
  10.    exit 1
  11. fi
  12.  
  13. # Check if command and domain name are provided
  14. if [ $# -lt 2 ]; then
  15.     echo "Usage: $0 {create|create-wp|delete} domain.com"
  16.     exit 1
  17. fi
  18.  
  19. COMMAND=$1
  20. DOMAIN=$2
  21. DOCROOT="/var/www/${DOMAIN}"
  22. CONF="/etc/apache2/sites-available/${DOMAIN}.conf"
  23.  
  24. # Function to generate random string for WordPress
  25. generate_password() {
  26.     openssl rand -base64 12
  27. }
  28. check_certbot()
  29. {
  30.  
  31. echo "Checking if Certbot is installed..."
  32.  
  33. # Check if Certbot is installed
  34. if ! command -v certbot &> /dev/null; then
  35.     echo "Certbot is not installed. "
  36.     echo "Please install certbot first"
  37.     echo "Visit : https://certbot.eff.org/"
  38.     exit 1
  39. fi
  40.  
  41. echo "Certbot is installed."
  42. }
  43. create_virtualhost() {
  44.     local domain=$1
  45.     local docroot=$2
  46.     local conf=$3
  47.  
  48.     # Create document root directory
  49.     echo "Creating document root directory..."
  50.     mkdir -p $docroot
  51.     chown -R www-data:www-data $docroot
  52.     chmod -R 755 $docroot
  53.  
  54.     # Create a sample index.html
  55.     echo "<html><head><title>Welcome to ${domain}</title></head><body><h1>Welcome to ${domain}!</h1></body></html>" > ${docroot}/index.html
  56.  
  57.     # Create Apache virtual host configuration
  58.     echo "Creating Apache virtual host configuration..."
  59.     cat > $conf <<EOF
  60. <VirtualHost *:80>
  61.     ServerAdmin webmaster@${domain}
  62.     ServerName ${domain}
  63.     DocumentRoot ${docroot}
  64.  
  65.     #<Directory ${docroot}>
  66.     #    Options Indexes FollowSymLinks MultiViews
  67.     #    AllowOverride All
  68.     #    Require all granted
  69.     #</Directory>
  70.  
  71.     ErrorLog \${APACHE_LOG_DIR}/${domain}_error.log
  72.     CustomLog \${APACHE_LOG_DIR}/${domain}_access.log combined
  73. </VirtualHost>
  74. EOF
  75.  
  76.     # Enable the site
  77.     echo "Enabling the virtual host..."
  78.     a2ensite ${domain}
  79.  
  80.     # Test Apache configuration
  81.     echo "Testing Apache configuration..."
  82.     apache2ctl configtest
  83.  
  84.     if [ $? -eq 0 ]; then
  85.         # Reload Apache
  86.         echo "Reloading Apache..."
  87.         systemctl reload apache2
  88.  
  89.         check_certbot
  90.         # Install SSL certificate using Certbot
  91.         echo "Installing SSL certificate using Certbot..."
  92.         certbot --apache -d ${domain} --non-interactive --agree-tos --redirect
  93.        
  94.         echo "Setup completed successfully!"
  95.         echo "Your website is now accessible at: https://${domain}"
  96.         echo "Document root is: ${docroot}"
  97.     else
  98.         echo "Apache configuration test failed. Please check the configuration."
  99.         exit 1
  100.     fi
  101. }
  102.  
  103. create_wordpress() {
  104.     local domain=$1
  105.     local docroot=$2
  106.  
  107.     # First create the virtualhost
  108.     create_virtualhost $domain $docroot $CONF
  109.  
  110.     # Generate WordPress database credentials
  111.     DB_NAME="wp_${domain//[.-]/_}"
  112.     DB_USER="wp_${domain//[.-]/_}"
  113.     DB_PASS=$(generate_password)
  114.  
  115.     echo "Installing required packages..."
  116.     apt-get update
  117.     apt-get install -y php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip mysql-server
  118.  
  119.     # Enable PHP modules
  120.     a2enmod php
  121.  
  122.     # Create MySQL database and user
  123.     echo "Creating MySQL database and user..."
  124.     mysql -e "CREATE DATABASE IF NOT EXISTS ${DB_NAME};"
  125.     mysql -e "CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';"
  126.     mysql -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';"
  127.     mysql -e "FLUSH PRIVILEGES;"
  128.  
  129.     # Download and extract WordPress
  130.     echo "Downloading and installing WordPress..."
  131.     cd /tmp
  132.     wget https://wordpress.org/latest.tar.gz
  133.     tar xzf latest.tar.gz
  134.     rm -rf ${docroot}/*
  135.     cp -r wordpress/* ${docroot}/
  136.     rm -rf wordpress latest.tar.gz
  137.  
  138.     # Create wp-config.php
  139.     cp ${docroot}/wp-config-sample.php ${docroot}/wp-config.php
  140.     sed -i "s/database_name_here/${DB_NAME}/" ${docroot}/wp-config.php
  141.     sed -i "s/username_here/${DB_USER}/" ${docroot}/wp-config.php
  142.     sed -i "s/password_here/${DB_PASS}/" ${docroot}/wp-config.php
  143.  
  144.     # Set WordPress salts
  145.     wget -qO- https://api.wordpress.org/secret-key/1.1/salt/ | sed -e "s/\n//g" >> ${docroot}/wp-config.php
  146.  
  147.     # Set correct permissions
  148.     chown -R www-data:www-data ${docroot}
  149.     chmod -R 755 ${docroot}
  150.     find ${docroot} -type d -exec chmod 755 {} \;
  151.     find ${docroot} -type f -exec chmod 644 {} \;
  152.  
  153.     echo "WordPress installation completed!"
  154.     echo "----------------------------------------"
  155.     echo "WordPress has been installed with the following details:"
  156.     echo "Site URL: https://${domain}"
  157.     echo "Database Name: ${DB_NAME}"
  158.     echo "Database User: ${DB_USER}"
  159.     echo "Database Password: ${DB_PASS}"
  160.     echo "----------------------------------------"
  161.     echo "Please save these credentials and complete the installation by visiting https://${domain}"
  162. }
  163.  
  164. delete_virtualhost() {
  165.     local domain=$1
  166.     local docroot=$2
  167.     local conf=$3
  168.  
  169.     # Check if virtual host exists
  170.     if [ ! -f "$conf" ]; then
  171.         echo "Virtual host configuration for $domain does not exist."
  172.         exit 1
  173.     fi
  174.  
  175.     # Remove SSL certificate if exists
  176.     echo "Removing SSL certificate..."
  177.     certbot delete --cert-name $domain --non-interactive || true
  178.  
  179.     # Disable the site
  180.     echo "Disabling the virtual host..."
  181.     a2dissite $domain
  182.  
  183.     # Remove configuration file
  184.     echo "Removing virtual host configuration..."
  185.     rm -f $conf
  186.  
  187.     # Remove document root if exists
  188.     if [ -d "$docroot" ]; then
  189.         echo "Removing document root directory..."
  190.         rm -rf $docroot
  191.     fi
  192.  
  193.     # If WordPress was installed, remove database
  194.     DB_NAME="wp_${domain//[.-]/_}"
  195.     DB_USER="wp_${domain//[.-]/_}"
  196.    
  197.     echo "Checking for WordPress database..."
  198.     mysql -e "DROP DATABASE IF EXISTS ${DB_NAME};"
  199.     mysql -e "DROP USER IF EXISTS '${DB_USER}'@'localhost';"
  200.  
  201.     # Reload Apache
  202.     echo "Reloading Apache..."
  203.     systemctl reload apache2
  204.  
  205.     echo "Virtual host for $domain has been successfully removed."
  206. }
  207.  
  208. echo " __     ___   _  ___  ____ _____            "
  209. echo " \ \   / / | | |/ _ \/ ___|_   _|           "
  210. echo "  \ \ / /| |_| | | | \___ \ | |             "
  211. echo "   \ V / |  _  | |_| |___) || |             "
  212. echo "    \_/  |_| |_|\___/|____/ |_|             "
  213. echo "                                            "    
  214. echo " ------[ APACHE2 VIRTUAL HOST CREATOR ]----- "
  215. echo " ------[ @javaradigital - javaradigital.com "
  216. echo " Usage : "
  217. echo ""
  218. echo " ./vhost.sh create domain.com     : create virtual host and auto ssl "
  219. echo " ./vhost.sh delete domain.com     : delete virtual host "
  220. echo " ./vhost.sh create-wp domain.com  : create vhost and install wordpress"
  221.  
  222. case $COMMAND in
  223.     create)
  224.         create_virtualhost $DOMAIN $DOCROOT $CONF
  225.         ;;
  226.     create-wp)
  227.         create_wordpress $DOMAIN $DOCROOT
  228.         ;;
  229.     delete)
  230.         delete_virtualhost $DOMAIN $DOCROOT $CONF
  231.         ;;
  232.     *)
  233.         echo "Invalid command. Use 'create', 'create-wp' or 'delete'"
  234.         exit 1
  235.         ;;
  236. esac
Add Comment
Please, Sign In to add comment