Vagrant – setup LAMP container

Install Vagrant and VirtualBox.

Go to https://app.vagrantup.com/boxes
and choose vagrant box that suits you best.
After that you need to create vagrant file by typing in console:

vagrant init ubuntu/xenial64

– ubuntu/xenial64 – stands for the name of the box you selected

You will notice that file was created in your folder. Open it and replace it with this.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  # Box Settings
  config.vm.box = "ubuntu/xenial64"

  # Provider Settings
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048
    vb.cpus = 2
  end

  # Network Settings
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  config.vm.network "private_network", ip: "192.168.33.10"
  # config.vm.network "public_network"

  # Folder Settings
  config.vm.synced_folder "../aqualife", "/var/www/html/", :nfs => { :mount_options => ["dmode=777","fmode=666"] }

  # Provision Settings
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

– aqualife – is the name of the folder in localmachine that will be synced with virtualBox.

Go back to console and write:

vagrant up

There are useful vagrant commands like
-vagrant destroy (will delete the box)
-vagrant suspend (will suspend the virtualBox)
-vagrant resume (will resume the virtualBox)
-vagrant reload (will reload the virtualBox)
-vagrant ssh (will shh to container)

To shh to your container type:

vagrant ssh

So we are in!!!Now we need to update repositories and install LAMP.

sudo apt update
sudo apt install -y git
sudo apt install -y apache2
a2enmod rewrite
sudo apt-add-repository ppa:ondrej/php
sudo apt update
sudo apt install -y php7.2
sudo apt install -y libapache2-mod-php7.2
sudo service apache2 restart
sudo apt install -y php7.2-common
sudo apt install -y php7.2-mcrypt
sudo apt install -y php7.2-zip
debconf-set-selections <<< 'mysql-server mysql-server/root_password root'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again root'
sudo apt install -y mysql-server
sudo apt install -y php7.2-mysql
sudo apt install -y php-xml
sudo service apache2 restart

Ideal will be to insert these commands (without sudo) in vagrant file between “config.vm.provision “shell”, inline: <<-SHELL" and "SHELL". So when we start the vagrant these command to be executed and LAMP stack to be ready for use. To make it possible to open the apache server outside the container, you need to add the private network IP from configuration file to /etc/hosts in your local machine like this:

192.168.33.10  vgdemo.local www.vgdemo.local

Now from browser you can open vgdemo.local and continue working the project.

Leave a Reply

Your email address will not be published. Required fields are marked *