Tuesday 30 March 2010

Getting Started: The AMP in LAMP

In the previous post we set up CentOS running under Virtual Box.

So far we've just taken the default "Server - GUI" configuration.  Now we'll install the rest of the AMP stack, along with some tools to make our work easier.

We'll install the base AMP components:

  • Apache HTTP
  • MySQL
  • Perl
  • PHP
  • Python
and the additional tools:
  • Firefox
  • emacs
Installing Software on CentOS

CentOS uses the yum package manager. We will use the command-line application yum to install packages.  The primary commands we will use are "yum install pkgname" and "yum search pkgname".

If you prefer to use a GUI, the "Applications > Add/Remove Software" provides dialogs for installing and uninstalling software.

To use "yum", first open a terminal from the "Applications > Accessories > Terminal" menu item.  We will be logged into the terminal as "r-user".  In order to install system software we need to be a "superuser".  That is, a user with administrative rights.

To show that you are a superuser, use the "su" command and enter the system password.  In our example installation the password is "r-lamp".

Here are the commands to install the components we want.

yum install emacs
yum install firefox
yum install httpd httpd-devel
yum install mysql mysql-server mysql-devel
yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml
yum install perl
yum install python mod_python



If the component is already installed, it will say so and return immediately. If not, you'll be shown a list of components to be installed. Press "y" to continue with the installation.

The list of MySQL and PHP components is taken from the very useful "Quick 'n' Easy LAMP Server" article.  That article is the basis for the configuration steps described below, along with the article "Embedding Python in Apache 2".

Apache HTTPD

The most-used web server in the world is Apache HTTPD.  This is the Apache HTTP daemon.  We'll refer to this as "Apache".

In a LAMP application, the job of HTTPD is to take requests from a web browser, hand them over to something like PHP to prepare a response such as an HTML page, and delivers that response back to the web browser.

Some commands we will use to manage HTTPD are:

/etc/init.d/httpd/status
/etc/init.d/httpd/start
/etc/init.d/httpd/stop
/etc/init.d/httpd/restart


Apache has a standard system for installing add-on "modules".  These are C applications expanding the capabilities of Apache.  We will configure the modules needed for Apache to call PHP, Perl, or Python when the user requests a page requiring processing.

Apache has a wide range of capabilities for directing requests based on the URL requested.  Here we will configure Apache to call PHP, Perl, or Python when files with specific file extensions are requested.

By default, Apache maps URL's to locations under the "/var/www/html" directory.  We will place our example files there.

PHP

When the PHP components are install by "yum", Apache is also configured to use PHP when a requested file name ends in "php", such as "test.php".

Create a file "/var/www/html/test.php" with the following text:

<?php
   phpinfo();
?>

If you are familiar with emacs, you can launch an emacs editor
window from the Terminal using the "emacs" command. Otherwise,
use some other method to get a text editor with permission to
write to "/var/www/html".

I'm being vague here as I haven't
explored how to get the right permissions using the "gedit"
text editor that is available from the menu system.

To test PHP, make sure Apache is started, open Firefox, and
browse to "http://localhost/test.php". If PHP is working,
the browser will display PHP version information.

Python

The Apache configuration file for Python is in "/etc/httpd/conf.d/python.conf".  Before modifying this file, you may want to save a copy under a name such as "python.conf.orig".

The line in this file that enables the Python support in general is:


LoadModule python_module modules/mod_python.so


The Python module supports two types of file processing.  The "Python Publisher" handler will execute a Python script file.  The "Python PSP" handler takes a file with Python code embedded in HTML and processes it in a manner similar to PHP.

We can specify that "py" files will be handled by the "Python Publisher" and "psp" files by the "Python PSP" handler as shown below.


<Directory /var/ww/html>
  AddHandler mod_python .psp .py
  <Files *.psp>
    PythonHandler mod_python.psp
  </Files>
  <Files *.py>
    PythonHandler mod_python.publisher
  </Files>
</Directory>



The following code creates an "mpinfo" location that provides information about mod_python similar to the information we saw from the "phpinfo()" function.


<Location /mpinfo>
    SetHandler mod_python
    PythonHandler mod_python.testhandler
</Location>


With these settings, we can create a "test.py" and "test.psp" file in "/var/www/html" to try things out.

An example "test.py" file is:


def index(req):
    return "Hello from Python!";


An example "test.psp" file is:


<HTML>
<BODY>
<H1><% req.write("Hello from PSP!") %></H1>
</Body>
</HTML>


We can browse to "http://localhost/test.py" and "http://localhost/test.psp" to try these out.  Browse to "http://localhost/mpinfo" to see the information about mod_python.

Perl

Configuring Perl is similar in principle to configuring Python.  Having said that, I don't have a lot of interest in Perl at the moment and the "Hello World" examples aren't concise enough to be trivial at this time of the evening.  Hence we will leave Perl unexplored for the time being.

MySQL

Before departing, let's not forget the "M" in LAMP, which is "MySQL".  MySQL is a popular open source database.  The MySQL daemon will start up MySQL and leave it waiting for requests from PHP, Python, or Perl.  The daemon is started with:


/etc/init.d/mysqld start


The console interface to MySQL can be started with:


mysql


The "Quick 'n' Easy LAMP" article referenced above provides information on how to set the administrative password and create a new user in MySQL.


Automatic Startup

On a server, we will want Apache and MySQL to start up automatically when the machine is started.  The following commands will set this:


/sbin/chkconfig httpd on
/sbin/chkconfig mysqld on


Summary

In this posting we've discussed how to install and configure Apache, MySQL, PHP, and Python.  We skipped Perl for the time being.

This gives us everything we need to start creating LAMP applications. The next post in this series will discuss how to add Rapache in order to use R rather than PHP or Python for processing requests.









No comments:

Post a Comment