LAMP Install Automated
I had a small project where I was tasked with installing and configuring several virtual machines with their respective *AMP components (apache, maria db and php.) After performing the installs on a couple of servers, I quickly realized that I should spend the extra time to automate the procedure.
Below are some quick and to the point scripts to check the current status of the *AMP stack on the server, perform the installation of those components and lastly uninstall those components if needed.
These steps were performed on virtual machines hosted by VMWare Workstation 12 Pro and running CentOS Linux release 7.2.1511 (Core) as the guest operating system. However, the scripts should work for all Linux/UNIX based operating systems where bash shell and rpm/yum repository is used.
Checking status of *AMP components
This script relies upon rpm’s -qa switch to query currently installed packages piped into a grep to search for the name of a package such as ‘httpd’ which is then piped again into a wc -l which will return the numerical count of the processes found.
After finding the above value and setting its content into a variable, I simply created several if/else statements to check if the variable was empty which probably means it is not installed and if the count is greater than one, the script will enter a loop equal to length of the count found and output the installed components name.
Installing *AMP Components
This script will first check if the *AMP components are installed; if the components are not installed, the script will proceed with installation and do so quietly without any output – this is done simply by calling yum install ‘pkg name’ -y -q -e 0:
The script will also handle alternate conditions where the components may already be installed. In this scenario, the script will output ‘pkg name install failed…’ Whether this install failed due to the components already existing or some other underlying issue – you will have to debug that on your own but the script will not attempt to re-install the components if they are detected:
If you do need to debug the installation due to some strange issues with your yum, the below would be a good place to start:
Uninstalling *AMP Components
The uninstall script is very similar to the install script in that it utilizes rpm’s -qa switch to check if the components exist or not. Based on that, it proceeds to remove the components which are hard-coded into the script. I originally wanted to dynamically handle the removal of each package in the event that the package name or version changed later on but I encountered an issue with this approach.
When using yum to install httpd, mariadb-server mariadb and php – I did not realize that some of the components were the actual “parent” component, the rest of the items were installed as dependencies once the main parent component was installed. Due to this oversight, when I worked on the portion of the script that handled the removal of the LAMP stack, I ended up with erroneous output stating that several components were not available to be uninstalled. This is simply due to the parent component being removed first which in parallel also removes their child dependencies.
For example, installing php will give you the below components. However, if you uninstalled “php-common-5.4.16-42.el7.x86_64” you will also lose the php-5.4 and the php-cli components.
Here is how I approached my code:
- Count how many sub-components exist for a given parent component (php has 3 sub-components.)
- Set the total number of sub-components as a max count and the first entry of a sub-component as the starting count. (php max count would be 3 and starting count would be 1)
- Iterate using a for loop through the counts and pass the count as an integer argument to my yum remove command to handle removing php’s sub-components 1-3 without explicitly mentioning the sub-component name.
This would have been the best approach at removing these components based on their count value; however, if we recall that removing “php-common-5.4.16-42.el7.x86_64” will remove all of the sub-components as well – this means that if count 1 removes php-common, then although count 2 and count 3 exists, there will not be any actual components to remove! As you can see, we have two occurrences of “Error: Need to pass a list of pkgs to remove” messages, this is because once the loop executes and removes the package where count = 1, the two sub-components for php is also removed resulting in nothing being passed as an argument in the yum remove command for when count=2 and count=3.
Anyways, back to the uninstall script:
Both of the install and uninstall scripts utilize another script:
This lamp_status.sh was written as a function so that I can reutilize it anywhere else within my code as long as I source as shown in the screenshot above. You can download the scripts in the following link -> LAMP Utility.
It would be great if you could provide any feedback to me if you utilize these scripts, I will also entertain requests for any enhancements as long as they are within the scope of the *AMP install procedure.
Thanks for reading.