Skip to content

Instantly share code, notes, and snippets.

@xdayeh
Last active August 5, 2024 23:09
Show Gist options
  • Save xdayeh/f2be80df004c05dd8f71e9c2ad1da0be to your computer and use it in GitHub Desktop.
Save xdayeh/f2be80df004c05dd8f71e9c2ad1da0be to your computer and use it in GitHub Desktop.
Automated User and Apache Virtual Host Setup Script for Ubuntu
#!/bin/bash
# Prompt the user for the username and website
read -p "Enter the username: " USERNAME
read -p "Enter the website (e.g., example.com): " WEBSITE
# Define variables
USER_HOME="/home/$USERNAME"
PUBLIC_HTML="$USER_HOME/public"
APACHE_CONF="/etc/apache2/sites-available/$WEBSITE.conf"
HOST_ENTRY="127.0.0.1 www.$WEBSITE"
# Add the user
sudo useradd -m -s /bin/bash "$USERNAME"
# Set the user password (prompts for input)
echo "Please set a password for $USERNAME:"
sudo passwd "$USERNAME"
# Create public_html directory
sudo -u "$USERNAME" mkdir -p "$PUBLIC_HTML"
# Set directory permissions
sudo chmod 755 "$USER_HOME"
# Copy the default Apache configuration and edit it
sudo cp /etc/apache2/sites-available/000-default.conf "$APACHE_CONF"
# Append Apache configuration for the new site
sudo bash -c "cat > $APACHE_CONF" <<EOF
<VirtualHost *:80>
ServerAdmin yqd@hotmail.com
ServerName $WEBSITE
ServerAlias www.$WEBSITE
DocumentRoot $PUBLIC_HTML
<Directory $PUBLIC_HTML>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
Options -Indexes
ServerSignature Off
</Directory>
</VirtualHost>
EOF
# Enable the new site and reload Apache
sudo a2ensite "$WEBSITE.conf"
sudo systemctl reload apache2
# Enable the rewrite module and restart Apache
sudo a2enmod rewrite
sudo systemctl restart apache2
# Update /etc/hosts
sudo bash -c "echo $HOST_ENTRY >> /etc/hosts"
# Create an index.php file in the public_html directory
sudo bash -c "cat > $PUBLIC_HTML/index.php" <<EOF
<?php
echo "Welcome to $WEBSITE!";
?>
EOF
# Change ownership of the index.php file to the created user
sudo chown "$USERNAME:$USERNAME" "$PUBLIC_HTML/index.php"
echo "Configuration complete. The site is available at http://www.$WEBSITE"
@xdayeh
Copy link
Author

xdayeh commented Aug 5, 2024

This Bash script automates the process of setting up a new user, configuring a virtual host in Apache, and creating a basic index.php file in the user's public HTML directory on an Ubuntu system. The script performs the following tasks:

  1. User Creation: Creates a new user with a specified username, home directory, and Bash shell.
  2. Password Setup: Prompts the administrator to set a password for the new user.
  3. Directory Setup: Creates a public_html directory in the user's home directory and sets the appropriate permissions.
  4. Apache Configuration: Copies the default Apache configuration, customizes it for the new virtual host, and enables the site.
  5. Apache Modules and Services: Enables the rewrite module and restarts the Apache service to apply changes.
  6. Hosts File Update: Adds an entry to the /etc/hosts file to map the new domain to 127.0.0.1.
  7. Index.php Creation: Creates a basic index.php file in the user's public_html directory and sets the correct ownership.

Usage:
Save the script to a file, make it executable, and run it with sudo privileges:
chmod +x setup_darkdapp.sh sudo ./setup_darkdapp.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment