LAMP Server (Vagrant)

References


Files


$ tree vagrant/
vagrant/
├── Vagrantfile
├── install
│   └── lamp.sh
└── src
    └── index.php

2 directories, 3 files

Vagrantfile


Vagrantfile:

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

Vagrant.configure("2") do |config|
    config.vm.hostname = "lamp"
    config.vm.box = "ubuntu/bionic64"
    config.vm.network "forwarded_port", guest: 80, host: 8080
    config.vm.network "forwarded_port", guest: 3306, host: 3306
    config.vm.synced_folder "./src/", "/var/www/html/" #, mount_options: ["dmode=775,fmode=777"]
    config.vm.provider "virtualbox" do |vb|
      vb.name = "lamp"
    end
    config.vm.provision "shell", path: "install/lamp.sh"
end

Provision


install/lamp.sh:

echo "update & upgrade"
sudo apt update -y > /dev/null
sudo apt upgrade -y > /dev/null

echo "install apache2"
sudo apt install apache2 -y > /dev/null
sudo rm /var/www/html/index.html > /dev/null

echo "install php7.2"
sudo apt install php7.2 php7.2-mysql php7.2-mbstring libssh2-1 php-ssh2 -y > /dev/null
sudo sed -i -r -e 's/display_errors = Off/display_errors = On/g' /etc/php/7.2/apache2/php.ini
sudo systemctl restart apache2 > /dev/null

echo "Installing MySQL"
DBPASSWD=secret
echo "mysql-server mysql-server/root_password password $DBPASSWD" | sudo debconf-set-selections  > /dev/null
echo "mysql-server mysql-server/root_password_again password $DBPASSWD" | sudo debconf-set-selections  > /dev/null
sudo apt-get -y install mysql-server  > /dev/null
sudo sed -i -r -e 's/127.0.0.1/0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo systemctl restart mysql > /dev/null
mysql -uroot -p"$DBPASSWD" -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$DBPASSWD' REQUIRE NONE WITH GRANT OPTION; FLUSH PRIVILEGES;"

echo "Vagrant finish"

Creating LAMP


$ cd vagrant

$ vagrant up

$ vagrant status

$ curl -i http://localhost:8080/

$ vagrant suspend

Checking PHP


$ vagrant ssh

$ php -i

$ php -m

Connecting to VM via SSH and running PHP code (Interactive shell)


$ vagrant ssh
$ php -a
Interactive shell

php > $x = 10;
php > echo $x;
10
php > exit

Connecting to VM via SSH and running PHP code (File)


$ vagrant ssh
$ echo '<?php echo 'Hello world!'; ?>' > hello.php
$ php -f hello.php
Hello world!

Vagrant commands