Tuesday, March 10, 2015

How to setup a LAMP development environment in Vagrant

https://www.howtoforge.com/tutorial/vagrant-ubuntu-linux-apache-mysql-php-lamp

In the previous tutorial, we have learned about the basics of Vagrant. In this tutorial, we will move to next level and will learn more about Vagrant and setting up LAMP development environment in Vagrant.
To start with, LAMP stands for: Linux (OS/Kernel), Apache (Web Server), MySQL (Database), PHP (Scripting Language). It is an open-source Web development environment which lets you create web applications. It is generally also referred as Web Stack.
Let us now setup LAMP development environment in Vagrant with below steps:
#1- Create a directory where we would be creating the instance:
mkdir -p ~/Vagrant/lamp
cd  ~/Vagrant/lamp
#2- Now we are required to initialize the Vagrant box. Here we are left with two options.
#2.1- One, we can re-use Ubuntu 12.04 LTS (Precise Pangolin) box, which we have already downloaded in the previous tutorial. To use this, just do:
vagrant init precise32
#2.2- Second option we have is to do a fresh addition of the box by downloading the Ubuntu 12.04 LTS (Precise Pangolin) Vagrant box, as:
vagrant box add precise32 http://files.vagrantup.com/precise32.box
And then do:
vagrant init precise32
Please note- #2.2 is only for those who have not downloaded the Ubuntu 12.04 LTS (Precise Pangolin) Vagrant box previously.
#3- Now do:
vagrant up
This will create a VagrantFile. Edit the VagrantFile as below:
Vagrant.configure(2) do |config|
config.vm.box = "precise32"
# Mentioning the SSH Username/Password:
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
# Begin Configuring
config.vm.define "lamp" do|lamp|
lamp.vm.hostname = "lamp" # Setting up hostname
lamp.vm.network "private_network", ip: "192.168.205.10" # Setting up machine's IP Address
lamp.vm.provision :shell, path: "script.sh" # Provisioning with script.sh
end
# End Configuring
end
Please note- I have deleted the commented lines to avoid confusion.
You can download this file from here.
#4- Let us now start provisioning the LAMP installation. For that, create a simple shell script named script.sh using your favorite text editor as:
#!/bin/bash

# Updating repository

sudo apt-get -y update

# Installing Apache

sudo apt-get -y install apache2

# Installing MySQL and it's dependencies, Also, setting up root password for MySQL as it will prompt to enter the password during installation

sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password rootpass'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password rootpass'
sudo apt-get -y install mysql-server libapache2-mod-auth-mysql php5-mysql

# Installing PHP and it's dependencies
sudo apt-get -y install php5 libapache2-mod-php5 php5-mcrypt

You can download this file from here.

#5- After saving the script.sh, run:
vagrant up

It will do lot of things. You would be able to see as what it is doing on the stdout. To give you an overview, it will start with importing the precise32 base box, then it will SSH into the box, then it will set the hostname of the machine(we have specified this in the Vagrantfile), then it start updating and installing the LAMP stack.

Please note- While installing mysql-server, it will set it's root password to 'rootpass' since we have mentioned this in the script.sh file.

#6- Now after the vagrant is done with installation, you are ready to go. SSH into the vagrant box as:
vagrant ssh

#7- Verify the installations by:
dpkg -l | grep "apache2\|mysql-server-5.5\|php5"

You'll see all these packages listed and this means they have been installed successfully. With this you're done with setting up the LAMP development environment in Vagrant :)

Tip- You can now save this box to it's current state(Ubuntu 12.04 LTS with LAMP) as:

Step 1- Come out of Vagrant box by issuing:
exit
Step 2- Package this box with:
vagrant package --output ubuntu1204_LAMP.box
This will create a ubuntu1204_LAMP.box file which you can re-use as a base box with LAMP already installed, by default.
To keep things organized, create a directory where you gonna store all such boxes and move this ubuntu1204_LAMP.box into it:
mkdir -p ~/Vagrant/boxes
mv ubuntu1204_LAMP.box ~/Vagrant/boxes
You can anytime import and initialize this box by:
vagrant init ubuntu1204_LAMP file:~/Vagrant/boxes/ubuntu1204_LAMP.box
Author: Sanchit Jain Rasiya

No comments:

Post a Comment