How to Set Up a LAMP Environment For the Cloud

LAMP is the popular acronym which stands for Linux, Apache, MySQL and PHP which are open-source software that are closely associated with dynamic data driven websites. The open source nature (read “free” to adapt to your own requirements) make them very attractive to web developers and businesses alike who are looking for a cost effective solution to running websites and servers. The main benefit of using a LAMP stack is that it’s fairly straightforward to get started – even beginners should find it relatively headache free. Better yet a LAMP environment sits rather perfectly with a range of different web hosting providers which means that pretty much all hosting companies offer LAMP servers as standard. But what if you fancy learning how to create your own LAMP server by scratch?  

The quickest way to set up your LAMP stack is to find yourself an online tutorial that fits with your server set up. In this tutorial we take a look at setting up a LAMP environment using a virtualised server offering known as cloud server. The benefit of using a cloud server is that they are quick to set up and extremely flexible. If you find you make a mistake on a cloud server you can simply delete the instance of the virtual server and start again. They also offer a fantastic learning environment as the resources will be standardised depending on the operating system you install. Clearly there is a cost to running a cloud server much in the same way as running any website. Fortunately there are plenty of cloud hosting demos that offer an environment in which you can create a test instance to follow along with this tutorial. Once you have found your cloud server solution of choice then start SSH or VNC.

Firstly boot into your virtual computer using your VNC of choice. If you have opted to use ElasticHosts’ free cloud computing demo then simply grab your password for the server (it should automatically create one) and use the root user when you have arrived at the login screen for your OS. A few steps back but ensure you install a Linux based OS such as Ubuntu, Debian or CentOS. If using SSH at the terminal simply go to:

# ssh [email protected]

Where xx.xxx.xxx.xxx is the IP address assigned to your virtual server. Next we use what is known as a package management tool to essentially download the latest build version of both the Apache server software and the PHP language libraries. To do this enter the following in the terminal screen:

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

This essentially tells the server to update the updated version of the package management tool, install it /upgrade and then install the latest version of both Apache 2 and PHP 5. Yup it really is that easy! You can check to make sure it’s all working by typing the following into a browser: http://xx.xxx.xxx.xxx/(add in your server’s IP address - you’ll get this in the control panel).

Excellent! Now let’s test out PHP by creating a page right in the terminal!

Now set up the PHP page and make it accessible by following these steps:

$ 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

This is telling the server to create a directory within /var/www/ called “test”, create a file called index.php, make it executable and then restart Apache2. /var/www/ is the directory location of the server’s main web files. If you want to check your PHP, then paste the following into your browser: http://http://xx.xxx.xxx.xxx/test.

 

You’ll know that both PHP and Apache are working properly because this should bring up a table showing a selection of information about the PHP configuration.

Next you can construct a database! To do this, you’ll need to install MySQL – a database management system that runs in Apache and works with PHP:

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

The installation screen will require you to set a password for your admin account and to give your preferred server (Apache). If your aim is for the site to essentially go “live” then ensure that you secure your database by providing a secure password.

You should now be able to access your new MySQL server so that you can set up a database, along with a privileged user using these steps:

$ mysql -u root -p > CREATE DATABASE prices; > CREATE USER ‘elastic1′ IDENTIFIED BY ‘oag4Chai’; > GRANT ALL PRIVILEGES ON prices.* to ‘elastic1′;

Add in whatever data material you’re going to use (for demonstration this database is using basic grocery prices):

> USE prices; > CREATE TABLE groceries(name VARCHAR(100), price_usd_lb DOUBLE); > INSERT INTO groceries VALUES (‘carrots’,0.86); > INSERT INTO groceries VALUES (‘peas’,1.20); > INSERT INTO groceries VALUES (‘oats’,0.96); > exit;

Almost there. To make a PHP page that shows information and connects to your database follow these steps:

$ cd /var/www $ vi index.php

You’ll need to use a text editor (such as vi - $ apt-get install vi), to add the following information to index.php:


<?php
$conn = mysql_connect("localhost", " elastic1", "oag4Chai ");  
if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
if (!mysql_select_db("prices")) {
echo "Unable to select prices: " . mysql_error();
exit;
}
$sql = "SELECT name, price_usd_lb         FROM   groceries";  
$result = mysql_query($sql);
$count = count(mysql_fetch_assoc($result));
print "$count groceries available";
print "";
print "<table><tbody><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 "</tbody></table>";
?>

Select ‘file executable’, then delete the placeholder index page, and finally restart the server as follows:

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

If you paste the following text into your browser, you should see the page showing the data from your new MySQL database: http://xx.xxx.xxx.xxx/test And that should be that. If you decide that you’d like this information to be publicly visible, then you’ll also need to add a static IP address and a DNS record.

ElasticHosts’ linux cloud servers are easy to create, manage and scale to your requirements. Find out more about cloud hosting at www.elastichosts.com

Featured image courtesy of ivanpw.

Filed in: Cloud Hosting, Feature Articles, Technology
Want to submit your own guest post? Visit our guest posting guidelines page.

You might like:

Advertising Antics: Finding the Best Agency Advertising Antics: Finding the Best Agency
HR Outsourcing Services: Why They Are the Right Choice for Many Small Businesses HR Outsourcing Services: Why They Are the Right Choice for Many Small Businesses
10 WordPress Tips for the Indie Maker-Seller 10 WordPress Tips for the Indie Maker-Seller
Interesting Business Tips and Ideas From Successful Millionaires Interesting Business Tips and Ideas From Successful Millionaires

Leave a Reply

You must be Logged in to post comment.

© 2012 Geek Business. All rights reserved. XHTML / CSS Valid.