Vous êtes sur la page 1sur 3

LOCAL ENVIRONMENT SETUP with VAGRANT

(alternative: Docker for Developers by Chris Tankersley)

https://www.taniarascia.com/what-are-vagrant-and-virtualbox-and-how-do-i-use-them/

- download & install VirtualBox (https://www.virtualbox.org/wiki/Downloads)

- download & install Vagrant (https://www.vagrantup.com/downloads.html)

- install Ubuntu vagrant box inside the folder in C:\Users\Tiberiu\VirtualBox VMs (vagrant box add
ubuntu/trusty64) (just once)

- vagrant init ubuntu/trusty64 (just once)

- vagrant up (every time you start pc)

- vagrant ssh (every time you start pc)

To start the machine go to the folder where the vagrantfile lives (C:\Users\Tiberiu\VirtualBox VMs)
and use the last 2 commands (up and ssh).

- use vagrant halt to shutdown the machine

- to see machine in browser, you need to give the Vagrant box a static IP address: create a private
network, which allows host-only access to the machine (edit Vagrantfile and uncomment

# config.vm.network "private_network", ip: "192.168.33.10", change ip to whatever you want,


vagrant reload) / Optionally map custom domain name to the ip in hosts file
(C:\Windows\System32\drivers\etc\hosts), to use name in browser, instead of IP.

INSTALL APACHE, PHP7, MYSQL ON UBUNTU MACHINE

(https://www.taniarascia.com/how-to-install-apache-php-7-1-and-mysql-on-ubuntu-with-vagrant/)

APACHE:

- sudo apt-get update && sudo apt-get upgrade

- sudo apt-get install apache2 -y

- sudo nano /etc/apache2/apache2.conf

This is done to fix “server's fully qualified domain name” error. Go to end of file and paste
“ServerName localhost”, then save and close file(ctrl-x, yes);

- sudo service apache2 restart

- sudo apache2ctl configtest (syntax OK)

PHP:

- sudo apt-add-repository ppa:ondrej/php

- sudo apt-get update

- sudo apt-get install php7.1

- php -v
MYSQL:

- sudo apt-get install mysql-server php7.1-mysql (leave root password blank, it’s easier)

CONNECT VM’S MYSQL TO LOCAL WORKBENCH:

- forward the default MySQL port 3306 on your vm (add this line to vagrantfile):

config.vm.network "forwarded_port", guest: 3306, host: 3306

- create user and give all privileges (to be used by workbench):

CREATE USER 'user'@'%' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

sudo service apache2 restart

sudo service mysql restart

- open workbench and create a new connection(standard tcp/ip over ssh), the info below comes from
vagrant ssh-config command:

SSH Hostname: 127.0.0.1:2222

SSH Username: ubuntu

SSH Key File: …

MySQL Hostname: 127.0.0.1

MySQL Server Port: 3306

Username: user

Password: password

LINK LOCAL FOLDER TO VM FOLDER

In the Vagrant root directory(C:\Users\Tiberiu\VirtualBox VMs) create a www directory. Sync this
directory with the /var/www/html folder in VM, by modifying this line in vagrantfile:

config.vm.synced_folder "LOCAL", "VIRTUAL"

with this:

config.vm.synced_folder "www/", "/var/www/html"

Then, vagrant reload.

- check setup: create connection.php


<?php $dbname = 'test'; $dbuser = 'user'; $dbpass = 'password'; $dbhost = '127.0.0.1'; $link =
mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); mysqli_select_db($link,
$dbname) or die("Could not open the db '$dbname'"); $test_query = "SHOW TABLES FROM $dbname"; $result =
mysqli_query($link, $test_query); $tblCnt = 0; while($tbl = mysqli_fetch_array($result)) { $tblCnt++; } if (!$tblCnt)
{ echo "There are no tables<br />\n"; } else { echo "There are $tblCnt tables<br />\n"; }

create test.php <?php echo phpinfo();

Vous aimerez peut-être aussi