Created
September 10, 2015 20:00
-
-
Save timvisee/48ba5fd493be1403c08d to your computer and use it in GitHub Desktop.
VPSscript 0.1.1
This file contains 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 | |
# VPS setup script by Tim Visee | |
APP_NAME="VPS-Builder"; | |
APP_VERSION_NAME="0.1"; | |
APP_VERSION_CODE=1; | |
LOG_PREFIX="[$APP_NAME] "; | |
LOG_PREFIX_ERROR="[ERROR] " | |
# Configuration | |
DB_PASS="root"; | |
# TODO: Make sure enough permissions are granted? | |
# Initialize. | |
function init { | |
# Show an initialization message | |
logEmpty | |
log "$APP_NAME v$APP_VERSION_NAME ($APP_VERSION_CODE) by Tim Visee"; | |
log "Copyright (c) Tim Visee 2015. All rights reserved."; | |
log " "; | |
# Gather and show the system status | |
printSystemStatus; | |
} | |
# Setup the VPS. | |
function setup { | |
# Remove unused robot files | |
removeFile "/robot.sh" | |
removeFile "/etc/rc2.d/S99Zrobot" | |
# Update apt-get | |
updateAptGet | |
# Update the files database | |
log "Updating the files database..."; | |
updatedb; | |
log "The files database has been updated successfully!"; | |
# Install systemd | |
aptGetInstall systemd; | |
# TODO: Configure grub here! | |
# Install ntp | |
aptGetInstall ntp; | |
# Configure the timezone | |
dpkg-reconfigure tzdata; | |
# Install htop | |
aptGetInstall htop; | |
# Setup nano | |
setupNano; | |
# Install webmin | |
installWebmin; | |
# TODO: Add a new user here! | |
# TODO: Harden SSH usage! | |
# TODO: Harden kernel usage! | |
# TODO: Setup firewall! | |
# Install MySQL server | |
installMysqlServer; | |
# Install apache | |
installApache; | |
# TODO: Create a site here...! | |
# Install PHP | |
installPhp; | |
# Install memcached | |
installMemcached; | |
# Install php-apc | |
installPhpApc; | |
# Install PhpMyAdmin | |
installPhpMyAdmin; | |
# Install git | |
installGit; | |
# Install log analizer | |
aptGetInstall awstats; | |
} | |
# Log a message to the console. | |
# | |
# @param 1 Message to log. | |
function log { | |
echo "$LOG_PREFIX$1" | |
} | |
# Log an error message to the console. | |
# | |
# @param 1 Error message to log. | |
function logError { | |
log "$LOG_PREFIX_ERROR$1" | |
} | |
# Log an empty line to the console. | |
function logEmpty { | |
echo " " | |
} | |
# Throw an error and terminate the script. | |
function error { | |
logError "An error occurred that couldn't be recovered. The script is being terminated!" | |
exit 1 | |
} | |
# Print the system status. | |
function printSystemStatus { | |
# Show a status message | |
log "Gathering system information..."; | |
# Get the system status | |
OS=$(uname -s); | |
ARCH=$(uname -m); | |
VER=$(uname -r); | |
# Print the system status | |
log " Operating System: $OS"; | |
log " Architecture: $ARCH"; | |
log " Version: $VER"; | |
log " " | |
} | |
# Install and configure MySQL server. | |
function installMysqlServer { | |
# TODO: Make sure MySQL isnt intalled already (update the data if that's the case? show prompt?) | |
# Predefine the MySQL password | |
echo "mysql-server mysql-server/root_password password $DB_PASS" | debconf-set-selections | |
echo "mysql-server mysql-server/root_password_again password $DB_PASS" | debconf-set-selections | |
# Install MySQL server | |
aptGetInstall mysql-server | |
# Show a password status message | |
log "The MySQL root password has been set!"; | |
# Configure MySQL | |
log "[MySQL] Configuring database..." | |
# Enable database passwords | |
log "[MySQL] Enabling user passwords..."; | |
mysql -u root --password=$DB_PASS -e "UPDATE mysql.user SET Password = PASSWORD('CHANGEME') WHERE User = 'root'"; | |
# Delete anonymous users | |
log "[MySQL] Removing anonoymous users..." | |
mysql -u root --password=$DB_PASS -e "DROP USER ''@'localhost'"; | |
# Delete the hostname user | |
mysql -u root --password=$DB_PASS -e "DROP USER ''@'$(hostname)'"; | |
# Delete the demo database | |
log "[MySQL] Deleting demo database..." | |
mysql -u root --password=$DB_PASS -e "DROP DATABASE test"; | |
# Flush the previliges | |
log "[MySQL] Flushing privileges..." | |
mysql -u root --password=$DB_PASS -e "FLUSH PRIVILEGES"; | |
log "[MySQL] Database has been configured successfully!" | |
# Restart MySQL | |
log "Restarting the MySQL service..."; | |
service mysql restart | indent; | |
log "The MySQL service has been restarted!"; | |
} | |
# Install and configure PHP. | |
function installPhp { | |
# Install PHP packages | |
aptGetInstall php5; | |
aptGetInstall php5-pear; | |
aptGetInstall php5-mysql; | |
aptGetInstall php5-curl; | |
# Restart apache | |
restartService apache2 | |
} | |
# Install and configure memcached. | |
function installMemcached { | |
# Install memcached | |
aptGetInstall memcached; | |
aptGetInstall php5-memcached; | |
# TODO: Install myMemcachedAdmin. | |
# TODO: Properly configure memcached. | |
# Restart the apache2 service | |
restartService apache2; | |
} | |
# Install and configure php-apc. | |
function installPhpApc { | |
# Install php-apc | |
aptGetInstall php-apc; | |
# TODO: Properly configure PHP Apc. | |
# Restart the apache2 service | |
restartService apache2; | |
} | |
# Install and configure phpmyadmin. | |
function installPhpMyAdmin { | |
# Install phpmyadmin | |
aptGetInstall phpmyadmin; | |
# TODO: Configure phpmyadmin while installing!!! | |
# Add the phpmyadmin configuration to the apache configuration | |
log "Configuring PhpMyAdmin with Apache..." | |
echo "" >> /etc/apache2/apache2.conf | |
echo "### AUTOMATICALLY ADDED BY VPS SCRIPT" >> /etc/apache2/apache2.conf | |
echo "### VPS SCRIPT BY TIM VISEE" >> /etc/apache2/apache2.conf | |
echo "" >> /etc/apache2/apache2.conf | |
echo "# PhpMyAdmin configuration" >> /etc/apache2/apache2.conf | |
echo "Include /etc/phpmyadmin/apache.conf" >> /etc/apache2/apache2.conf | |
log "PhpMyAdmin has been configured with Apache successfully!" | |
# Show a status message | |
log "PhpMyAdmin is now available at: http://host/phpmyadmin/"; | |
# TODO: Set the PhpMyAdmin security here! | |
# Restart the apache2 service | |
restartService apache2; | |
} | |
# Install and configure FTP. | |
function installFtp { | |
# Install vsftpd | |
aptGetInstall vsftpd; | |
# TODO: Configure vsftpd here! | |
} | |
# Install and configure git. | |
function installGit { | |
# Install git | |
aptGetInstall git; | |
# TODO: Configure git? | |
} | |
# Configure nano. | |
function setupNano { | |
# Display line numbers in the nano editor | |
log "Configuring the nano editor..." | |
echo "" >> /etc/nanorc | |
echo "### AUTOMATICALLY ADDED BY VPS SCRIPT" >> /etc/nanorc | |
echo "### VPS SCRIPT BY TIM VISEE" >> /etc/nanorc | |
echo "" >> /etc/nanorc | |
echo "# Show line numbers" >> /etc/nanorc | |
echo "set const" >> /etc/nanorc | |
log "The nano editor has been configured successfully!" | |
} | |
# Install and configure webmin. | |
function installWebmin { | |
# Add the webmin sources | |
log "Adding webmin source repositories..." | |
echo "" >> /etc/apt/sources.list | |
echo "### AUTOMATICALLY ADDED BY VPS SCRIPT" >> /etc/apt/sources.list | |
echo "### VPS SCRIPT BY TIM VISEE" >> /etc/apt/sources.list | |
echo "" >> /etc/apt/sources.list | |
echo "# Webmin sources" >> /etc/apt/sources.list | |
echo "deb http://download.webmin.com/download/repository sarge contrib" >> /etc/apt/sources.list | |
echo "deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib" >> /etc/apt/sources.list | |
log "Webmin source have been added successfully!" | |
# Change the directory | |
log "Creating webmin key..." | |
cd /root | indent | |
wget http://www.webmin.com/jcameron-key.asc | indent | |
apt-key add jcameron-key.asc | indent | |
log "Webmin key has been created successfully!" | |
# Update apt-get | |
updateAptGet; | |
# Install webmin | |
aptGetInstall webmin; | |
# Disable SSL for webmin | |
setConfigOption "/etc/webmin/miniserv.conf" "ssl" "0" | |
# Stop webmin | |
log "Stopping webmin..."; | |
logEmpty | |
/etc/webmin/stop | indent | |
logEmpty | |
log "Webmin has been stopped successfully!" | |
# Start webmin | |
log "Starting webmin..."; | |
logEmpty | |
/etc/webmin/start | indent | |
logEmpty | |
log "Webmin has been started successfully!" | |
} | |
# Install a mail server. | |
function installMailServer { | |
# Show a status message | |
log "Setting up mail server..."; | |
# Install exim4 | |
aptGetInstall exim4; | |
# Configure the mail server | |
log "Configuring exim4 server..."; | |
dpkg-reconfigure exim4-config | indent | |
log "exim4 has been configured successfully!" | |
# Show a status message | |
log "The mail server has been setup successfully!" | |
} | |
# Install apache. | |
function installApache { | |
# Install apache2 | |
aptGetInstall apache2; | |
# Install apache2-mpm-prefork | |
aptGetInstall apache2-mpm-prefork; | |
# Enable mod rewrite | |
log "Enabling mod rewrite..." | |
a2enmod rewrite; | |
# Enable mod deflate | |
log "Enabling mod deflate..." | |
a2enmod deflate; | |
# Restart apache | |
restartService apache2 | |
} | |
# Restart a service. | |
# | |
# @param 1 Name of the service to restart. | |
function restartService { | |
log "Restarting the $1 service..."; | |
service $1 restart; | |
log "The $1 service has been restarted successfully!"; | |
} | |
# Update and upgrade apt-get. | |
function updateAptGet { | |
# Show a status message | |
log "Updating apt-get..." | |
logEmpty | |
# Update apt-get | |
apt-get update | indent | |
# Show a status message | |
logEmpty | |
log "apt-get has been updated successfully!" | |
log "Upgrading apt-get..." | |
logEmpty | |
# Upgrade apt-get | |
apt-get --assume-yes -V upgrade | indent | |
# Show a status message | |
logEmpty | |
log "apt-get has been upgraded successfully!" | |
} | |
# Check whether a package exists. | |
# | |
# @param 1 Name of the package to check for. | |
# | |
# @return True if the package exists, false otherwise. | |
function packageExists { | |
return dpkg -l "$1" &> /dev/null | |
} | |
# Indent console output. | |
function indent { | |
sed 's/^/ /'; | |
} | |
# Install a package through apt-get. | |
# | |
# @param 1 The name of the package to install. | |
function aptGetInstall { | |
# Show an installation message | |
log "Installing $1..."; | |
logEmpty; | |
# Install the application | |
apt-get --assume-yes install $1 | indent; | |
# Show an success message | |
logEmpty; | |
# TODO: Make sure the package is installed properly | |
#if ! ! packageExists $1; | |
#then | |
# log "$1 has been installed successfully!"; | |
#else | |
# logError "$1 failed to install!"; | |
# error; | |
#fi | |
# Show a status message | |
log "$1 has been installed successfully!"; | |
} | |
# Start apt-get autoremove, to remove all unused packages. | |
function aptGetAutoremove { | |
# Show a status message | |
log "Automatically removing unused packages..."; | |
logEmpty; | |
# Remove unused packages | |
apt-get --assume-yes autoremove | indent; | |
# Show a status message | |
logEmpty; | |
log "Successfully removed unused packages!"; | |
} | |
# Remove a file. | |
# | |
# @param 1 The path of the file to remove. | |
function removeFile { | |
log "Removing file '$1'..." | |
[[ -f "$1" ]] && rm -f "$1"; | |
log "File removed successfully!" | |
} | |
# Set an option in a configuration file. | |
# | |
# @param 1 The path of the configuration file. | |
# @param 2 The key to change. | |
# @param 3 The new value of the key. | |
function setConfigOption { | |
log "Setting $2 to \"$3\" in $1..." | |
sed -i "s/\($2 *= *\).*/\1$3/" $1 | |
} | |
# Initialize the script | |
init; | |
log "Configuring systemd..."; | |
setConfigOption "/etc/default/grub" "GRUB_CMDLINE_LINUX_DEFAULT" "\"quiet init\=\/bin\/systemd\""; | |
log "Updating grub..."; | |
logEmpty; | |
update-grub | indent; | |
logEmpty; | |
log "systemd has been configured successfully!"; | |
# TODO: Should reboot! | |
# Setup everything | |
setup; | |
# Automatically remove unused apt-get packages | |
aptGetAutoremove; | |
# TODO: apt-get autoclean | |
# Script finished, show a message | |
log "The VPS setup script finished executing successfully!"; | |
logEmpty | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment