Bruning Questions: LAMP stack on Joyent SmartOS

December 14, 2012 - by Mr. Max Bruning

ask-mr-bruning-logo

To ask a SmartOS, DTrace, or mdb question of your own, submit to MrBruning@joyent.com or on Twitter at #BruningQuestions

Question: How do I get a LAMP stack running on SmartOS?

This week, I'll show how to set up a LAMP stack on a Linux virtual machine in the Joyent Public Cloud, and the equivalent on a SmartMachine running in a SmartOS virtualized instance. I'll assume that you already have a lamp stack on, for instance, an AMI instance on AWS, and that you have already signed up for the Joyent Public Cloud (JPC), (see joyent.com and click on "GET STARTED"). I'll also assume that after signing up for the JPC, you have set up ssh keys so that you can log in to your machines.

lamp-ss-1

First, we'll provision a Centos instance (AMI is running Centos). Once you have signed in to the JPC, you'll click on "Add a machine", and get to a window that looks like:

lamp-ss-2

In this window, under "Select an image" and "View", we'll choose "Linux" instead of "All". And here we'll choose "centos-5.7". This is the closest image to what is running on AWS. You may also want to select a datacenter that is closer to your physical location. Clicking on the centos-5.7 image will take us to a screen with "Sizes".

lamp-ss-3

We'll choose the smallest size (the first choice) and continue. This will take us to a "Confirm & Customize" screen. We'll give the machine a name, for instance: "centostest", and click on "Provision".

lamp-ss-4

This will provision a 512MB machine with 15GB of disk space, and 1 virtual CPU. After a short time, you'll get a window that says "Provisioning Machine...".

lamp-ss-5

Click on "Go to your machine list" on that window.

lamp-ss-6

And click on your machine name.

lamp-ss-7

Next to IP Addresses, you’ll see 2 IP addresses. One of these (10.x.y.z) is an internal address. We’ll use the other address to ssh to the machine.

lamp-ss-8

Note that you log in as root (different on AWS).

Now we’ll do the same steps that are done on the AMI image to install php and mysql.

# yum update  ← update everything
…

# yum install httpd
…

# yum install mysql-server mysql
…
# yum install php php-mysql
…
# yum install php-xml php-pdo php-odbc php-soap php-common  php-cli php-mbstring php-bcmath php-    
ldap php-imap php-gd
…

At this point, these are the same steps to install a LAMP stack on a centos virtual machine on the JPC as they are on an AMI image on AWS. On the JPC, there are some additional steps. Amazon gives you a domain name for your machine. Something like ec2-12-345-67-890.compute-1.amazonaws.com. On the JPC, you are responsible for getting a domain name for your machine. We’ll use the IP address instead. To tell LAMP this information, we need to edit /etc/hosts and /etc/httpd/conf/httpd.conf.

You’ll need the name of your machine. This will be a uuid, something like: ba81f31c-7f93-4a05-a65b-ab0fba7aef05. You can get this by running:

# uname -a
Linux ba81f31c-7f93-4a05-a65b-ab0fba7aef05 3.5.4joyent-centos-5.7-opt #1 SMP Sun Sep 30 13:53:44
PDT 2012 x86_64 x86_64 x86_64 GNU/Linux
#

Now edit /etc/hosts and add your machine. Afterwards, your < /etc/hosts file should look something like:

# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       localhost.localdomain localhost ba81f31c-7f93-4a05-a65b-ab0fba7aef05
::1         localhost6.localdomain6 localhost6
#

The name of the machine has been added to the line with localhost.

Next, you’ll need to edit the /etc/httpd/conf/httpd.conf file to add the ServerName. This will be a new line that looks like:

ServerName 165.225.137.43:80

where 165.225.137.43 should be the IP address for your machine.

Next, we’ll flush any existing IP tables:

# iptables  --flush

And now add a /var/www/html/index.php , the same as on the AMI image:

# cat /var/www/html/index.php
<?
    phpinfo();
?>

Of course, if you are moving an existing image from AWS, there may be other changes to configuration files, as well as additional files containing your web pages and database(s) that need to be copied over.

At this point, we’re ready to start the services:

# service httpd start
Starting httpd:                                            [  OK  ]
# service mysqld start
…

And now, we’ll go to the web page:

lamp-ss-9

We’re up and running.

So, let’s take a look at doing the same thing on a SmartMachine. A SmartMachine is an OS virtualized instance of SmartOS (the operating system running in the JPC). A SmartMachine has various benefits over running a Linux or Windows virtual machine, but we’ll save the benefits for a future blog. Here, we are basically setting up a SAMP stack (SmartOS instead of Linux, with apache, mysql, and php).

We can skip the steps of installing php and mysql. They come pre-installed in some of the SmartMachine images. The main differences are that configuration and web page files are in different locations. Let’s take a look at the steps.

We’ll start back on the JPC provisioning page:

lamp-ss-10

This time, we’ll choose the first image (standard), which comes with “Everything you need to build a web stack”.

lamp-ss-11

And again, we’ll pick the extra small 512MB package.

lamp-ss-12

This time, we’ll call the machine “smartostest”, and click on “Provision”.

lamp-ss-13

And from here, go to “Go to your Machine List”.

lamp-ss-14

And click on the “smartostest” machine.

lamp-ss-15

At this point, we can try the web page using the external IP address from the above screen (165.225.129.160).

lamp-ss-16

To get the same (phpinfo()) output, we’ll create an index.php file, the same as on the Linux images (but in a different location…).

# cat /opt/local/share/httpd/htdocs/index.php
<?php
       phpinfo();
?>
#

And going to the website now shows:

lamp-ss-17

I am currently working on a DTrace script that will trace ZFS write calls from application to the device. I’ll blog about this in the next few weeks. Please let me know if you have questions.

Sign up Now For
Instant Cloud Access

Get Started