Allow Linux in VirtualBox to connect to the localhost of the host machine

In this article I’ll show you how to set up a Linux running in VirtualBox to be able to access a service running on localhost on the host machine. I’ll use MySQL as an example, because that was the problem I faced with. The Linux OS of my choice is Ubuntu.

Testing the connection

First, let’s see how you can test if your virtual machine can access the service on the host. There is a great program called nmap for it. If it is not installed, you can install it with the following commands:

sudo apt-get update
sudo apt-get install nmap

Let’s suppose we have a web application that is trying to connect to the MySQL server on localhost:3306 by default. We would not like to change this configuration, but allow our virtual linux to connect to the MySQL server running on the host.

So, first let’s check if we can connect to MySQL:

nmap -p 3306 localhost

You could see something like this, indicating that it cannot connect:

Starting Nmap 7.01 ( ) at 2016-09-18 19:12 CEST
Nmap scan report for localhost (
Host is up (0.000053s latency).
3306/tcp closed mysql

You should see an “open” STATE in the output instead of “closed”. Let’s try to resolve that.

Map localhost in the hosts file

We would like for our virtual linux to map localhost calls to the host machine. For this, we have to determine the default gateway of our machine.

netstat -rn
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface         UG        0 0          0 enp0s3   U         0 0          0 enp0s3     U         0 0          0 enp0s3

Check the Gateway in the line. That IP address is what we need.

Open /etc/hosts and comment out the default line for localhost mapping and add a new one like this:

# localhost  localhost

After saving the file, and running nmap again, you should see that the STATE of the port is “open”.



How to temporarily disable foreign key checks in MySQL?

Usually it is not a good idea to disable your foreign keys because they make sure that the integrity of the data is maintained. But when you would like to drop some or all of your tables, the presence of foreign keys could pervent that.

To get around this problem you just need to temporarily disable the foreign keys before running the DROP statements and enable them after the DROPs are completed.

To disable foreign key checks:


To enable foreign key checks:


Putting it all together with some DROP statements:


DROP TABLE messages;


To make sure you don’t forget to switch it back on I recommend running the disable and enable statements together, as you can see in the above example.

How to install PHP 5.4, Apache 2.4, MySQL 5.6.19 and phpMyAdmin 4.2 on Windows 7

In this tutorial I am going to show you how to install PHP 5.4.30, Apache 2.4.9, MySQL 5.6.19 and phpMyAdmin 4.2.2 on a Windows 7 machine. This is a standard set of software that is required for PHP development.

When you read this tutorial it is quite likely that there are newer versions available of the software I used. In most cases if you download the latest version it shouldn’t change the installation procedure…but you can never know, so if you run into problems, read up on possible solutions or try to use the versions listed here and see if it helps.

Installing Apache

You can download the latest 2.4 Apache for Windows from this site: You will also need the VC11 Visual C++ Redistributable for Visual Studio 2012. You can download it from here:

First install the  VC11 Visual C++ Redistributable for Visual Studio 2012, then download and extract the Apache web server. The extracted folder contains a folder named Apache24, copy this where you want Apache to reside. I’ll have it under the following path: C:/Software/Apache24.

Next, we need to register Apache as a service. In your command line (open it as an Administrator) navigate to the bin directory of your Apache installation. In my case it is: C:/Software/Apache24/bin, and issue the following command:

httpd -k install

The -k option tell Apache to start the service immediately after installing it. If the installation is successful, you will recieve an output similar to the following:

Installing the Apache2.4 service
The Apache2.4 service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
httpd: Syntax error on line 37 of C:/Software/Apache24/conf/httpd.conf: ServerRoot must be a valid directory

As you can see it notifies you that the ServerRoot must be a valid directory. This is because by default the configuration specifies c:/Apache24 as the Apache installation directory. Open the httpd.conf file in your conf directory and replace all occurrences of c:/Apache24 with the path to your actual installation directory.

Run the services.msc command to see if Apache is really installed. You should see Apache2.4 in the list of services. Apache service installed


Remember that we recieved an error when we installed it so it couldn’t start then. Right click on the Apache service and start it.

Type localhost or in your browser and you should see a blank page with an “It works!” message. This HTML page is located in the htdocs directory. It is served by the Apache web server, so it works correctly. Although, currently it is only capable of serving static content.

Installing PHP

You can download PHP for Windows from this site: Choose the latest 5.4 thread safe version that is available. At the time of writing the latest 5.4 release is 5.4.30. I know that 5.5 has been around for a long time, but I have come across a couple of hosting providers where the highest available version is 5.4 so for compatibility reasons I am sticking with that.

Download PHP, extract it, and move it to the desired location. I moved PHP to the C:/Software/php-5.4.30 directory. In the root of this directory find the php.ini-development file and rename it to php.ini, this will contain the configuration of PHP.

The next thing to do is to enable Apache the serving of PHP pages. For this, add the following lines to the end of your httpd.conf file:

#Configure the PHP module
LoadModule php5_module "C:/Software/php-5.4.30/php5apache2_4.dll"
AddHandler application/x-httpd-php .php

#Configure the path to php.ini
PHPIniDir "C:/Software/php-5.4.30"

Change the paths according to the directory structure you have. Apache needs a module to support PHP pages, this is why we load php5apache2_4.dll, it is in the root of your PHP installation directory.

If you don’t find the dll, then you probably donwloaded the not thread safe version of PHP.

While we are at it, we  configure Apache to use index.html and index.php as welcome pages. This means that if you point your browser to a directory that contains one of these files, Apache will automatically display it rather then showing the directory structure. This requires the following config:

DirectoryIndex index.html index.php

To try out if PHP works, rename the index.html located in the htdocs directory to index.php and place some PHP code in it. Then open localhost in your browser. If all went well, you should see the result of you interpreted PHP code.

I used the following php code:

    <?php phpinfo(); ?>

And this is what I see:

Installing MySQL

Download the MySQL Community Server. You can download the latest version (at the time of writing 5.6.19) from this site:

Installing MySQL is a pretty straightforward process, just download the Windows installer and follow the steps. After the installation is completed the MySQL server instance configuration wizard opens.

Choose the Detailed configuration, but just accept most of the default settings. The only changes you should make is to set the character encoding to utf8 and provide a password.

At the end of the wizard, the MySQL service will be registered and started automatically.

Checking the MySQL connection

To check the connection I suggest you to install  MySQL Workbench. This is a great tool for administering your databases and designing your database schemas.

To test your MySQL server, create a new connection:

  • Hostname:
  • Port: 3306
  • Username: root

Then click on Test Connection and type in the password you provided during installation.





Download the latest version from here: You can check on the download page if it is compatible with the PHP version you are using.

Extract the downloaded archive, rename the folder to phpmyadmin and copy it to the htdocs directory. Type localhost/phpmyadmin/setup/index.php in your browser. You should see the following error message:

Fatal error: Call to undefined function mb_detect_encoding() in 
C:\Software\Apache24\htdocs\phpmyadmin\libraries\php-gettext\ on line 177

To fix this open php.ini and perform the following changes:

  • Set your extension_dir to the correct path according to your PHP installation. In my case it is: extension_dir = “C:/Software/php-5.4.30/ext”
  • Enable the php_mbstring.dll extension by uncommenting it.

If you restart your Apache, your should now be able to load the page. However, there are still two errors displayed:

Bzip2 Bzip2 compression and decompression requires functions (bzopen, bzcompress) which are unavailable on this system. 
Cannot load or save configuration Please create web server writable folder config in phpMyAdmin top level directory as described in documentation. 
Otherwise you will be only able to download or display it.

To solve the first one, enable the php_bz2.dll extension in your php.ini and restart Apache. For the second one, create a directory called “config” in your phpmyadmin folder. If you refresh your browser, the errors are gone. Leave everything on default and open localhost/phpmyadmin. There you can see another error 🙂

The mysqli|mysql extension is missing.

There is a quick fix for this also. Enable the php_mysql.dll and php_mysqli.dll extensions and restart Apache. Now phpmyadmin should work correctly.

This completes our setup.


As you can see, installing these software seperately involves a few gotchas, but leaves you with complete control over your development environment. You can easily swich any of the components to newer versions at any time.

If you would rather like a quicker installation, then check out WAMP or XAMPP. They are ready made packages containing all of the software we have just installed but you will have much less control over the individual components.