Skip to content

Instantly share code, notes, and snippets.

@panchr
Last active August 29, 2015 14:08
Show Gist options
  • Save panchr/bc128618b2ea30314b5f to your computer and use it in GitHub Desktop.
Save panchr/bc128618b2ea30314b5f to your computer and use it in GitHub Desktop.
Bootstraps a LAMP or LEMP webserver
#!/bin/sh
# Rushy Panchal
# Bootstrap a new server, with either Apache or Nginx
# This is experimental and not production-ready
# I do not take any liability for any damage or problems caused by misuse
# This script is provided as-is, as detailed in the General Public License (GPL) version 3.0
# To use:
# upload to your server (through SFTP)
# chmod +x bootstrap.sh
# ./bootstrap.sh WEBSITE_NAME
# Options
if [["$1" != ""]]; then
set SITE_NAME = $1
else
set SITE_NAME = "$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')"
fi
if [["$2" != ""]]; then
set SERVER_TYPE = $2
else
set SERVER_TYPE = "apache"
fi
set BACKUP_EXCLUDE = "/proc
/sys
/dev/pts
/root/.dogecoin/blocks
/root/.dogecoin/database
/root/.dogecoin/chainstate"
# Updates
sudo apt-get update && sudo apt-get -y upgrade
# Server configuration
if [["$SERVER_TYPE" == "apache"]]; then
# LAMP
sudo apt-get install -y apache2
sudo apt-get install -y php5
sudo apt-get install -y libapache2-mod-auth-mysql libapache2-mod-php5
sudo service apache2 restart
# Apache configuration
cd /etc/apache2/sites-available
cp 000-default.conf $SITE_NAME.conf
sudo a2ensite $SITE_NAME.conf
sudo service apache2 restart
elif [["$SERVER_TYPE" == "nginx"]]; then
# LEMP
sudo apt-get install -y nginx
sudo service nginx start
sudo apt-get install -y php5-fpm
# Nginx configuration
echo "cgi.fix_pathinfo=0" >> /etc/php5/fpm/php.ini
cd /etc/nginx/sites-available
cp default $SITE_NAME.conf
sudo ln -s /etc/nginx/sites-available/$SITE_NAME.conf /etc/nginx/sites-enabled/$SITE_NAME.conf
sudo rm ../sites-enabled/default
sudo service nginx restart
sudo service php-fpm restart
else
printf "Server Type %s not implemented." "$SERVER_TYPE"
fi
# PHP and MySQL configurations
sudo apt-get install -y php5-mcrypt php5-curl php-apc
sudo apt-get install -y mysql-server php5-mysql
sudo mysql_install_db
sudo apt-get install -y fail2ban
# Python, Pip, and relevant packages
sudo apt-get install -y python python-pip
pip install requests
# Mail Server
sudo apt-get install -y postfix
postconf -e myhostname="$SITE_NAME" virtual_alias_maps=hash:/etc/postfix/virtual
printf "admin@%s root" "$SITE_NAME" >> /etc/postfix/virtual
sudo postmap /etc/postfix/virtual
sudo service postfix restart
sudo apt-get install -y mailutils
# Other packages
sudo apt-get install -y sqlite3 git vnstat
sudo apt-get -y autoremove # remove any unnecessary packages
sudo apt-get clean # cleanup old installers
sudo apt-get check # make sure there aren't any broken dependencies
# Apache2's web directory
sudo mkdir -p /var/www/$SITE_NAME/public_html
sudo chown -R $USER:$USER /var/www/$SITE_NAME/public_html
sudo chmod -R 755 /var/www
# Self-signed SSL certificate
cd "/etc/$SERVER_TYPE"
sudo mkdir -p ssl/$SITE_NAME && cd ssl/$SITE_NAME
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $SITE_NAME.key -out $SITE_NAME.crt
# Set up repository for website
cd /var
mkdir repo && cd repo
mkdir $SITE_NAME.git && cd $SITE_NAME.git
git init --bare
cd hooks
printf "#!/bin/sh\ngit --work-tree=/var/www/%s --git-dir=/var/repo/%s.git checkout -f" "$SITE_NAME" "$SITE_NAME" >> post-receive
chmod +x post-receive
# Server backups
mkdir /backups && cd /backups
printf "$BACKUP_EXCLUDE" >> backup-exclude
printf "tar -zcpf /backup/fullbackup.tar.gz --directory=/ --exclude=backup --exclude-from=backup-exclude ." >> backup-server
chmod +x backup-server
# Final instructions to the user
sudo mysql_secure_installation
printf "\n\n"
printf "\nNOTES:\n"
printf "\tMake sure to setup all necessary configuration in /etc/%s/sites-available/%s" "$SERVER_TYPE" "$SITE_NAME"
printf "\tAlso set up necessary SSL configurations in /etc/%s/ssl/%s" "$SERVER_TYPE" "$SITE_NAME"
if [["$SERVER_TYPE" == "apache"]]; then
echo "Apache Setup Guide: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04"
elif [["$SERVER_TYPE" == "nginx"]]; then
echo "Make sure to change the document root to /var/www/%s" "$SITE_NAME"
echo "Nginx Setup Guide: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04"
else
printf "Please look up the relevant setup guide for: %s" "$SITE_NAME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment