How to Install Apache, PHP 7.1, and MySQL on Ubuntu with Vagrant

Install Apache, PHP 7.1, In order to run a server regionally, we want to installation an environment for the stack, or server, database, and language, to run in.

An smooth manner for beginners to do this is to set up MAMP on Mac or Windows, which units up the environment for you.

As you emerge as a greater advanced developer, you would possibly need to create a nearby environment that fits your production server greater carefully.

If you are operating domestically on Windows or Mac, and your server is on a Linux distribution, you can stumble upon insects and mistakes which can be precise to the working gadget the server is going for walks and also you may not be capable of troubleshoot them properly.

Fortunately, we have equipment like Vagrant and VirtualBox that allow us to set up a digital system locally that could match the manufacturing surroundings.

In this educational, we’ll take an Ubuntu Server running gadget that has been set up on Vagrant and deploy a LAMP stack on it.

Prerequisites

There are conditions to finish before analyzing this academic.

  • Knowledge of the command line. Please study How to Use the Command Line to get your self familiar in case you’re no longer already familiar.
  • A Vagrant/VirtualBox setup with an Ubuntu digital machine. You will discover instructions on how to do that on this guide to putting in VirtualBox and Vagrant with Ubuntu Server.
  •  

Goals

In this academic, we are going to accomplish the following:

  • Install LAMP (Apache, PHP 7.1, MySQL) on an Ubuntu Server 14.04 digital device
  • View the server in a browser through IP cope with and custom area call
  • Link your local files with the virtual system for you to increase at the Vagrant surroundings out of your neighborhood computer
  • Connect to MySQL via SequelPro (or any database program)

Step 1 – Install Apache

The examples in this text use the .dev suffix for custom domain names. This suffix is not allowed. You can use .test in place of .dev for the duration of this article.

We’re starting this text wherein the ultimate one left off – with a modern day Ubuntu digital system installation. Navigate to the directory you’ve got your Ubuntu installation in, and ssh into the gadget.

vagrant ssh

Apt-get is command line software for installing packages on Ubuntu.

Let’s run the instructions to replace and improve the package deal manager and make certain everything is up to date.

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

Now we’re going to install the latest version of Apache, which is 2.4.7 at the time of this writing.

sudo apt-get install apache2 -y

After installing it you’ll get this 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

Easily constant. We’re simply going to set the ServerName to localhost. Use nano (or vi, or something you experience relaxed with) to edit the apache2 configuration document.

sudo nano /etc/apache2/apache2.conf

If you are no longer acquainted with nano, or enhancing files via the Terminal, please overview the command line article.

Navigate all the way to the end of the file, and insert this line at the bottom.

ServerName localhost

Save and close nano (cntl + o, cntl + x), then restart Apache to apply the changes.

sudo service apache2 restart
* Restarting web server apache2 [ OK ]

You can check to make sure the ServerName is correct with the following command.

sudo apache2ctl configtest
Syntax OK

I always like to check the version number to see exactly what I’m running.

apache2 -v
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Jun 26 2017

Great, Apache is up and running properly! But now we want confirmation of this.

Step 2 – Map a local domain to the IP and view the server

Now, typically whilst you set up a manufacturing server, you can navigate to the IP cope with or area call of the server for your browser and notice both the “New Linux installation” page, or your website, in case you’ve already delivered it.

We’re going to do the same component right now, except instead of connecting to a public facing server, we’re connecting to our personal local digital system.

I’m strolling my virtual device inside the following directory.

/Users/taniarascia/Dev/VirtualMachines/ubuntu

Within this directory, you may see documents – .Vagrant, and Vagrantfile. Open your Vagrantfile, as this is the neighborhood configuration file we’re going to need to edit to make any adjustments.

Find the “private_network” configuration line, which should look like this.

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

And uncomment it.

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

Reboot Ubuntu to make sure the Vagrantfile changes take effect. Run the reload command from back in your local computer’s 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

Add a new domain to the bottom with the IP address we were given. I’m calling it trusty.dev.

192.168.33.10  trusty.dev

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!

You can also navigate to 192.168.33.10 in the browser now, and it will work as well.

Step 3 – Install PHP 7.1

bAt this factor, we are going to set up PHP 7.1. Since 7.1 is new and cutting part, it isn’t always to be had within the normal apt repository.

We’re going to use a PPA (Personal Package Archive) to put in this non-standard software.

Add the Onrej PPA on your device.

sudo apt-add-repository ppa:ondrej/php

Update apt-get with the software from the new repository.

sudo apt-get update

Now you will be able to install PHP 7.1.

sudo apt-get install php7.1

Confirm successful installation of PHP by checking the version.

php -v
PHP 7.1.6-2~ubuntu14.04.1+deb.sury.org+1 (cli) (built: Jun 14 2017) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.6-2~ubuntu14.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

Okay, using nano or vi to edit documents goes to begin getting tedious. I want with a view to edit the documents within the virtual device from within the consolation of my personal pc’s environment (currently using Visual Studio Code as a textual content editor).

In order to do that, we’re going to need to hyperlink a folder on our nearby laptop to 1 on the virtual system.

Right now, the Ubuntu default page exists on /var/www/html, the public going through folder on an Apache server.

Back in our neighborhood (Windows or Mac) pc, permit’s create a www folder in the root directory, and create two documents: join.Php and check.Hypertext Preprocessor, which we will use to check MySQL and PHP in a second.

Here is how our local listing shape should be set up:

Let’s go lower back to our Vagrantfile. We’re going to set up a synced folder, first inputting our nearby direction, then inputting our virtual system’s route.

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

We’ll set the www folder we simply made as the nearby folder, and the public server directory for Ubuntu.

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

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

Success! Your local folder has now synced with the Ubuntu server, and every change you make there will be reflected in the server.

Step 5 – Install MySQL

Finally, we’re going to install MySQL for database access.

Installation within Ubuntu

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

You also can set up the mysql comfortable installation. Follow the commands at the web page.

Since we are doing a nearby environment, it doesn’t need to be as cozy as a manufacturing surroundings.

You can go away the ports open for us to get admission to later.

sudo mysql_secure_installation

As some distance as I can tell, it is a lie. When it came to this point, I simply placed “root” as the password.

I located some right directions on configuring MySQL with Vagrant right here. Edit your MySQL configuration file.

sudo nano /etc/mysql/my.cnf

Find skip-external-locking and bind-address and remark them out.

skip-external-locking
bind-address 0.0.0.0

Once it really is all performed, I restart Apache and MySQL.

sudo service apache2 restart
sudo service mysql restart

At this point, you should be able to login to MySQL with root, using password root.

mysql -u root -p

I determined the very best manner to setup MySQL to allow entry through SequelPro or any other 3rd birthday party is to installation a brand new person.

I encountered problems connecting with root, and determined a few solutions on Stack Overflow.

While in the command line mysql, create a new consumer and password. I literally used “consumer” and “password” for the ones fields.

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

Granted all privileges to the new user.

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

And flushed privileges.

FLUSH PRIVILEGES;

Now to restart Apache and MySQL once again.

sudo service apache2 restart
sudo service mysql restart

Install Apache, PHP 7.1, Local setup to access MySQL

Everything must be set from the Ubuntu side for MySQL setup. We’re going to need to do a few matters to connect with MySQL via SequelPro now.

First, edit the VagrantFile and upload this line to recognize the MySQL port, 3306.

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

You’ll need to restart Vagrant for this to take effect.

vagrant reload

Now open up SequelPro, or your database application (SQLYog for Windows) to connect.

Install Apache, PHP 7.1, We’ll want to apply the SSH tab of the connection. There are two connections we’ll need to make – MySQL and SSH.

For MySQL, the credentials might be:

  • Host: 192.168.33.10
  • Username: user
  • Password: password
  • Port: 3306

For SSH, they will be:

  • Host: 127.Zero.0.1
  • User: vagrant
  • Key: ~/.Vagrant.D/insecureprivatekey
  • Port: 2222
In your house folder, there may be a hidden listing referred to as .Vagrant.D. Inside is a record referred to as insecure_private_key, that is the key the vagrant ssh command uses to attach.

Running vagrant ssh is similar to walking the subsequent:

ssh -i ~/.vagrant.d/insecure_private_key [email protected]

Inputting all the following credentials, we will be prompted for a password.

Install Apache, PHP 7.1, The password is “vagrant”

Update three/five/2018: This key may not work in more moderen variations of Ubuntu. Instead, you will run vagrant ssh-config out of your neighborhood laptop, and observe the record course for IdentityFile. Mine changed into /Users/mypath/ubuntu/.Vagrant/machines/default/virtualbox/privatekey. Use this rather than ~/.Vagrant.D/insecureprivatekey.

Now if the whole lot went properly, we’re going to be in! If you had any issues up thus far, please allow me recognize and I’ll see if I could make it greater clear.

Install Apache, PHP 7.1, Since I’m in, I’m just going to create a database known as check and a desk called table1 so I can take a look at MySQL via PHP.

Step 6 – Testing PHP and MySQL

Using the connect.Hypertext Preprocessor and take a look at.Hypertext Preprocessor documents we made in /var/www/html earlier than, permit’s test PHP and MySQL from the browser.

In take a look at.Hypertext Preprocessor, we’re going to use the phpinfo() feature to test PHP.

<?php phpinfo();

In connect.php, I used a quick MySQL test script to make sure everything is connecting.

<?php

$dbname = 'test';
$dbuser = 'user';
$dbpass = 'password';
$dbhost = '192.168.33.10';

$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";
}

And I can see that MySQL is connecting properly.