LAMP Tutorials (1/6): Set Up a LAMP Stack on a Cloud Server

This post is the first chapter of a LAMP Tutorial series in which we will show you how to set up a fully redundant, high-availability LAMP stack in the cloud with ElasticHosts. Find the other parts here:

Set Up a LAMP Stack on a Cloud Server

In this tutorial, we’ll set up a LAMP (Linux, Apache, MySQL, and PHP) stack on an ElasticHosts Cloud Server. We’ll be using an Ubuntu 14.04 Linux server, running a website on Apache, with a PHP front-end, backed by a MySQL database.

Step 1: Set up an ElasticHosts Cloud Server

First, we need to set up an ElasticHosts Cloud Server as described in the Set Up A Cloud Server Tutorial. For the purposes of this tutorial, we’ll use a pre-installed Ubuntu 14.04 system, and we’ll call it puffin.

Once you have created the server, click Start to boot it, then VNC into your server, and reset the root password, as described in the tutorial.

Step 2: Install PHP and Apache

Connect to your server via ssh, using your new root password:

ssh root@  

Update the package management tools, then install Apache and PHP, as follows:

apt-get update  
apt-get dist-upgrade  
apt-get install apache2 php5 libapache2-mod-php5  

Once it’s finished, you can go in a browser to (substitute the IP address of your own server, which you can find in the control panel), and you should see the Apache ‘It works!’ page:


Step 3: Set up a basic PHP page

Now let’s write a PHP page, make it executable, and restart Apache:

mkdir /var/www/test  
echo "" > /var/www/test/index.php  
chmod a+x /var/www/test/index.php  
/etc/init.d/apache2 restart

To see your PHP, open a browser and go to


You should see a lot of information about your PHP setup, which means both PHP and Apache are running correctly.

Step 4: Build a simple database

Now let’s build a very simple database, and connect our web app to it. First, we install MySQL:

apt-get install mysql-server mysql-client php5-mysql  

Set a MySQL root password when asked, and leave Apache as your chosen server. Now access your MySQL server, and create a database and a privileged user:

mysql -u root -p  
CREATE USER 'elastic1' IDENTIFIED BY 'oag4Chai';  
GRANT ALL PRIVILEGES ON prices.* to 'elastic1';  

And insert some basic data – for the purposes of this tutorial, we’re going to create a database of metal prices:

USE prices;  
CREATE TABLE metals (name VARCHAR(100), price_usd_lb DOUBLE);  
INSERT INTO metals VALUES ('zinc',0.9811);  
INSERT INTO metals VALUES ('tin',13.6305);  
INSERT INTO metals VALUES ('copper',4.0531);  

And now let’s make a PHP page that connects to our database and displays some information:

cd /var/www  
vi index.php  

Using the text editor of your choice (we use vi), add the following to index.php:

// connect to the database
$con = mysql_connect('localhost','elastic1','oag4Chai')
or die('Could not connect to the server!');  
// select a database:
or die('Could not select a database.');  
// build query:
$sql = "SELECT name, price_usd_lb FROM metals";
// execute query:
$result = mysql_query($sql)
or die('A error occurred: ' . mysql_error());  
// get result count:
$count = mysql_num_rows($result);
print "<h3>$count metal prices available</h3>";  
print "<table>";  
print "<tr><th>Name</th><th>Price (USD/lb)</th></tr>";  
// fetch results:
while ($row = mysql_fetch_assoc($result)) {  
$name = $row['name'];
$price = $row['price_usd_lb'];
print "<tr><td><strong>$name</strong></td><td>$price</td></tr>";  
print "</table>";  

Make the file executable, delete the placeholder index page, and restart the server:

chmod a+x index.php  
rm index.html  
/etc/init.d/apache2 restart

Now if we visit, we should see our page displaying the data from the MySQL database:


So that’s how to set up a LAMP stack on just one ElasticHosts server. If you want to make it visible on the public internet, you’ll also want to add a static IP address and a DNS record.

You're finished

With the LAMP stack in place, the next step in the tutorial will be: Moving MySQL to a Separate Cloud Database Server.