Last active
August 5, 2024 23:09
-
-
Save xdayeh/f2be80df004c05dd8f71e9c2ad1da0be to your computer and use it in GitHub Desktop.
Automated User and Apache Virtual Host Setup Script for Ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 [email protected] | |
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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