Setup a LAMP Stack with Mac/VirtualBox/Ubuntu - Part 2 of 4 - Setup LAMP Stack

1st Oct, 2016 | Environments | Read Time: 4 mins

In the first article, you learned how to install Ubuntu in VirtualBox. In this second article, we will complete the steps necessary for you to actually build web applications by installing a LAMP stack with Apache, MySQL and PHP.

Table of Contents

Install Apache

This entire process will be done from the command line, so go ahead and open up the Terminal.

Ubuntu Terminal

Let's emulate 'superuser' so we don't have to keep typing 'sudo' before every command:

sudo su

Make sure we have the latest software updates:

apt update

Now we can install Apache:

apt install apache2

Once installed, enable Apache's mod rewrite:

a2enmod rewrite

Install MySQL 5.7

apt install mysql-server mysql-client

You can leave the password blank each of the 3 times you are prompted.

As of MySQL v5.7, you can't use 'root' without sudo. So all web applications will require a different user. We'll create a new user called 'user' with a password of 'password'... easy enough to remember, right? This will be the credentials to use when connection your web applications to a database. Go ahead and access MySQL as root:

mysql -uroot

Now we'll create that new MySQL user:

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

Install PHP 7

The following command will install PHP 7 along with the latest versions of some of the more common PHP modules:

apt-get install php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-mysql php7.0-curl php7.0-json php7.0-cgi php7.0-fpm php7.0-mcrypt php7.0-gd php-imagick php-memcache php-memcached php-mbstring php7.0-mbstring php-gettext

Once installed, enable the most common PHP modules:

phpenmod mcrypt mbstring curl gd imagick

Restart Apache

systemctl restart apache2

Prepare the WWW Folder

Now that we have the LAMP stack set up, we need to prepare the WWW directory by setting ownership to your Ubuntu user (mydev).

First, modify Apache's environment variables:

nano /etc/apache2/envvars

Find the following lines:

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

and change them to:

export APACHE_RUN_USER=mydev
export APACHE_RUN_GROUP=mydev

then save and exit.

Next we'll change the ownership of the WWW directory, modify existing user data from the original 'www-data' user to 'mydev', and recursively set the group to 'mydev':

chown -R mydev:mydev /var/www
usermod -a -G www-data mydev
chmod g+s /var/www

Finally, restart Apache again:

systemctl restart apache2

If everything went right, you should now be able to open up your browser and visit http://localhost or and see the default web page.

Default Localhost

This article will not go into detail about setting up new individual web applications... I'll save that for another article.

Install phpMyAdmin

phpMyAdmin is a popular web-based tool for managing your local databases.

apt install phpmyadmin php-mbstring php-gettext

You will be prompted to select a web server. Hit the SPACE key to put an asterisk on 'apache2', press TAB and then hit ENTER:

phpMyAdmin Web Server Configuration

Next leave 'Yes' selected:

phpMyAdmin Database Configuration

Then leave the password blank so phpMyAdmin will generate a random one. Don't worry, you won't need it for anything.

phpMyAdmin Database Configuration

Since we've already enabled the 'mbstring' and 'mcrypt' PHP modules, just go ahead and restart Apache:

systemctl restart apache2

You should now be able to access phpMyAdmin by visiting http://localhost/phpmyadmin or When prompted for login credentials, enter 'user' and 'password' which was defined when installing MySQL above.


If you don't change the login cookie validity, you'll find it gets annoying always having to keep logging back in after 24 minutes of inactivity. The safest way to change this is through phpMyAdmin's settings UI. Just click the Settings button up at the top, then select Features. Change the Login Cookie Validity setting to a higher number than 1440. I use 10800 which is 3 hours. DO NOT do the same thing in a production environment!

Next, we'll allow terminal and Finder access to your Ubuntu server from your Mac.

Full Tutorial Table of Contents

Part 1: Installation

Part 2: Setup LAMP Stack

Part 3: Access Guest From Host

Part 4: Ubuntu Customization


Leave a comment

Function get_magic_quotes_gpc() is deprecated
The error has been logged in /anchor/errors.log
Uncaught Exception

Uncaught Exception

Function get_magic_quotes_gpc() is deprecated


system/boot.php on line 41


#0 [internal function]: System\Error::shutdown()
#1 {main}