Apache Toamcat Installation
Apache Toamcat Installation
Setting up a Web 2.0 stack on Red Hat Enterprise Linux 5 Update 2 August 2008
Setting up a Web 2.0 stack on Red Hat Enterprise Linux 5 Update 2 August 2008
Note Before using this document, be sure to read the information in Notices on page 49.
First Edition August 2008 This edition applies to Red Hat Enterprise Linux 5 Update 2 only. Copyright International Business Machines Corporation 2008. All rights reserved. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Contents
Chapter 1. Introduction . . . . . . . . 1
What is Web 2.0? . . . . . . . . . The Web 2.0 stack components . . . . Applications exploiting the Web 2.0 stack . Web 2.0 on Linux on System z . . . . System requirements for the Web 2.0 stack Assumptions for this whitepaper . . . Where to find this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 2 3 4 4 4 Creation of a Ruby Server Page 'Hello World!' example . . . . . . . . . . . . . Database connectors for Ruby . . . . . . Installing additional Ruby libraries using RubyGems . . . . . . . . . . . . Setup of Ruby on Rails . . . . . . . . . 23 . 23 . 23 . 24
iii
iv
Chapter 1. Introduction
In recent years, the traditional way to use the Internet has changed significantly. Web administrators who published information on the Internet invite their community to contribute to the company's Web pages. Applications like Wikis, Blogs, Content Management Systems and a couple of others are providing this new functionality to the Web administrators. The variety of available open source Web 2.0 applications require a proper setup of server components. These server components and their setup for Red Hat Enterprise Linux 5 Update 2 on Linux on System z are described in this whitepaper.
Web 2.0 is the business revolution in the computer industry caused by the move to the Internet as platform, and an attempt to understand the rules for success on that new platform. This new perspective changed the usage of the Internet significantly. In the past, system administrators were required to prevent users to access their private application interfaces from Web applications to avoid security issues. Of course, security issues are still required to be prevented, but APIs have been defined to allow reuse by 3rd party Web applications. Data that was earlier only available for customers who paid for it is now opened up for public use. Who imagined ten years ago that satellite images of the whole world would be freely available and accessible by a well defined, public API? Additionally, Web applications are getting more dynamic and more flexible due to new technologies. AJAX is one example to increase the performance of a Web 2.0 application by receiving data asynchronously from different services. Functionality of desktop application like drag and drop are available through JavaScript libraries. All of this created a new spirit around the Internet and this is called Web 2.0.
Database MySQL, PostgreSQL Web server Apache HTTP server, Apache Tomcat Programming Languages PHP, Perl, Python, Ruby, Java Frameworks Dojo (AJAX support), Ruby on Rails Caches memcached, Squid A system administrator does not have to install all Web 2.0 stack components at once. The selection is dependent on the Web 2.0 application and its functionality. i.e. if the Web 2.0 application offers information which is very static, it would make sense to establish a cache, but if the information is updated frequently then a cache is not the best choice. The appendix gives an overview on all packages which are used in this document.
Table 1. Open Source Web 2.0 applications Application Type Wiki Application name MediaWiki MoinMoin Confluence XWiki
Blogs
Business Applications
osCommerce
Setup of MySQL
MySQL is a SQL database management system (DBMS), which has become one of the most popular open source database systems. Some key characteristics - ease of use, reliability, security and performance at close to zero cost - increased the usage of MySQL, especially in Web applications. Detailed information about MySQL is available at http://www.mysql.org
Installation of MySQL
The installation of the MySQL is straight forward as the packages are included in Red Hat Enterprise Linux 5 Update 2. Install the MySQL server and the MySQL client packages by using the following command:
# yum install mysql mysql-server
The mysql package includes a command line client for MySQL. This client is required for administration purposes of the database server. To access the MySQL database from a Web application, a client for the related programming language is required. For the programming languages mentioned in this document, different client implementations are available. The setup of these clients are described in the section where the setup of the programming languages is described.
Lifecycle of MySQL
Lifecycle operations of Linux services like starting/stopping are basic functionality. In general, the lifecycle functionality is supported by a command line tool named service. The tool allows, dependent on the supported functionality of the service specific script, to start, stop and restart a server. Apart from that, the current status
of a server can be displayed. In the following, a walkthrough is shown for the MySQL server. This walkthrough assumes that this is the initial startup right after the packages have been installed. 1. Verifying the current status of the server To get the current status of the MySQL server, run the following command:
# service mysqld status mysqld is stopped
This indicates that the MySQL server is not started. 2. Starting the server for the first time To start the MySQL server, run the following command
# service mysqld start Initializing MySQL database: OK Filling help tables... OK Installing MySQL system tables...
To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password new-password /usr/bin/mysqladmin -u root -h localhost.localdomain password new-password See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! The latest information about MySQL is available on the web at http://www.mysql.com Support MySQL by buying support/licenses at http://shop.mysql.com [ OK ] Starting MySQL: [ OK ]
The output displays some initialization steps which indicate that the MySQL server has been started for the first time. After the startup is completed, verify the status of the MySQL server by running:
# service mysqld status mysqld (pid 10040) is running...
As shown, the MySQL server is running as expected. Note that the PID varies after each startup 3. Restarting the server Another useful functionality is the restart where it is possible to stop and start the server with one single command. To restart the MySQL server, run the following command:
# service mysqld restart Stopping MySQL: Starting MySQL: [ [ OK OK ] ]
Once the restart completes, verify the status of the MySQL server by running:
As expected, the MySQL server is running again. Note that the PID varies after each restart 4. Stopping the server To stop the MySQL server, run the following command:
# service mysqld stop Stopping MySQL: [ OK ]
As expected, the MySQL server is not running anymore. To start the MySQL server at boot time, the command chkconfig is used. Decide first in which runlevel the MySQL server should start. Runlevels are used to coordinate the startup of services during boot time. In the following example, the runlevels 3 and 5 are used as both support networking:
# chkconfig --level 35 mysqld on
Now, the MySQL server starts during boot time of runlevels 3 and 5. To deactivate this behavior again, run:
# chkconfig mysqld off
as the configuration for the MySQL server. The following steps should be processed to activate the predefined configuration:
# cp /usr/share/mysql/my-small.cnf /etc/my.cnf # chown root:root /etc/my.cnf # chmod 644 /etc/my.cnf
One of the first issues when dealing with any common resource is to ensure its security. At the beginning the password for the database superuser is set properly by running the following commands:
# mysqladmin -u root password <new-password> # mysqladmin -u root -h 127.0.0.1 password <new-password> # mysqladmin -u root -h <your_host_name> password <new-password>
Note: As this is the superuser for all databases, use a strong password The second recommended security measure is to remove anonymous accounts from the server. Access is granted only to those users specifically enabled.
# mysql -u root -p mysql> DELETE FROM mysql.user WHERE User = ; mysql> FLUSH PRIVILEGES; mysql> SELECT Host, User FROM mysql.user; mysql> quit
To get the list of users which have database access privileges, run the following command:
# mysql -u root -p mysql> select user,host from mysql.user; +------+----------------------------------+ | user | host | +------+----------------------------------+ | root | 127.0.0.1 | | root | localhost | | root | machine.example.com | +------+----------------------------------+ mysql> quit
Note: Applications connecting to the MySQL servers databases should use an account with minimum privileges required for its actions. This helps to prevent malicious code from accessing other databases and data. The configuration shown in this chapter does not cover all security aspects of MySQL. For more detailed information, refer to the MySQL documentation.
Setup of PostgreSQL
PostgreSQL is an advanced object-relational database management system.
Numerous features such as stored procedures, functions and triggers are included. All of these are blocks of code to be executed by the server and can be written in SQL or some compatible languages like C, C++, Java, PHP, Perl, Python, Ruby, etc. PostgreSQL provides a MVCC (Multi-Version Concurrency Control), eliminating read locks efficiently, different rules to rewrite incoming queries, etc. These features are very advanced, and very similar to the ones offered by commercial alternatives. Detailed information about PostgreSQL and the supported functionality is available at http://www.postgres.org
Installation of PostgreSQL
PostgreSQL has been included in various Linux distributions for some time. The installation process is straight forward. The database server can be installed by running the following command:
# yum install postgresql postgresql-server
Next to the PostgreSQL server, the client which is required to maintain the databases is installed also. To access the PostgreSQL database from a Web application, a client for the related programming language is required. For the programming languages mentioned in this document, different client implementations are available. The setup of these clients are described in the section where the setup of the programming languages is described.
Lifecycle of PostgreSQL
Once these basic PostgreSQL packages are installed, lifecycle operations can be performed like starting/stopping the server. In the following, a walkthrough is shown for the PostgreSQL server. This walkthrough assumes that this is the initial startup right after the packages have been installed. 1. Verifying the current status of the server To get the current status of the PostgreSQL server, run the following command:
# service postgresql status postmaster is stopped
This indicates that the PostgreSQL server is not started. 2. Starting the server for the first time To start the PostgreSQL server, run the following command
[ OK ] [ OK ]
The output displays an initialization step which indicates that the PostgreSQL server has been started for the first time. After the startup is completed, verify the status of the PostgreSQL server by running:
# service postgresql status postmaster (pid 18586 18585 18584 18582 18577) is running...
As shown, the PostgreSQL server is running as expected. Note that the PIDs vary after each startup. 3. Restarting the server Another useful functionality is the restart where it is possible to stop and start the server with one single command. To restart the PostgreSQL server, run the following command:
# service postgresql restart Stopping postgresql service: Starting postgresql service: [ [ OK OK ] ]
Once the restart completes, verify the status of the PostgreSQL server by running:
# service postgresql status postmaster (pid 18792 18791 18790 18788 18786) is running...
As expected, the PostgreSQL server is running again. Note that the PIDs vary after each restart. 4. Stopping the server To stop the PostgreSQL server, run the following command:
# service postgresql stop Stopping postgresql service: [ OK ]
As expected, the PostgreSQL server is not running anymore. To start the PostgreSQL server at boot time, the command chkconfig is used. Decide first in which runlevel the PostgreSQL server should start. Runlevels are used to coordinate the startup of services during boot time. In the following example, the runlevels 3 and 5 are used as both support networking:
# chkconfig --level 35 postgresql on
10
2:off
3:on
4:off
5:on
6:off
Now, the PostgreSQL server starts during boot time of runlevels 3 and 5. To deactivate this behavior again, run:
# chkconfig postgresql off
The client authentication is controlled by a configuration file, which traditionally is named pg_hba.conf. This file is stored in the database folder /var/lib/pgsql/data. One possible scenario is to allow connections from local and from a specific subnet. The following example shows how this works for the subnet 192.168.12.0/24. Local users can connect only to their own databases (databases with the same name as their database user name), administrators can connect to all databases and client connections from a subnet of 192.168.12.x can connect to the databases db1 and db2. For all users, authentication is required.
# TYPE local local host DATABASE sameuser all db1,db2 USER all @admins all CIDR-ADDRESS METHOD md5 md5 md5
192.168.12.0/24
The configuration shown in this chapter does not cover all security aspects of PostgreSQL. For more detailed information, refer to the PostgreSQL documentation.
11
12
This installs the Apache HTTP server with some documentation and an example page.
The output should look similar to the above. To connect with a Web browser to the Apache HTTP server, the server needs to be started. Use the following command to start the server:
# service httpd start
Open a Web browser and enter the URL http://localhost. A default Web page gets displayed indicating that the Apache HTTP server is running properly.
13
To get the current status of the Apache HTTP server, run the following command:
# service httpd status httpd is stopped
This indicates that the Apache HTTP server is not started. 2. Starting the server for the first time To start the Apache HTTP server, run the following command
# service httpd start Starting httpd: [ OK ]
After the startup is completed, verify the status of the Apache HTTP server by running:
# service httpd status httpd (pid 8608 8607 8606 8605 8604 8602 8601 8600 8599) is running...
As shown, the Apache HTTP server is running as expected. Note that the PIDs vary after each startup. 3. Restarting the server Another useful functionality is the restart where it is possible to stop and start the server with one single command. To restart the Apache HTTP server, run the following command:
# service httpd restart Stopping httpd: Starting httpd: [ [ OK OK ] ]
Once the restart completes, verify the status of the Apache HTTP server by running:
# service httpd status httpd (pid 8666 8665 8664 8663 8662 8661 8660 8659 8657) is running...
As expected, the Apache HTTP server is running again. Note that the PIDs vary after each startup. 4. Stopping the server To stop the Apache HTTP server, run the following command:
# service httpd stop Stopping httpd: [ OK ]
14
To start the Apache HTTP server at boot time, the command chkconfig is used. Decide first in which runlevel the Apache HTTP server should start. Runlevels are used to coordinate the startup of services during boot time. In the following example, the runlevels 3 and 5 are used as both support networking:
# chkconfig --level 35 httpd on
Now, the Apache HTTP server starts during boot time of runlevels 3 and 5. To deactivate this behavior again, run:
# chkconfig httpd off
During the installation of Apache HTTP server extensions additional modules are installed, but those might not be enabled. To enable or disable a module, configuration files have to be modified. In general those configuration files are the /etc/httpd/conf/httpd.conf and files located in the folder /etc/httpd/conf.d/.
15
Installation of mod_php
The Apache HTTP server can be enhanced by installing the package php. This is processed by using the following command:
# yum install php
Apart from the php package other dependent packages such as php-common are installed if they are not available on the system. The installation of the php package enabled the Apache HTTP server to server PHP pages. To activate the PHP support, restart the Apache HTTP server
# service httpd restart
During the installation process of PHP, one additional configuration file is created in the Apache server configuration folder. This file is located at /etc/httpd/conf.d/php.conf and includes a definition for the module to configure the interpreter to do its work when a PHP page is requested.
This newly created PHP script can be accessed by opening up a Web browser at http://localhost/test.php. A Web page is shown which displays 'Hello World!' along with the current time.
16
Another benefit is the amount of additional modules which are available for Perl. These modules add ease of use and interoperability with other tools or software components to the portfolio of the basic Perl functionality. Further information about Perl is available at http://www.perl.org.
Installation of mod_perl
mod_perl is an Apache enhancement which integrates the Perl interpreter into the Apache HTTP server. One benefit of the integration from Perl with Apache is that the startup time for any HTTP request for a Web page returned by a Perl script is reduced. The Perl scripts are compiled once and rerun every time a request occurs. To install the Perl module for Apache, run the following command:
# yum install mod_perl
Apart from the mod_perl package other dependent packages such as the core Perl package are installed if they are not available on the system. The installation of mod_perl integrates the configuration file /etc/httpd/conf.d/ perl.conf into the Apache HTTP setup. This file includes the setup for the Perl interpreter which is not enabled by default. Therefore, remove the leading # characters for the following configuration entries:
Alias /perl /var/www/perl <Directory /var/www/perl> SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlOptions +ParseHeaders Options +ExecCGI </Directory>
To enable the Perl support in the Apache HTTP server, restart the Apache server by running:
# service httpd restart
To get more information about the setup of mod_perl, refer to the documentation at http://perl.apache.org
17
Once the file is created, open up a Web browser on the system where the Web server is running and use the following URL: v http://localhost/perl/hello.pl
For Perl, the generic database interface needs to be installed to be able to use the MySQL or PostgreSQL interfaces of Perl. The MySQL and PostgreSQL interfaces can be installed separately also.
Installation of mod_python
The Apache HTTP server can easily be equipped to support Python. The required packages are all included in Red Hat Enterprise Linux 5 Update 2. To install the Python module for Apache, run the following command:
# yum install mod_python
Apart from the mod_python package other dependent packages such as the core Python package are installed if they are not available on the system.
18
The installation of mod_python integrates the configuration file /etc/httpd/conf.d/python.conf into the Apache setup. This file includes the setup for the interpreter to run Python scripts in specific folders. By default none of the folders are enabled to serve Python scripts. The configuration file offers configurations for the Publisher and the PSP handlers which need to be enabled. Using the Publisher handler To enable the Publisher handler, open the /etc/httpd/conf.d/python.conf file and remove the leading # character for the following configuration:
<Location /python> SetHandler python-program PythonHandler mod_python.publisher </Location>
This configuration enables the folder /var/www/python to deliver Python scripts using the Publisher handler. Using the PSP handler To enable the Python Server Pages handler, open the /etc/httpd/conf.d/ python.conf file and remove the leading # character for the following configuration:
<Directory /var/www/html> AddHandler mod_python .psp PythonHandler mod_python.psp </Directory>
This configuration enables the folder /var/www/html and all subfolders to deliver PSP files using the PSP handler. To enable the Python support in Apache restart the Apache HTTP server:
# service httpd restart
19
The PSP handler is slightly different since it allows embedding Python code into HTML code. A file named /var/www/html/hello.psp is created with the following content:
<html> <head> <title>Hello, World! - Python PSP</title> </head> <body> <% import time %> Hello, World!<br> The time on the server is <%=time.strftime("%H:%M:%S")%> </body> </html>
To give the two 'Hello, World!' examples a try, open a Web browser on the machine where the Web server is running and have a look at the result of the following two URLs: The Publisher handler 'Hello, World!' v http://localhost/python v http://localhost/python/index v http://localhost/python/index/index The PSP handler 'Hello, World!' http://localhost/hello.psp
Installation of mod_ruby
mod_ruby enables the execution of Ruby scripts by the Apache HTTP server. It embeds the Ruby interpreter to run the scripts natively in the Apache HTTP server. The mod_ruby package is not part of Red Hat Enterprise Linux 5 Update 2. To enable the Apache HTTP server with native Ruby support, a tarball needs to be
20
downloaded from the Internet at http://www.modruby.net/en/index.rbx/ mod_ruby/download.html. During time of writing the stable version of mod_ruby was 1.2.6. Prior to compiling mod_ruby some dependencies need to be resolved. Install the Ruby interpreter and the Apache Portable Runtime libraries along with the related development packages by using the following command:
# yum install apr-devel apr apr-util-devel apr-util httpd-devel ruby-devel ruby
The 'make install' step integrates mod_ruby into the Apache HTTP server infrastructure. During this step the configuration for mod_ruby is not enabled and needs to be done manually. A file which includes the configuration needs to be created in the Apache HTTP server environment. Create the file /etc/httpd/conf.d/ruby.conf with the following content:
LoadModule ruby_module modules/mod_ruby.so <IfModule mod_ruby.c> RubyRequire apache/ruby-run <Location /ruby> SetHandler ruby-object RubyHandler Apache::RubyRun.instance Options +ExecCGI </Location> <Files *.rbx> SetHandler ruby-object RubyHandler Apache::RubyRun.instance </Files> </IfModule>
Installation of eRuby
eRuby allows embedding of Ruby code into HTML files similar to other approaches like ASP, JSP or PSP. To install eRuby support for Apache HTTP server use the following command:
# yum install eruby eruby-libs ruby ruby-devel
In the previous section, mod_ruby was setup to serve Ruby CGI scripts. To setup eRuby, the configuration file /etc/httpd/conf.d/eruby.conf is created with the following content:
21
This requires adding the eRuby executable to the /cgi-bin directory. A symbolic link is created by using
# ln -s $(which eruby) /var/www/cgi-bin/eruby
Once this symbolic link is in place, the folder cgi-bin needs to be allowed to follow symbolic links. In the file /etc/httpd/conf/httpd.conf append the '+FollowSymLinks' value to the Options attribute of the <Directory "/var/www/cgi-bin"> entry. This new functionality is activated once the Apache HTTP server is restarted. Use the following command to restart the server:
# service httpd restart
Note: SELinux might reject the execution of eRuby because of the symbolic link. One possible solution is to disable SELinux for the Apache HTTP server. In many environments, disabling SELinux is not acceptable; therefore refer to the documentation at http://www.nsa.gov/selinux/ on how to setup SELinux properly.
In this folder, create a file named /var/www/html/ruby/hello.rbx with the following content:
require "cgi" cgi = CGI.new("html4") cgi.out { cgi.html { cgi.head { cgi.title {"Hello, World! - Ruby"} } + cgi.body { "Hello, World! <br>" + Time.now.strftime("The time on the server is %H:%M:%S") } } }
The Web page is reached by a locally started Web browser at the URL http://localhost/ruby/hello.rbx
22
The Web page is reached by a locally started Web browser at the URL http://localhost/hello.rhtml
23
Once RubyGems is installed on the system it can be used to enhance the Ruby installation with additional libraries such as tzinfo:
# gem install tzinfo --remote
Verifying the Ruby on Rails installation: Check the version number by running the following command:
# rails -v Rails 2.0.2
24
To verify the Rails example, start a Web browser on the machine where the Rails application server is running and direct to the URL http://localhost. A 'Welcome aboard' screen comes up with some information how to proceed further activities on the Web application.
25
Then, the configuration for the Apache HTTP needs to be modified. Add a new virtual host by modifying the /etc/httpd/conf/httpd.conf to include the following content2:
ProxyRequests off <VirtualHost *:80> ServerName www.rubytest.example.com ProxyPass / http://127.0.0.1:8000/ ProxyPassReverse / http://127.0.0.1:8000 ProxyPreserveHost on </VirtualHost>
Additionally, the Apache HTTP server needs to be setup to map requests for the new server name to the new virtual host. Therefore, the NameVirtualHost attribute needs to be configured in the file /etc/httpd/conf/httpd.conf. Since this is dependent on the individual Network setup, refer to the documentation of Apache HTTP server. Start the rails application in production mode on port 8000 by executing the following commands:
# cd /var/www/html/www.rubytest.example.com # mongrel_rails start -d -p 8000 -e production
Note: When multiple Ruby on Rails Web applications are intended to run simultaneously on the same system, each Web application must be assigned to a unique port number. Therefore the configuration of the virtual host given in the rubytest.conf file needs to be duplicated with another ServerName. Additionally the ports of the ProxyPass and ProxyPassReverse attributes have to match the port specified during startup of the Web application. Finally, restart Apache:
# service httpd restart
Make sure the application is running by opening http://localhost in a browser. The default Welcome aboard page should now appear.
2. This configuration is taken from http://mongrel.rubyforge.org/wiki/Apache where additional information is available about the integration of Mongrel into Apache
26
The output should look similar to the above. Note: If the output reports a different version of Java, but the 1.5.0 version has been installed, it might be the case that the version 1.5.0 is not set to be the one used by the system. The tool alternatives is used to switch the Java version used by the system. Use 'alternatives --config java' to select the right Java version. Once the correct Java version has been installed, install Apache Tomcat by running:
# yum install tomcat5 tomcat5-admin-webapps tomcat5-webapps
This installs the Apache Tomcat server and some administrative Web applications which allow some basic configuration of the Apache Tomcat server. Additionally, several example Web applications for Apache Tomcat are installed.
27
Open a Web browser on the server where the Apache Tomcat server is running and open the URL http://localhost:8080. The default Apache Tomcat Web page is shown which offers some examples, documentation and administration pages.
This indicates that the Apache Tomcat server is not started. 2. Starting the server To start the Apache Tomcat server, run the following command
# service tomcat5 start Starting tomcat5: [ OK ]
28
After the startup is completed, verify the status of the Apache Tomcat server by running:
# service tomcat5 status /etc/init.d/tomcat5 is already running (13847)
As shown, the Apache Tomcat server is running as expected. Note that the PID varies after each startup. 3. Restarting the server Another useful functionality is the restart where it is possible to stop and start the server with one single command. To restart the Apache Tomcat server, run the following command:
# service tomcat5 restart Stopping tomcat5: Starting tomcat5: [ [ OK OK ] ]
Once the restart completes, verify the status of the Apache Tomcat server by running:
# service tomcat5 status /etc/init.d/tomcat5 is already running (14661)
As expected, the Apache Tomcat server is running again. Note that the PID varies after each restart. 4. Stopping the server To stop the Apache Tomcat server, run the following command:
# service tomcat5 stop Stopping tomcat5: [ OK ]
As expected, the Apache Tomcat server is not running anymore. To start the Apache Tomcat server at boot time, the command chkconfig is used. Decide first in which runlevel the Apache Tomcat server should start. Runlevels are used to coordinate the startup of services during boot time. In the following example, the runlevels 3 and 5 are used as both support networking:
# chkconfig --level 35 tomcat5 on
Now, the Apache Tomcat server starts during boot time of runlevels 3 and 5. To deactivate this behavior again, run:
Chapter 4. Setup of Apache Tomcat
29
Afterwards the two administration applications can be accessed by using the URL http://localhost:8080/admin and http://localhost:8080/manager/html.
Adding support for JSP and Servlet API Installation of JSP and Servlet API libraries
The Apache Tomcat installation requires the JSP and Servlet libraries. Therefore an installation of additional packages is not required. In general the JSP implementation classes and the JSP Standard Tag Library are included in the following packages: v tomcat5-jsp-2.0-api v tomcat5-servlet-2.4-api The content of these packages is included into the Apache Tomcat environment during their installation.
30
Once this folder is created, create a file named /usr/share/tomcat5/webapps/ sample-jsp/hello.jsp which contains:
<%@page import="java.util.Date" %> <%@page import="java.text.DateFormat" %> <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>Hello, World! - JSP</title> </head> <body> <% String hwStr = "Hello World!\n"; %> <% String timeStr = "The time on the server is "; %> <% DateFormat currentTime = DateFormat.getTimeInstance(DateFormat.FULL); %> <%= hwStr %><br> <%= timeStr + currentTime.format(new Date()) %> </body> </html>
As well as the JSP file, create a folder which includes information about the Web application:
# mkdir /usr/share/tomcat5/webapps/sample-jsp/WEB-INF
This folder includes one additional file - web.xml - which can define various settings for the Web application. To keep this example simple it only includes the application name. The file /usr/share/tomcat5/webapps/sample-jsp/WEB-INF/ web.xml needs to be created with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/dtd/web-app_2_2.dtd"> <web-app> <display-name>JSP - Hello, World!</display-name> </web-app>
To complete the setup the new Web application needs to be deployed into the Apache Tomcat server. This is done by using the manager application. In the page at http://localhost:8080/manager/html, the application stored in the path sample-jsp needs to be started. Once the application is running, it can be accessed at http://localhost:8080/sample-jsp/hello.jsp. Note: Depending on the setup of Apache Tomcat the Web application example might be auto-deployed. In this case, the Web application is activated once the Apache Tomcat server is restarted.
31
The Servlet file is named /usr/share/tomcat5/webapps/sample-servlet/WEBINF/classes/sample/HelloWorldServlet.java and includes the following content:
package sample; import java.io.*; import java.util.Date; import java.text.*; import javax.servlet.http.*; import javax.servlet.*; public class HelloWorldServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Hello, World! - Servlet</title></head>"); out.println("<body>"); out.println("Hello, World!<br>"); DateFormat currentTime = DateFormat.getTimeInstance(DateFormat.FULL); out.println("The time on the server is " + currentTime.format(new Date())); out.println("</body></html>"); out.close(); } }
This Java source file needs to be compiled by using the following commands:
# cd /usr/share/tomcat5/webapps/sample-servlet/WEB-INF/classes # javac -cp /usr/share/java/tomcat5-servlet-2.4-api.jar sample/HelloWorldServlet.java
In the folder /usr/share/tomcat5/webapps/sample-servlet/WEB-INF, a web.xml file needs to be created with the following content:
32
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/dtd/web-app_2_2.dtd"> <web-app> <display-name>Servlet - Hello, World!</display-name> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>sample.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>hello-servlet</url-pattern> </servlet-mapping> </web-app>
To complete the setup, the new Web application needs to be deployed into the Apache Tomcat server. This is done by using the manager application. In the page at http://localhost:8080/manager/html, the application stored in the path sample-servlet needs to be started. Once the application is running, it can be accessed at http://localhost:8080/sample-servlet/hello-servlet. Note: Dependent on the setup of Apache Tomcat, the Web application example might be auto-deployed. In this case, the Web application is activated once the Apache Tomcat server is restarted.
Note: Version 5.1.5 is used in the examples throughout this document To integrate MySQL Connector/J integrated into the Apache Tomcat environment, create a link from the JAR file into the folder $CATALINA_HOME/common/lib of the Apache Tomcat server. Create the link as follows:
Chapter 4. Setup of Apache Tomcat
33
# ln -s /usr/share/java/mysql-connector-java.jar /usr/share/tomcat5/common/lib/
The JDBC driver is available for usage after a restart of the Apache Tomcat server has been performed.
# service tomcat5 restart
The installed package includes different versions of the JDBC driver for PostgreSQL. The different versions belong to the different version of the Java installation. This document is based on Java 1.5.0 which requires making use of the JDBC3 driver for PostgreSQL. To add this specific JDBC driver to the Apache Tomcat configuration, a link from the JDBC driver into the folder $CATALINA_HOME/common/lib of the Apache Tomcat server needs to be created. Create the link as follows:
# ln -s /usr/share/java/postgresql-jdbc3.jar /usr/share/tomcat5/common/lib/
The JDBC driver is available for usage after restarting the Apache Tomcat server.
# service tomcat5 restart
34
Installation of Squid
The Squid server is also part of Red Hat Enterprise Linux 5 Update 2. Use the following command to install the package:
# yum install squid
Note: The Squid server requires the network setup to provide a fully qualified domain name (FQDN). Review /etc/hosts to make sure that a FQDN is provided.
The output in this document shows the version number. From the output of the command, several configuration parameters and values are displayed also.
Lifecycle of Squid
Once these basic Squid server packages are installed, lifecycle actions can be performed like starting/stopping the server. In the following, a walkthrough is shown for the Squid server. This walkthrough assumes that this is the initial startup right after the packages have been installed. 1. Verifying the current status of the server To get the current status of the Squid server, run the following command:
35
This indicates that the Squid server setup is not complete. The initialization has not taken place yet. 2. Starting the server for the first time To start the Squid server, run the following command
# service squid start init_cache_dir /var/spool/squid... Starting squid: . [ OK ]
The output displays an initialization steps which indicate that the Squid server has been started for the first time. After the startup is completed, verify the status of the Squid server by running:
# service squid status squid (pid 25404) is running...
As shown, the Squid server is running as expected. Note that the PID varies after each startup. 3. Restarting the server Another useful functionality is the restart where it is possible to stop and start the server with one single command. To restart the Squid server, run the following command:
# service squid restart Stopping squid: . Starting squid: . [ [ OK OK ] ]
Once the restart completes, verify the status of the Squid server by running:
# service squid status squid (pid 25478) is running...
As expected, the Squid server is running again. Note that the PID varies after each restart. 4. Stopping the server To stop the Squid server, run the following command:
# service squid stop Stopping squid: . [ OK ]
36
To start the Squid server at boot time, the command chkconfig is used. Decide first in which runlevel the Squid server should start. Runlevels are used to coordinate the startup of services during boot time. In the following example, the runlevels 3 and 5 are used as both support networking:
# chkconfig --level 35 squid on
Now, the Squid server starts during boot time of runlevels 3 and 5. To deactivate this behavior again, run:
# chkconfig squid off
If the Web server runs on the same machine as the Squid server is running, the Web server must be re-configured to listen on a different IP address, e.g. the loopback address. The configuration of /etc/squid/squid.conf needs to be modified to allow Squid to listen on the Web servers IP address.
Chapter 5. Setup of Caches
37
Setup of memcached
Memcached is a distributed memory system for caching purposes. In general it is used to speed up communication between a Web application and a database. The result is to reduce response time for highly frequented Web pages and lesser load of the database server. There are several client APIs available to access the memcached server. All of the programming languages described in this book, Java, PHP, Perl, Python and Ruby, are supported with a memcached client API. There are various public Web sites which make use of memcached such as SourceForge, Wikipedia, YouTube, Facebook and many others. More information is available on the memcached project Web page at http://www.danga.com/memcached.
Installation of memcached
Memcached is not part of Red Hat Enterprise Linux 5 Update 2 itself. At the moment, the Fedora EPEL project includes a package for memcached. At the time of writing this the available version is 1.2.3. To build a package for Linux on System z the source RPM needs to be rebuilt. The procedure to get a binary RPM in place is the following:
# # > # yum install libevent-devel rpm-build wget -c\ http://download.fedora.redhat.com/pub/epel/5/SRPMS/memcached-1.2.3-7.el5.src.rpm rpmbuild --rebuild memcached-1.2.3-7.el5.src.rpm
Note that the version is displayed in the first line of output, followed by a list of available options.
Lifecycle of memcached
Once the memcached server package is installed, lifecycle actions can be performed like starting/stopping the server. In the following, a walkthrough is shown for the memcached server. This walkthrough assumes that this is the initial startup right after the packages have been installed. 1. Verifying the current status of the server To get the current status of the memcached server, run the following command:
38
This indicates that the memcached server setup is not complete. The initialization has not taken place yet. 2. Starting the server for the first time To start the memcached server, run the following command
# service memcached start Starting memcached: [ OK ]
The output displays an initialization steps which indicate that the memcached server has been started for the first time. After the startup is completed, verify the status of the memcached server by running:
# service memcached status memcached (pid 8012) is running...
As shown, the memcached server is running as expected. Note that the PID varies after each startup. 3. Restarting the server Another useful functionality is the restart where it is possible to stop and start the server with one single command. To restart the memcached server, run the following command:
# service memcached restart Stopping memcached: Starting memcached: [ [ OK OK ] ]
Once the restart completes, verify the status of the memcached server by running:
# service memcached status memcached (pid 8078) is running...
As expected, the memcached server is running again. Note that the PID varies after each restart. 4. Stopping the server To stop the memcached server, run the following command:
# service memcached stop Stopping memcached: [ OK ]
39
To start the memcached server at boot time, the command chkconfig is used. Decide first in which runlevel the memcached server should start. Runlevels are used to coordinate the startup of services during boot time. In the following example, the runlevels 3 and 5 are used as both support networking:
# chkconfig --level 35 memcached on
Now, the memcached server starts during boot time of runlevels 3 and 5. To deactivate this behavior again, run:
# chkconfig memcached off
These settings define to run memcached as user nobody at port 11211. At most 1024 connections are allowed and the cache size is limited to 64MB. The OPTIONS variable is used to pass additional parameter to the memcached. Refer to the documentation to get more information.
40
41
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Dojo example: AJAX clock</title> <script type="text/javascript" src="dojo-release-1.1.1/dojo/dojo.js" djConfig="parseOnLoad: true"></script> <style type="text/css"> @import "dojo-release-1.1.1/dijit/themes/tundra/tundra.css"; @import "dojo-release-1.1.1/dojo/resources/dojo.css"; </style> <style type="text/css"> #error, #main { margin: auto; margin-top: 120px; text-align: center } #error { font-size: 120%; } #main input,button { font-size: 400%; width: 250px }
</style> <script type="text/javascript"> dojo.require("dojo.parser"); dojo.require("dojox.timing"); dojo.require("dijit.form.TextBox"); dojo.require("dijit.form.Button"); var timer; var startClock = function() { timer.start(); } var stopClock = function() { timer.stop(); } var getCurrentTime = function() { console.debug("Timer ticked: Requesting time from server") // Performs the AJAX request to the URL specified; // After the request is sent and the response is received, // the load event is triggered, which in turn sets the new // value of the clock widget. dojo.xhrGet( { url: "http://localhost/time.php", handleAs: "text", timeout: 4000, load: function(response) { dojo.byId("error").innerHTML = ""; dijit.byId("clock").setValue(response); }, error: function(response) { dojo.byId("error").innerHTML = response; } } ); }
42
dojo.addOnLoad(function() { // Create a new timer that fires a tick every second timer = new dojox.timing.Timer(1000); // Every tick will lead to updating the text box with the current //time value from the server timer.onTick = getCurrentTime; dojo.connect(dojo.byId("clock-start"), onclick, startClock); dojo.connect(dojo.byId("clock-stop"), onclick, stopClock); }); </script> </head> <body class="tundra"> <div id="error"></div> <div id="main"> <input id="clock" dojoType="dijit.form.TextBox" style="" value="" /><br/> <button id="clock-start" dojoType="dijit.form.Button">Start the clock</button><br/> <button id="clock-stop" dojoType="dijit.form.Button">Stop the clock</button> </div> </body>
The second file, time.php, is stored in the /var/www/html folder also. It should include the following source code:
<?php // Returns the server time as HH:MM:SS echo (date ("H").":".date("i").":".date("s")); ?>
To run the example, a Web browser needs to be opened on the machine where the Web server is running and pointed to the URL http://localhost/time.html
43
44
PostgreSQL
postgresql postgresql-server
8.1.11-1 8.1.11-1
RHEL5 U2 RHEL5 U2
RHEL5 U2 RHEL5 U2 RHEL5 U2 RHEL5 U2 RHEL5 U2 RHEL5 U2 Internet Download at http:// www.modruby.net/ en/index.rbx/ mod_ruby/ download.html RHEL5 U2 RHEL5 U2 RHEL5 U2 RHEL5 U2
Apache Tomcat
PHP
php-mysql php-pgsql
5.1.6-20 5.1.6-20
RHEL5 U2 RHEL5 U2
Perl
perl-DBI perl-DBD-Mysql
1.52-1 3.0007-1
RHEL5 U2 RHEL5 U2
45
Table 2. Package overview of Web 2.0 stack in Red Hat Enterprise Linux 5 Update 2 (continued) Component Package Name perl-DBD-Pg Version 1.49-1 Source RHEL5 U2
Python
MySQL-python postgresql-python
1.2.1-1 8.1.11-1
RHEL5 U2 RHEL5 U2
Java
RHEL5 U2 supplementary RHEL5 U2 supplementary RHEL5 U2 Internet Download at http:// dev.mysql.com/ downloads/ connector/j/5.1.html RHEL5 U2
postgresql-jdbc
8.0.1-407
Ruby
ruby ruby-devel ruby-rdoc ruby-irb rubygems rubygem-rake rubygem-rails rubygemactivesupport rubygem-actionpack rubygemactionmailer
1.8.5-5 1.8.5-5 1.8.5-5 1.8.5-5 0.9.4-1 0.7.3-2 2.0.2-2 2.0.2-2 2.0.2-2 2.0.2-2
RHEL5 U2 RHEL5 U2 RHEL5 U2 RHEL5 U2 Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL Download from Fedora EPEL
46
Table 2. Package overview of Web 2.0 stack in Red Hat Enterprise Linux 5 Update 2 (continued) Component Package Name eruby eruby-libs eruby-devel ruby-mysql ruby-postgres Version 1.0.5-1 1.0.5-1 1.0.5-1 2.7.3-1 0.7.1-5 Source RHEL5 U2 RHEL5 U2 RHEL5 U2 Download from Fedora EPEL Download from Fedora EPEL
Squid
squid
2.6.STABLE6-5
RHEL5 U2
memcached
memcached
1.2.37
Dojo
dojo-release
1.1.1
47
48
Notices
This information was developed for products and services offered in the U.S.A. IBM may not offer the products, services, or features discussed in this document in other countries. Consult your local IBM representative for information on the products and services currently available in your area. Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM product, program, or service may be used. Any functionally equivalent product, program, or service that does not infringe any IBM intellectual property right may be used instead. However, it is the users responsibility to evaluate and verify the operation of any non-IBM product, program, or service. IBM may have patents or pending patent applications covering subject matter described in this document. The furnishing of this document does not give you any license to these patents. You can send license inquiries, in writing, to: IBM Director of Licensing, IBM Corporation, North Castle Drive Armonk, NY 10504-1785 U.S.A. The following paragraph does not apply to the United Kingdom or any other country where such provisions are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS PUBLICATION AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer of express or implied warranties in certain transactions, therefore, this statement may not apply to you. This information could include technical inaccuracies or typographical errors. Changes are periodically made to the information herein; these changes will be incorporated in new editions of the publication. IBM may make improvements and/or changes in the product(s) and/or the program(s) described in this publication at any time without notice. Any references in this information to non-IBM Web sites are provided for convenience only and do not in any manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the materials for this IBM product and use of those Web sites is at your own risk. IBM may use or distribute any of the information you supply in any way it believes appropriate without incurring any obligation to you. Information concerning non-IBM products was obtained from the suppliers of those products, their published announcements or other publicly available sources. IBM has not tested those products and cannot confirm the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products. This information contains examples of data and reports used in daily business operations. To illustrate them as completely as possible, the examples include the
49
names of individuals, companies, brands, and products. All of these names are fictitious and any similarity to the names and addresses used by an actual business enterprise is entirely coincidental. COPYRIGHT LICENSE: This information contains sample application programs in source language, which illustrates programming techniques on various operating platforms. You may copy, modify, and distribute these sample programs in any form without payment to IBM, for the purposes of developing, using, marketing or distributing application programs conforming to the application programming interface for the operating platform for which the sample programs are written. These examples have not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. You may copy, modify, and distribute these sample programs in any form without payment to IBM for the purposes of developing, using, marketing, or distributing application programs conforming to IBMs application programming interfaces.
Trademarks
The following terms are trademarks of the International Business Machines Corporation in the United States, other countries, or both: developerWorks, HiperSockets, IBM, OS/2, System z, z/VM The following terms are trademarks of other companies: Java, JavaScript, and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft, Windows, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a trademark of Linus Torvalds in the United States, other countries, or both. Other company, product, and service names may be trademarks or service marks of others.
50