Mais conteúdo relacionado Semelhante a Lamp Introduction 20100419 (20) Mais de Vu Hung Nguyen (20) Lamp Introduction 201004191. Introduction to LAMP
Cory Aitchison
Software Engineer
Novell Inc.
caitchison@novell.com
March 24, 2006
2. Session Overview
• Introduction to LAMP
• Installation on OES Linux
• Optimization
• Some available applications
• Migration
• Creating Lamp Applications
2 © Novell Inc, Confidential & Proprietary
4. What is LAMP
• LAMP is a framework for developing and web
applications rapidly
• LAMP stands for
– Linux
– Apache
– MySQL
– PHP (or Perl or Python)
• Each one of these components of the LAMP stack is
interchangable
4 © Novell Inc, Confidential & Proprietary
5. Stack Model
Apache
mod_php
PHP
MySQL
Linux/OES
Hardware
5 © Novell Inc, Confidential & Proprietary
6. Stack Model – MySQL
You can use any database in place of MySQL:
• Oracle*
• PostgreSQL
• Access
• DB/2*
MySQL is generally preferred for the following reasons:
• Ease of use
• Good documentation
• Open Source
• 4.0.18
6 © Novell Inc, Confidential & Proprietary
7. Stack Model - PHP
When people talk about LAMP the language
generally referenced is PHP. Although, it is
possible to use any web ready language at this
point in the stack.
• OES Version 4.3.4
• Excellent documentation at http://www.php.net
• A lot of support in the open source community
• (http://hotscripts.com)
• Tight integration with Apache through mod_php
• You must take care of security concerns
7 © Novell Inc, Confidential & Proprietary
8. Stack Model - Apache
Again here you could use any webserver, however, apache
is perhaps the best and most widely used webserver
available
• Highly configurable
• Very flexible
• Large market share
• OES Version 2.0.49
8 © Novell Inc, Confidential & Proprietary
10. OES Linux Installation
• During install ensure that required packages have
been selected
– apache2-prefork
– mysql
– php4
– apache2-mod-php4
– php4-mysql
– php4-session
– php4-sockets
– php4-zlib
10 © Novell Inc, Confidential & Proprietary
11. OES Linux Installation
• Some additional configuration is helpful.
• Create the file /etc/apache2/httpd.conf.local
• Add the line:
DirectoryIndex index.html index.php
• Edit the file /etc/sysconfig/apache2
• Add /etc/apache2/httpd.conf.local to the
APACHE_CONG_INCLUDE_FILES line
11 © Novell Inc, Confidential & Proprietary
12. Errata
• Most services run in user space.
• Kernel is isolated from processes
• Development can be done on server
• Can be used as a desktop
• Admin skills are similar for desktop and server
• Easy to configure
12 © Novell Inc, Confidential & Proprietary
14. Apache
• Most popular web server on the planet
• Highly configurable
• can be clustered
• open source
• many modules and management applications
available
• http://httpd.apache.org/
14 © Novell Inc, Confidential & Proprietary
15. Popular Apache Modules
• mod_rewrite
• mod_php
• prefork
• mod_auth
• mod_cgi
• optimization modules in following slides
15 © Novell Inc, Confidential & Proprietary
16. Apache Optimization
• Mod_Mem_Cache – Memory based storage manager
(Experimental)
– CacheEnable – Enables memory or disk based caching
– MCacheSize - maximum amount of memory to be used by the
cache (in Kbytes)
– MCacheMaxObjectCount - maximum number of objects to be
cached
– MCacheMinObjectSize - minimum size of a document (in bytes)
– MCacheMaxObjectSize – maximum allowable size of a
document (in bytes)
– http://httpd.apache.org/docs-2.0/mod/mod_mem_cache.html
16 © Novell Inc, Confidential & Proprietary
17. Apache Optimization
• Mod_Disk_Cache – Disk based storage manager (Experimental)
– CacheEnable
– CacheSize
– CacheMinFileSize
– CacheMaxFileSize
– http://httpd.apache.org/docs-2.0/mod/mod_disk_cache.html
17 © Novell Inc, Confidential & Proprietary
18. Apache Optimization
• Load Balancing with Mod_Proxy
– Most effect in environments where there is a lot of dynamic
content in a distributed environment
– Disable forward proxy with “ProxyRequests Off”
– Redirect requests to the specific backend servers with
“ProxyPass”
– Allow redirection headers to be fixed up with
“ProxyPassReverse”
– Use in conjunction with mod_mem_cache
– http://httpd.apache.org/docs-2.0/mod/mod_proxy.html
• Logging
– LogLevel Directive (ie LogLevel error)
18 © Novell Inc, Confidential & Proprietary
19. Apache Configuration
• The configuration for apache is handled in many
different files on SLES and therefore on OES. The
main ones are
• /etc/apache/httpd.conf
• /etc/apache/httpd.conf.local
• /etc/apache/conf.d/*
• /etc/sysconfig/apache2
19 © Novell Inc, Confidential & Proprietary
20. Apache Configuration
• /etc/apache/httpd.conf
• Don't edit this file. It is owned by the apache package
and will be over written if you install an update.
• /etc/apache/httpd.conf.local
• This is where you should make changes to you server
as it is parsed after /etc/apache/httpd.conf
20 © Novell Inc, Confidential & Proprietary
21. Apache Configuration
• /etc/apache/conf.d/*
• All conf files in this directory are parsed. This is the
best place to put specific module configuration (ala
jk.conf)
•
• /etc/sysconfig/apache2
• You can tell apache which modules to load here, as
well as what other conf files to parse.
•
• Apache has a LOT of options. Far to many to discuss
here.
21 © Novell Inc, Confidential & Proprietary
23. MySQL
• Popular database
• a lot of traffic in news groups
• open source
• excellent management tools
– phpMyAdmin
• clusterable
• easy to optimize
23 © Novell Inc, Confidential & Proprietary
24. MySQL Optimization
My.INI
Variable Default >512 MB >1 GB
key_buffer_size 16M 128 M 256M
table_cache 64 128 256
sort_buffer_size 2M 4M 8M
myisam_sort_buffer_size 8M 32M 64M
read_buffer_size 128K 1M 2M
24 © Novell Inc, Confidential & Proprietary
25. MySQL Optimization
• Use Indexes!!! - unique if possible
• NOT NULL – especially with indexed columns
• Fixed length fields
• Use binary logging.
• Use OPTIMIZE to defragment table files
• Use ANALYZE to store key distributions
• Use default values
• Do not SELECT *
• Use EXPLAIN to see how queries are performed
• –log-slow-queries
25 © Novell Inc, Confidential & Proprietary
27. PHP
• Very easy to use
• Powerful mysql integration
• Simple HTML integration
• Tied tightly to Apache
• Easy application deployment
27 © Novell Inc, Confidential & Proprietary
28. Scripting Optimization
• The default setting are good but you can make some
changes (for example logging, memory usage, how
long a script can run, . . .)
• In general let MySQL do the work for you. If you write
good queries and have optimized your tables you will
be in good shape.
28 © Novell Inc, Confidential & Proprietary
29. PHP Pages
It is remarkably easy to combine php and html and have
Apache render the pages for you correctly.
You embed php into a page by surrounding your code with a
scriptlet tag. In the case of php you use:
<?php . . . ?>
29 © Novell Inc, Confidential & Proprietary
30. PHP Pages
So our first php program could look something like this:
<?php
echo “Hello World”;
?>
As you may be able to guess this code would render Hello
World in a browser.
30 © Novell Inc, Confidential & Proprietary
31. PHP Pages
If we wanted to make this page compliant to the XHTML
spec it would look something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>NDK Self Service</title></head>
<body>
<?php
echo “<div>Hello World</div>”;
?>
</body>
</html>
31 © Novell Inc, Confidential & Proprietary
32. PHP Pages
We can do most anything inside those <?php . . . ?> tags
that any programming language can:
•conditional logic
•loops
•variables
•objects
•arrays
•parameter passing (via gets and posts)
•and the list goes on and on
32 © Novell Inc, Confidential & Proprietary
33. PHP Pages
We can do most anything inside those <?php . . . ?> tags
that any programming language can:
•conditional logic
•loops
•variables
•objects
•arrays
•parameter passing (via gets and posts)
•and the list goes on and on
33 © Novell Inc, Confidential & Proprietary
34. PHP – MySQL Functions
• mysql_affected_rows • mysql_fetch_field • mysql_info
• mysql_change_user • mysql_fetch_lengths • mysql_insert_id
• mysql_client_encoding • mysql_fetch_object • mysql_list_dbs
• mysql_close • mysql_fetch_row • mysql_list_processes
• mysql_connect • mysql_field_flags • mysql_num_fields
• mysql_create_db • mysql_field_len • mysql_num_rows
• mysql_data_seek • mysql_field_name • mysql_pconnect
• mysql_db_name • mysql_field_seek • mysql_ping
• mysql_db_query • mysql_field_table • mysql_query
• mysql_drop_db • mysql_field_type • mysql_real_escape_string
• mysql_errno • mysql_free_result • mysql_result
• mysql_error • mysql_get_client_info • mysql_select_db
• mysql_escape_string mysql_get_host_info
•
• mysql_stat
• mysql_fetch_array mysql_get_proto_info
•
• mysql_thread_id
• mysql_fetch_assoc mysql_get_server_info
•
• mysql_unbuffered_query
34 © Novell Inc, Confidential & Proprietary
35. PHP – Common MySQL Functions
• resource mysql_connect ( [string server [, string username [, string password [,
bool new_link [, int client_flags]]]]] )
• int mysql_errno ( [resource link_identifier] )
• string mysql_error ( [resource link_identifier] )
• array mysql_fetch_array ( resource result [, int result_type] )
• resource mysql_query ( string query [, resource link_identifier] )
• bool mysql_select_db ( string database_name [, resource link_identifier] )
35 © Novell Inc, Confidential & Proprietary
36. PHP – DB Query Example
<?php
//getting the return var is not strictly necessary
$conn = mysql_connect('localhost', 'admin', 'novell');
mysql_select_db('address_book', $conn);
$sql = 'SELECT name, address, email ' .
' FROM address_book ';
$rs = mysql_query($sql, $conn);
foreach (list($name, $address, $email) = mysql_fetch_row($rs))
{
echo ...
}
?>
36 © Novell Inc, Confidential & Proprietary
37. PHP – HTTP Parameter Passing
• HTTP parameters are accessible via “superglobal”
variables
• Use on of the following
– $_GET['<parameter name>']
– $_POST['<parameter name>']
– $_REQUEST['<parameter name>']
• It is better security to access your variables with
$_GET or $_POST (and only once at that).
37 © Novell Inc, Confidential & Proprietary
38. Other Useful Functions
• phpinfo()
This function display all sorts of useful information
regarding your php setup, and is a great tool for
debugging.
• var_dump()
Allows you to see the contents and type of your var.
38 © Novell Inc, Confidential & Proprietary
40. Deploying a LAMP Application
• One of the nicest things about LAMP is how easy the
applications are to deploy.
• If apache is set up correctly it is a simple matter of
copying your files into the directory that you want your
application to reside.
• You do not need to restart anything or deploy
anything, just save and copy.
40 © Novell Inc, Confidential & Proprietary
42. Applications
• phpMyAdmin (http://www.phpmyadmin.net)
• xoops (http://www.xoops.org)
• Most everything at http://www.hotscripts.com/ should
work.
42 © Novell Inc, Confidential & Proprietary
43. Migration
Generally there are no migration issues. Some things you
may want to look for in migrating a LAMP application are:
Embedded path problems
System calls
43 © Novell Inc, Confidential & Proprietary
44. References
• http://www.mysql.com
• http://www.php.net
• http://www.apache.org
44 © Novell Inc, Confidential & Proprietary
46. Unpublished Work of Novell, Inc. All Rights Reserved.
This work is an unpublished work and contains confidential, proprietary, and trade secret information of Novell,
Inc. Access to this work is restricted to Novell employees who have a need to know to perform tasks within the
scope of their assignments. No part of this work may be practiced, performed, copied, distributed, revised,
modified, translated, abridged, condensed, expanded, collected, or adapted without the prior written consent of
Novell, Inc. Any use or exploitation of this work without authorization could subject the perpetrator to criminal
and civil liability.
General Disclaimer
This document is not to be construed as a promise by any participating company to develop, deliver, or market
a product. Novell, Inc., makes no representations or warranties with respect to the contents of this document,
and specifically disclaims any express or implied warranties of merchantability or fitness for any particular
purpose. Further, Novell, Inc., reserves the right to revise this document and to make changes to its content,
at any time, without obligation to notify any person or entity of such revisions or changes. All Novell marks
referenced in this presentation are trademarks or registered trademarks of Novell, Inc. in the United States and
other countries. All third-party trademarks are the property of their respective owners.