Beruflich Dokumente
Kultur Dokumente
1, and
MySQL on Ubuntu with Vagrant
By Tania Rascia / 38 responses / July 3, 2017
GitHub Twitter
Install the latest versions of Apache, PHP, and MySQL on an Ubuntu virtual machine,
and access the server and database through your local computer.
In order to run a server locally, we need to set up an environment for the stack, or server,
database, and language, to run in. An easy way for beginners to do that is to set up MAMP on
Mac or Windows, which sets up the environment for you.
As you become a more advanced developer, you might need to create a local environment that
matches your production server more closely. If you’re working locally on Windows or Mac, and
your server is on a Linux distribution, you may encounter bugs and errors that are specific to the
operating system the server is running and you won’t be able to troubleshoot them properly.
Fortunately, we have tools like Vagrant and VirtualBox that allow us to set up a virtual
machine locally that can match the production environment. In this tutorial, we’ll take an Ubuntu
Server operating system that has been set up on Vagrant and install a LAMP stack on it.
Prerequisites
Knowledge of the command line. Please read How to Use the Command Line to get
yourself acquainted if you’re not already familiar.
A Vagrant/VirtualBox setup with an Ubuntu virtual machine. You will find
instructions on how to do that in this guide to setting up VirtualBox and Vagrant with
Ubuntu Server.
Goals
vagrant ssh
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.
ServerName localhost
Save and close nano (cntl + o, cntl + x), then restart Apache to apply the changes.
/Users/taniarascia/Dev/VirtualMachines/ubuntu
Within this 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.
Find the “private_network” configuration line, which should look like this.
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.
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.
php -v
PHP 7.1.6-2~ubuntu14.04.1+deb.sury.org+1 (cli) (built: Jun 14 2017
05:58:40) ( 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
In order to do this, we’re going to need to link a folder on our local computer to one on the
virtual machine.
Right now, the Ubuntu default page exists on /var/www/html, the public facing folder on an
Apache server. Back in our local (Windows or Mac) computer, let’s create a www 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.
sudo mysql_secure_installation
As far as I can tell, that’s a lie. When it came to this point, I just put “root” as the password.
I found some good directions on configuring MySQL with Vagrant here. Edit your MySQL
configuration file.
mysql -u root -p
I found the easiest way to setup MySQL to allow entry via SequelPro or another 3rd party is to
set up a new user. I encountered issues connecting with root, and found some solutions on
Stack Overflow.
While inside the command line mysql, create a new user and password. I literally used “user”
and “password” for those fields.
FLUSH PRIVILEGES;
Now to restart Apache and MySQL once again.
Everything should be set from the Ubuntu side for MySQL setup. We’re going to need to do a
few things to connect to MySQL via SequelPro now.
First, edit the VagrantFile and add this line to recognize the MySQL port, 3306.
vagrant reload
Now open up SequelPro, or your database program (SQLYog for Windows) to connect.
We’ll need to use the SSH tab of the connection. There are two connections we’ll need to make
– MySQL and SSH.
Host: 192.168.33.10
Username: user
Password: password
Port: 3306
Host: 127.0.0.1
User: vagrant
Key: ~/.vagrant.d/insecure_private_key
Port: 2222
In your home folder, there will be a hidden directory called .vagrant.d. Inside is a file
called insecure_private_key, which is the key the vagrant ssh command uses to
connect.
Now if everything went well, we’ll be in! If you had any issues up to this point, please let me
know and I’ll see if I can make it more clear.
Since I’m in, I’m just going to create a database called test and a table called table1 so I can
test MySQL via 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';
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.