gary
1/10/2018 - 10:42 PM

How to setup a Vagrant Project.

How to setup a Vagrant Project.

Setup Vagrant

Change to client / project folder

vagrant box add ubuntu/trusty64

Now initialize the new vagrant in your VirtualMachines/ubuntu directory with the init command.

vagrant init ubuntu/trusty64

Finally, get Ubuntu up and running with the vagrant up command.

vagrant up

Fixing Guest Additions Error

vagrant halt

Let’s quickly install a plugin called vbguest to fix this error.

vagrant plugin install vagrant-vbguest

Let’s reload the Vagrant machine with the reload command.

vagrant reload

SSH into ubuntu

vagrant ssh

You’re in! You now have a complete Ubuntu Server installation running through VirtualBox on your computer, and you’re connected to it. Type exit at any time to exit out of the server, and vagrant halt to shut it down.

Apache

apt-get is command line software for installing packages on Ubuntu. Let’s run the commands to update and upgrade the package manager and make sure everything is up to date.

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install apache2 -y

Fix Warning: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppress this message

sudo nano /etc/apache2/apache2.conf

At the end of the file add

ServerName localhost

Restart apache

sudo service apache2 restart

Test apache config

sudo apache2ctl configtest

Now, usually when you set up a production server, you can navigate to the IP address or domain name of the server in your browser and see either the “New Linux installation” page, or your website, if you’ve already added it. We’re going to do the same thing right now, except instead of connecting to a public facing server, we’re connecting to our own local virtual machine.

Within the client / project directory, you’ll see two files – .vagrant, and Vagrantfile. Open your Vagrantfile, as this is the local configuration file we’ll need to edit to make any changes.

Uncomment

config.vm.network "private_network", ip: "192.168.33.10"

Reload Vagrant in the local terminal

vagrant reload

We’re going to make a custom domain to access this address and view it in our browser. Still in your local computer, edit your local hosts file.

sudo nano /etc/hosts
192.168.33.10  trusty.loc

Save and exit nano. Now in Google Chrome or whatever browser you’re using, go to trusty.dev. Now you should see the default Linux success page. It works!

PHP

log back into the box

vagrant ssh

Add the Onrej PPA to your machine.

sudo apt-add-repository ppa:ondrej/php

update

sudo apt-get update

install php 7.1

sudo apt-get install php7.1

Confirm php

php -v

Link a local folder with the virtual machine

Right now, the Ubuntu default page exists on /var/www/html

Back in our local (Windows or Mac) computer, let’s create a public_html folder in the root directory, and create two files: connect.php and test.php, which we’ll use to test MySQL and PHP in a moment.

Let’s go back to our Vagrantfile. We’re going to set up a synced folder, first inputting our local path, then inputting our virtual machine’s path.

#config.vm.synced_folder "LOCAL", "VIRTUAL"
config.vm.synced_folder "public_html", "/var/www/html"

Now vagrant reload, or vagrant halt and vagrant up to restart Ubuntu. Now when you go to your server you should see this.

Mysql

sudo apt-get install mysql-server php7.1-mysql
sudo mysql_secure_installation

Enter Password i.e.

root
sudo nano /etc/mysql/my.cnf

Connect to mysql via ssh

http://prntscr.com/hyrg4o

Restart apache

sudo service apache2 restart

Restart Mysql

sudo service mysql restart

Login to test mysql

mysql -u root -p

grant on mysql

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Testing PHP and MySQL

in test.php

<?php phpinfo();

in connect.php

<?php

$dbname = 'test';
$dbuser = 'root';
$dbpass = 'root';
$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";
}