lamp introduction 20100419

46
March 24, 2006 Introduction to LAMP Cory Aitchison Software Engineer Novell Inc. [email protected]

Upload: hung-nguyen-vu

Post on 14-Dec-2014

1.346 views

Category:

Documents


0 download

DESCRIPTION

aaa

TRANSCRIPT

Page 1: Lamp Introduction 20100419

March 24, 2006

Introduction to LAMPCory AitchisonSoftware Engineer

Novell [email protected]

Page 2: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

2

Session Overview

• Introduction to LAMP• Installation on OES Linux• Optimization• Some available applications• Migration• Creating Lamp Applications

Page 3: Lamp Introduction 20100419

Introduction to LAMP

Page 4: Lamp Introduction 20100419

© 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

Page 5: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

5

Stack Model

mod_php

PHP

MySQL

Linux/OES

Hardware

Apache

Page 6: Lamp Introduction 20100419

© 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

Page 7: Lamp Introduction 20100419

© 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

Page 8: Lamp Introduction 20100419

© 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

Page 9: Lamp Introduction 20100419

Installation

Page 10: Lamp Introduction 20100419

© 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

Page 11: Lamp Introduction 20100419

© 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

Page 12: Lamp Introduction 20100419

© 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

Page 13: Lamp Introduction 20100419

Apache

Page 14: Lamp Introduction 20100419

© 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/

Page 15: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

15

Popular Apache Modules

• mod_rewrite• mod_php• prefork• mod_auth• mod_cgi• optimization modules in following slides

Page 16: Lamp Introduction 20100419

© 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

Page 17: Lamp Introduction 20100419

© 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

Page 18: Lamp Introduction 20100419

© 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)

Page 19: Lamp Introduction 20100419

© 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

Page 20: Lamp Introduction 20100419

© 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

Page 21: Lamp Introduction 20100419

© 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.

Page 22: Lamp Introduction 20100419

MySQL

Page 23: Lamp Introduction 20100419

© 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

Page 24: Lamp Introduction 20100419

© 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

Page 25: Lamp Introduction 20100419

© 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

Page 26: Lamp Introduction 20100419

PHP

Page 27: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

27

PHP

• Very easy to use• Powerful mysql integration• Simple HTML integration• Tied tightly to Apache• Easy application deployment

Page 28: Lamp Introduction 20100419

© 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.

Page 29: Lamp Introduction 20100419

© 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 . . . ?>

Page 30: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

30

PHP Pages

So our first php program could look something like this:

<?phpecho “Hello World”;

?>

As you may be able to guess this code would render Hello World in a browser.

Page 31: Lamp Introduction 20100419

© 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>

<?phpecho “<div>Hello World</div>”;

?></body></html>

Page 32: Lamp Introduction 20100419

© 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

Page 33: Lamp Introduction 20100419

© 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

Page 34: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

34

PHP – MySQL Functions

• mysql_affected_rows• mysql_change_user• mysql_client_encoding• mysql_close• mysql_connect• mysql_create_db• mysql_data_seek• mysql_db_name• mysql_db_query• mysql_drop_db• mysql_errno• mysql_error• mysql_escape_string• mysql_fetch_array• mysql_fetch_assoc

• mysql_fetch_field• mysql_fetch_lengths• mysql_fetch_object• mysql_fetch_row• mysql_field_flags• mysql_field_len• mysql_field_name• mysql_field_seek• mysql_field_table• mysql_field_type• mysql_free_result• mysql_get_client_info• mysql_get_host_info• mysql_get_proto_info• mysql_get_server_info

• mysql_info• mysql_insert_id• mysql_list_dbs• mysql_list_processes• mysql_num_fields• mysql_num_rows• mysql_pconnect• mysql_ping• mysql_query• mysql_real_escape_string• mysql_result• mysql_select_db• mysql_stat• mysql_thread_id• mysql_unbuffered_query

Page 35: Lamp Introduction 20100419

© 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] )

Page 36: Lamp Introduction 20100419

© 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 ...}

?>

Page 37: Lamp Introduction 20100419

© 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).

Page 38: Lamp Introduction 20100419

© 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.

Page 39: Lamp Introduction 20100419

Deploying a LAMP Application

Page 40: Lamp Introduction 20100419

© 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.

Page 41: Lamp Introduction 20100419

Available Applications/Migration

Page 42: Lamp Introduction 20100419

© 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.

Page 43: Lamp Introduction 20100419

© 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 problemsSystem calls

Page 44: Lamp Introduction 20100419

© Novell Inc, Confidential & Proprietary

44

References

• http://www.mysql.com• http://www.php.net• http://www.apache.org

Page 45: Lamp Introduction 20100419
Page 46: Lamp Introduction 20100419

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 DisclaimerThis 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.