Setting up a Cloud Server LAMP Stack


In this tutorial, we'll set up a LAMP (Linux, Apache, MySQL and PHP) stack on a Crosspeer 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 your Crosspeer Cloud Server

First, you need to setup your Crosspeer cloud server as described in the Getting Started tutorials.  For the purposes of this tutorial, we'll use a pre-installed Ubuntu 10.04 system, and we'll call it seagull.
 
Once you have created the server, click 'Start' to boot it, then VNC in to your server, and reset the 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:
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 http://83.222.226.228/test:
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 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:
So that's how to set up a LAMP stack on just one Crosspeer 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.
Step 2:  Install PHP and Apache

Connect to your server via ssh as shown in the example below, using you new root password:
Copyright © 2012 - 2014 Crosspeer, Inc.
               All Rights Reserved