Bash script that will create an new user account for a LAMP environment

This bash script will create vhost, Linux and Mysql user, database and set all the permissions for a single account.

I needed this to create user accounts on a debian web server without having to use plesk, webmin, puppet or any other tools.

This is a modified/extended version of http://www.webtip………

Bash script:

#!/bin/bash

# permissions
if [ "$(whoami)" != "root" ]; then
        echo "Root privileges are required to run this, try running with sudo or as su user..."
        exit 2
fi

hosts_path="/etc/hosts"
vhosts_path="/etc/apache2/sites-available/"
vhost_skeleton_path="vhost.skeleton.conf"
web_root="/var/www/"
user_file="/var/www/users.txt"

# user input passed as options?
site_url=0
relative_doc_root=0
apache_group=0

while getopts ":u:d:g:" o; do
        case "${o}" in
                u)
                        site_url=${OPTARG}
                        ;;
                d)
                        relative_doc_root=${OPTARG}
                        ;;
                g)
                        apache_group=${OPTARG}
                        ;;
        esac
done

# prompt if not passed as options
if [ $site_url = 0 ]; then
        read -p "Please enter the desired URL (e.g. test.org): " site_url
fi

if [ $relative_doc_root = 0 ]; then
        read -p "Please enter the site path relative to the web root (e.g. web01): $web_root_path" relative_doc_root
fi

if [ $apache_group = 0 ]; then
        read -p "Please enter the apache group you would like to add the user to (e.g. www-data): " apache_group
fi

# construct absolute path
absolute_doc_root=$web_root$relative_doc_root

# create directory if it doesn't exists
if [ ! -d "$absolute_doc_root" ]; then

        # create directory
        `mkdir "$absolute_doc_root/"`
        #`chown -R $SUDO_USER:staff "$absolute_doc_root/"`

        # create index file
        indexfile="$absolute_doc_root/index.html"
        `touch "$indexfile"`
        echo " <html> <head> </head> <body>Welcome! </body> </html>" >> "$indexfile"
        echo "Created directory $absolute_doc_root/"
fi

# update vhost
vhost=`cat "$vhost_skeleton_path"`
vhost=${vhost//@site_url@/$site_url}
vhost=${vhost//@site_docroot@/$absolute_doc_root}

`touch $vhosts_path$site_url.conf`
echo "$vhost" > "$vhosts_path$site_url.conf"
echo "Updated vhosts in Apache config"

# generating password
echo "Generating password"
password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)

# quietly add a user without password
echo "Creating user"
adduser --quiet --ingroup www-data --disabled-password --shell /bin/bash --home $absolute_doc_root --gecos "User" $relative_doc_root

# set and store password
echo "$relative_doc_root:$password" | chpasswd
echo $relative_doc_root  $site_url  $password > $user_file

# create mysql database and username
echo "Creating Mysql database and user"
mysql -e "CREATE DATABASE ${relative_doc_root} /*\!40100 DEFAULT CHARACTER SET utf8 */;"
mysql -e "CREATE USER ${relative_doc_root}@localhost IDENTIFIED BY '${password}';"
mysql -e "GRANT ALL PRIVILEGES ON ${relative_doc_root}.* TO '${relative_doc_root}'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"

# update hosts file
echo 127.0.0.1    $site_url >> $hosts_path
echo "Updated the hosts file"

# restart apache
echo "Enabling site in Apache..."
echo `a2ensite $site_url`

echo "Restarting Apache..."
echo `/etc/init.d/apache2 restart`

echo "Process complete, check out the new site at http://$site_url" 
echo "Settings stored in $user_file"

exit 0

 

Skeleton file: (vhost.skeleton.conf)


# @site_url@

 <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "@site_docroot@"
    ServerName @site_url@
    ServerAlias www.@site_url@
    ErrorLog "logs/@site_url@-error_log.log"
    CustomLog "logs/@site_url@-access_log.log" common
    <Directory "@site_docroot@">
        Require all granted
        AllowOverride All
  </Directory>
</VirtualHost>


 

Leave a Reply