Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## This script automated create virtual host.
- ## Make sure your server was installed apache2 with php and mysql server.
- # Check if script is run as root
- if [[ $EUID -ne 0 ]]; then
- echo "This script must be run as root"
- exit 1
- fi
- # Check if command and domain name are provided
- if [ $# -lt 2 ]; then
- echo "Usage: $0 {create|create-wp|delete} domain.com"
- exit 1
- fi
- COMMAND=$1
- DOMAIN=$2
- DOCROOT="/var/www/${DOMAIN}"
- CONF="/etc/apache2/sites-available/${DOMAIN}.conf"
- # Function to generate random string for WordPress
- generate_password() {
- openssl rand -base64 12
- }
- check_certbot()
- {
- echo "Checking if Certbot is installed..."
- # Check if Certbot is installed
- if ! command -v certbot &> /dev/null; then
- echo "Certbot is not installed. "
- echo "Please install certbot first"
- echo "Visit : https://certbot.eff.org/"
- exit 1
- fi
- echo "Certbot is installed."
- }
- create_virtualhost() {
- local domain=$1
- local docroot=$2
- local conf=$3
- # Create document root directory
- echo "Creating document root directory..."
- mkdir -p $docroot
- chown -R www-data:www-data $docroot
- chmod -R 755 $docroot
- # Create a sample index.html
- echo "<html><head><title>Welcome to ${domain}</title></head><body><h1>Welcome to ${domain}!</h1></body></html>" > ${docroot}/index.html
- # Create Apache virtual host configuration
- echo "Creating Apache virtual host configuration..."
- cat > $conf <<EOF
- <VirtualHost *:80>
- ServerAdmin webmaster@${domain}
- ServerName ${domain}
- DocumentRoot ${docroot}
- #<Directory ${docroot}>
- # Options Indexes FollowSymLinks MultiViews
- # AllowOverride All
- # Require all granted
- #</Directory>
- ErrorLog \${APACHE_LOG_DIR}/${domain}_error.log
- CustomLog \${APACHE_LOG_DIR}/${domain}_access.log combined
- </VirtualHost>
- EOF
- # Enable the site
- echo "Enabling the virtual host..."
- a2ensite ${domain}
- # Test Apache configuration
- echo "Testing Apache configuration..."
- apache2ctl configtest
- if [ $? -eq 0 ]; then
- # Reload Apache
- echo "Reloading Apache..."
- systemctl reload apache2
- check_certbot
- # Install SSL certificate using Certbot
- echo "Installing SSL certificate using Certbot..."
- certbot --apache -d ${domain} --non-interactive --agree-tos --redirect
- echo "Setup completed successfully!"
- echo "Your website is now accessible at: https://${domain}"
- echo "Document root is: ${docroot}"
- else
- echo "Apache configuration test failed. Please check the configuration."
- exit 1
- fi
- }
- create_wordpress() {
- local domain=$1
- local docroot=$2
- # First create the virtualhost
- create_virtualhost $domain $docroot $CONF
- # Generate WordPress database credentials
- DB_NAME="wp_${domain//[.-]/_}"
- DB_USER="wp_${domain//[.-]/_}"
- DB_PASS=$(generate_password)
- echo "Installing required packages..."
- apt-get update
- 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
- # Enable PHP modules
- a2enmod php
- # Create MySQL database and user
- echo "Creating MySQL database and user..."
- mysql -e "CREATE DATABASE IF NOT EXISTS ${DB_NAME};"
- mysql -e "CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';"
- mysql -e "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';"
- mysql -e "FLUSH PRIVILEGES;"
- # Download and extract WordPress
- echo "Downloading and installing WordPress..."
- cd /tmp
- wget https://wordpress.org/latest.tar.gz
- tar xzf latest.tar.gz
- rm -rf ${docroot}/*
- cp -r wordpress/* ${docroot}/
- rm -rf wordpress latest.tar.gz
- # Create wp-config.php
- cp ${docroot}/wp-config-sample.php ${docroot}/wp-config.php
- sed -i "s/database_name_here/${DB_NAME}/" ${docroot}/wp-config.php
- sed -i "s/username_here/${DB_USER}/" ${docroot}/wp-config.php
- sed -i "s/password_here/${DB_PASS}/" ${docroot}/wp-config.php
- # Set WordPress salts
- wget -qO- https://api.wordpress.org/secret-key/1.1/salt/ | sed -e "s/\n//g" >> ${docroot}/wp-config.php
- # Set correct permissions
- chown -R www-data:www-data ${docroot}
- chmod -R 755 ${docroot}
- find ${docroot} -type d -exec chmod 755 {} \;
- find ${docroot} -type f -exec chmod 644 {} \;
- echo "WordPress installation completed!"
- echo "----------------------------------------"
- echo "WordPress has been installed with the following details:"
- echo "Site URL: https://${domain}"
- echo "Database Name: ${DB_NAME}"
- echo "Database User: ${DB_USER}"
- echo "Database Password: ${DB_PASS}"
- echo "----------------------------------------"
- echo "Please save these credentials and complete the installation by visiting https://${domain}"
- }
- delete_virtualhost() {
- local domain=$1
- local docroot=$2
- local conf=$3
- # Check if virtual host exists
- if [ ! -f "$conf" ]; then
- echo "Virtual host configuration for $domain does not exist."
- exit 1
- fi
- # Remove SSL certificate if exists
- echo "Removing SSL certificate..."
- certbot delete --cert-name $domain --non-interactive || true
- # Disable the site
- echo "Disabling the virtual host..."
- a2dissite $domain
- # Remove configuration file
- echo "Removing virtual host configuration..."
- rm -f $conf
- # Remove document root if exists
- if [ -d "$docroot" ]; then
- echo "Removing document root directory..."
- rm -rf $docroot
- fi
- # If WordPress was installed, remove database
- DB_NAME="wp_${domain//[.-]/_}"
- DB_USER="wp_${domain//[.-]/_}"
- echo "Checking for WordPress database..."
- mysql -e "DROP DATABASE IF EXISTS ${DB_NAME};"
- mysql -e "DROP USER IF EXISTS '${DB_USER}'@'localhost';"
- # Reload Apache
- echo "Reloading Apache..."
- systemctl reload apache2
- echo "Virtual host for $domain has been successfully removed."
- }
- echo " __ ___ _ ___ ____ _____ "
- echo " \ \ / / | | |/ _ \/ ___|_ _| "
- echo " \ \ / /| |_| | | | \___ \ | | "
- echo " \ V / | _ | |_| |___) || | "
- echo " \_/ |_| |_|\___/|____/ |_| "
- echo " "
- echo " ------[ APACHE2 VIRTUAL HOST CREATOR ]----- "
- echo " ------[ @javaradigital - javaradigital.com "
- echo " Usage : "
- echo ""
- echo " ./vhost.sh create domain.com : create virtual host and auto ssl "
- echo " ./vhost.sh delete domain.com : delete virtual host "
- echo " ./vhost.sh create-wp domain.com : create vhost and install wordpress"
- case $COMMAND in
- create)
- create_virtualhost $DOMAIN $DOCROOT $CONF
- ;;
- create-wp)
- create_wordpress $DOMAIN $DOCROOT
- ;;
- delete)
- delete_virtualhost $DOMAIN $DOCROOT $CONF
- ;;
- *)
- echo "Invalid command. Use 'create', 'create-wp' or 'delete'"
- exit 1
- ;;
- esac
Add Comment
Please, Sign In to add comment