So we’ve all been there. We’ve set up a development server with specific versions of software, added specific modules, got everything up and running and then had the need to recreate the same exact platform on another server to duplicate and test your work.
The problem we sometimes run into is that we THINK we added every module, plugin and correct version of the previous server but we miss something and things happen that are unexpected.
My answer to this is BASH scripting. The beautiful thing about BASH scripting is that whatever you decided to type at the command line during that first install and setup can be added to a bash script. Think of it as taking notes and letting someone else do the work for you the next time. Every time you successfully install something or add a setting, take note of it in a text file to add to your BASH script.
Here is my example of a very simple bash script that I run to set up a Centos 6 server for a basic LAMP stack with a custom repository and some basic tools like a better version of TOP, NANO and the locate app. This also adds firewall rules to accept web traffic. It ends with the secure installation of MySQL.
I take the file below, name it setup.sh and make it executable by doing a “chmod +x setup.sh”. So after running a minimal install of CentOS, I just fire off this script, drink my coffee and wait for the mysql-secure-install prompt. After setting up MySQL I’m ready to SCP my code over to my web directory and get to work!
This ensures that I’m using the same modules, plugins, apps and settings across each of my development servers. The “-y” switch skips the “yes/no” prompts. Also notice that you can chain multiple apps/plugins/modules to the same “yum -y install” lines instead of having a separate line for each.
#! /bin/bash
#make sure our distro is all up to date with the latest code fixes
yum -y update
#install some useful tools and text editors
yum -y install wget yum-utils nano locate htop
#download the remi and epel repository to bypass the low PHP version and upgrade it to PHP 5.6
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm && rpm -Uvh epel-release-latest-6.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && rpm -Uvh remi-release-6*.rpm
#use config manager to enable PHP 5.6 downloads
yum-config-manager --enable remi-php56
#Install PHP, some common modules and MYSQL server/client
yum -y install php php-common php-curl php-gd php-hash php-imap php-jsmin php-json php-mbstring php-mcrypt php-openssl php-simplexml php-zip php-zlib php-mysqlnd mysql-client mysql-server
#insert some Firewall rules to accept web traffic and save the results
iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT
iptables -I INPUT 5 -p tcp --dport 443 -j ACCEPT
iptables-save > /etc/iptables.conf
# Start the apache webserver and enable to it start on reboot
service httpd start
chkconfig httpd on
# Start the MySQL service and enable to it start on reboot
service mysqld start
chkconfig mysqld on
#start the MySQL secure installation
mysql_secure_installation
And that’s it! Just follow the MySQL prompts to initialize your database!