Cloud server

Get support by phone or email

We have native English-speaking support staff in the UK, US and Thailand, providing friendly support around the clock.

Speak to us in person by calling +1 415 358 5210, +44 (0) 20 7183 8250 or +44 (0)845 686 8642.

Mail us at support@elastichosts.com with your account zone and login email address.

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 10.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 introductory demo. For the purposes of this tutorial, we'll use a pre-installed Ubuntu 10.04 system, and we'll call it puffin.

Our Ubuntu 10.04 server, ready to boot

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

Step 2: Install PHP and Apache

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

# ssh root@83.222.226.228

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 http://83.222.226.228/ (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:

The Apache welcome page shows that we have installed correctly

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 "<?php phpinfo(); ?>" > /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 http://http://83.222.226.228/test:

An info page to show that PHP is running

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

Step 3: 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 DATABASE prices;
> 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);
> exit;

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:

<?php
// connect to the database
$con = mysql_connect('localhost','elastic1','oag4Chai')
    or die('Could not connect to the server!');
// select a database:
mysql_select_db('prices')
    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 http://83.222.226.228/test, we should see our page displaying the data from the MySQL database:

Our LAMP stack is complete - PHP displaying information from MySQL

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.