Lecture 9: Applications: Apache, PHP and MySQL Asoc. Prof. Guntis Barzdins Asist. Girts Folkmanis,...

Post on 26-Dec-2015

217 views 0 download

Transcript of Lecture 9: Applications: Apache, PHP and MySQL Asoc. Prof. Guntis Barzdins Asist. Girts Folkmanis,...

Lecture 9: Applications: Apache, PHP and MySQL

Asoc. Prof. Guntis BarzdinsAsist. Girts Folkmanis, Arnis Sinka

University of LatviaNov 26, 2004

Outline

Apache Dynamic Content

CGI PHP

MySQL

Apache

what is Apache? Apache’s functionality installing Apache directory structure configuration tools

If you request an HTML file

Browser Webserver

HTML

1 23

4

Web server

...is a software program that does the following Accepts requests for web pages from a browser. Looks for the requested pages on the server hard drive. Sends a copy of the the requested web page to the browser. A web server can only serve HTML and jpg/gif files

In our case, we use a very popular web server called Apache.

Apache

open-source very popular (more than 67% of the web sites) highly configurable and extensible with third-party

modules runs on many operating systems (most of the

Unix) is actively being developed

Apache functionality

DBM databases for authentication customized responses to errors and problems unlimited flexible URL rewriting and aliasing Virtual Hosts Configurable Reliable Piped Logs

Apache modules (1) mod_access

Access control based on client hostname or IP address

mod_alias Mapping different parts of the host filesystem in the document tree, and URL

redirection mod_auth

User authentication using text files

mod_autoindex Automatic directory listings

mod_cgi Invoking CGI scripts

Apache modules (2) mod_include

Server-parsed documents

mod_mime Determining document types using file extensions

mod_proxy Caching proxy abilities

mod_rewrite Powerful URI-to-filename mapping using regular expressions

mod_usertrack User tracking using Cookies

mod_vhost_alias Support for dynamically configured mass virtual hosting

Apache modules (3)

mod_ssl This module provides strong cryptography for the Apache 1.3

webserver via the Secure Sockets Layer (SSL) and Transport Layer Security (TLS) protocols by the help of the Open Source SSL/TLS toolkit OpenSSL.

Requires Apache 1.3.x and OpenSSL 0.9.x Private and Public keys Thawte (www.thawte.com), Versisign (www.verisign.com)

Installing Apache

Unix binary package RPM DEB

Source Windows (MSI Installer)

Installing Apache

$ ./configure --prefix=/usr/local/apache

$ make$ make install$ /usr/local/apache/bin/apachectl

start

Installing Apache

./configure –help --show-layout

show GNU style directory layout --with-layout=GNU

Use GNU style directory layout --enable-suexec

Enable suEXEC support for CGI and SSI --add-module=/path/to/mod_foo.c

compiles, installs and adds module as a Dynamic Shared Object

Testing Apache installation

arnis@perkons:~$ ps aux | grep apacheroot 289 0.0 0.2 8400 2564 ? Ss Nov15 0:02 /usr/local/apache/bin/httpdroot 307 0.0 0.1 8764 1480 ? Ss Nov15 0:00 /usr/local/apache-ssl/bin/httpd -DSSLapache- 315 0.0 0.1 14768 1580 ? S Nov15 0:27 /usr/local/apache-ssl/bin/httpd -DSSLapache- 13822 0.0 0.2 15224 2644 ? S Nov15 0:26 /usr/local/apache-ssl/bin/httpd -DSSLapache 11290 0.0 0.3 16856 3112 ? S Nov17 0:31 /usr/local/apache/bin/httpdapache 498 0.2 0.8 12596 8484 ? S Nov18 8:54 /usr/local/apache/bin/httpd

....

Testing Apache installation

Apache directory layout

Debian /etc/init.d/apache

Apache control script /etc/apache

Apache configuration files /var/www

Default Document Root /usr/lib/cgi-bin

Default script directory

Apache directory layout (2)

/var/log/apache log files (access.log, error.log)

/usr/sbin rotatelogs, ab (Apache Benchmark)

/usr/bin htpasswd, htdigest, dbmmanage

/usr/lib/apache/1.3 Apache modules

/usr/lib/apache/suexec

Apache directory layout (3)

Slackware /usr/local/apache /usr/local/apache/conf /usr/local/apache/htdocs /usr/local/apache/cgi-bin /var/log/apache /usr/local/apache/bin

Apache access log

LogFormat "%v %h %l %u %t \"%r\" %>s %b" commonCustomLog /usr/local/apache/logs/access_log common

%v – virtual host %h – remote host %u – user %t - time %r – HTTP request %>s – status code %b – size

www.atlants.lv 159.148.85.46 - - [21/Nov/2004:17:23:36 +0200] "GET /index.php?m=5 HTTP/1.1" 200 32257

Apache error log

ErrorLog /usr/local/apache/logs/error_logLogLevel warn

[Sun Nov 21 09:13:42 2004] [error] PHP Fatal error: Call to undefined function PN_DBMsgError() in /home/msaule/public_html/referer.php on line 85[Sun Nov 21 12:41:09 2004] [error] [client 81.198.145.117] File does not exist: /home/sms/public_html/favicon.icophp on line 85[Sun Nov 21 13:02:50 2004] [error] [client 66.249.66.173] File does not exist: /home/code/public_html/robots.txt[Sun Nov 21 13:08:26 2004] [error] [client 81.198.176.114] File does not exist: /home/refuser2/public_html/_vti_bin/owssvr.dll[Sun Nov 21 13:08:26 2004] [error] [client 81.198.176.114] File does not exist: /home/refuser2/public_html/MSOffice/cltreq.asp[Sun Nov 21 13:09:07 2004] [error] [client 81.198.176.114] File does not exist: /home/refuser2/public_html/_vti_bin/owssvr.dll[Sun Nov 21 13:09:07 2004] [error] [client 81.198.176.114] File does not exist: /home/refuser2/public_html/MSOffice/cltreq.asp

Apache configuration

Edit httpd.conf Check configuration “apachectl configtest” Restart Apache Check changes

http://httpd.apache.org/docs/

Apache configuration

Virtual host<VirtualHost *> ServerName www.jrt.lv ServerAlias www.jrt.com CustomLog /usr/local/apache/logs/jrt_access_log common ErrorLog /usr/local/apache/logs/jrt_error_log DocumentRoot /home/jrt/public_html</VirtualHost>

Apache configuration

.htaccessAuthType Basic AuthUserFile /home/someuser/passwdAuthName "Admin" require valid-user

htpasswdhtpasswd -c <password file> <username>

user1:Y90u499mUj6xEuser2:DOrWgcNwzaQUQ

Apache2

Unix Threading New Build System Multiprotocol Support New Apache API IPv6 Support Filtering Multilanguage Error Responses Regular Expression Library Updated

Dynamic content

Script Engine (PHP, Perl, ...)

Browser Webserver

HTML&

Scripts

1 2

3

4

56

Dynamic content

Scripting engine CGI PHP Apache module vs. CGI

Dynamic content

Apache only sends content to the user What if I need some resources/information from server

Send e-mail Store some information in file (guestbook) Execute unix applications And much more...

We need programming language

Dynamic content

Script engine is a software program that does the following: Accepts scripts passed along from the web server that

are of the non-HTML type. Processes these scripts. Returns the result of this processing to the web

server.

Dynamic content

Two ways how to server dynamic content CGI Apache module

Many programming languages to use PHP, Perl, Python, C, C++, shell scripts ...

Common gateway interface (CGI)

A standard for running external programs from a World-Wide Web HTTP server. CGI specifies how to pass arguments to the executing program as part of the HTTP request. It also defines a set of environment variables. Commonly, the program will generate some HTML which will be passed back to the browser but it can also request URL redirection.

CGI example

Shell script

#!/bin/bashecho "Content-type: text/plain"echo ""echo "Hello world!"echo "Today is:" `date`

CGI example (2)

Perl script

#!/usr/bin/perlprint "Content-type: text/plain\n\n";print "Hello world!\n";print "Today is: " . localtime() . "\n";

Apache modules

mod_perlmod_perl brings together the full power of the Perl programming

language and the Apache HTTP server. You can use Perl to manage Apache, respond to requests for web pages and much more.

mod_phpPHP is a widely-used general-purpose scripting language that is

especially suited for Web development and can be embedded into HTML

mod_python, OpenASP Module, ...

PHP

What is PHP? Installing PHP Configuring PHP

PHP: Hypertext Preprocessor(PHP)

<html>   <head>       <title>Example</title>   </head>   <body>

       <?php        echo "Hi, I'm a PHP script!";        ?>

   </body></html>

PHPPros

easy to learn ideal for small projects widely used no strong typing

Cons no strong typing code maintenance interpreted language executes in the Web server process

Installing PHP

Server-side scripting

Command line scripting

Client-side GUI applications

Installing PHP

Gentoo# emerge \<apache-2 # USE="-*" emerge php mod_php # ebuild /var/db/pkg/dev-php/mod_php-<your PHP

version>/mod_php-<your PHP version>.ebuild config

# nano /etc/conf.d/apache Add "-D PHP4" to APACHE_OPTS # rc-update add apache default

# /etc/init.d/apache start

Installing PHP

Source instalation Install PHP./configure --with-mysql --with-apxs=/www/bin/apxsmakemake installcp php.ini-dist /usr/local/lib/php.ini Edit your httpd.conf to load the PHP module. LoadModule php4_module libexec/libphp4.so AddModule mod_php4.c AddType application/x-httpd-php .php .phtml Restart Apache

PHP Configuration

php.ini read once at web server startup

; any text on a line after an unquoted semicolon (;) is ignored

[php] ; section markers (text within square brackets) are also ignored

; Boolean values can be set to either: ; true, on, yes

; or false, off, no, none

register_globals = off

track_errors = yes

; you can enclose strings in double-quotes

include_path = ".:/usr/local/lib/php"

PHP Configuration

php.ini directivesmax_execution_time = 30 ; Maximum execution time of each script, in

seconds max_input_time = 60 ; Maximum amount of time each script may spend

parsing request data memory_limit = 8M ; Maximum amount of memory a script may consume

(8MB)

; - Show all errors except for notices and coding standards warningserror_reporting = E_ALL & ~E_NOTICE & ~E_STRICT display_errors = Offlog_errors = Onerror_log = filename

PHP Configuration

Apache configuration file<VirtualHost 10.10.10.10>

DocumentRoot /home/someuser/public_htmlServerName www.somesite.lv<Directory /home/someuser/public_html/> php_admin_value open_basedir /home/someuser/:/tmp/:/usr/share/pear/ php_value auto_prepend_file /home/someuser/includes/default.inc php_value upload_max_filesize 10M</Directory>

</VirtualHost>

PHP Configuration

.htaccess fileAddType application/x-httpd-php .php3php_value include_path .:/home/someuser/includes:/home/someuser/public_htmlphp_flag register_globals Off

PHP scripts<?ini_set("display_errors", "true");ini_set("error_log","/home/someuser/log/php.log");...

Apache module vs. CGI

Apache module Good performance One user for all websites

Other user’s source files can be accessed PHP safe_mode

CGI New process each time suEXEC – each website under its own user

fastCGI

Apache, PHP and MySQL

PHP Engine

Browser Webserver

MySQL DatabaseServer

HTML&

PHP

1 2

3

4

56

78

MySQL

About MySQL Installing MySQL MySQL directory structure MySQL commands Some examples PHPMyAdmin

MySQL

Open source Very fast Stable Easy to use Independant storage engines

Can be run with or without transaction control Security

SSL support Resources configurable per user basis

MySQL 4.x

Subqueries New client-server protocol with prepared

statements Unicode and UTF-8 support Query cashing Much more...

Installing MySQL

Binary distributionshell> groupadd mysqlshell> useradd -g mysql mysqlshell> cd /usr/localshell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -shell> ln -s full-path-to-mysql-VERSION-OS mysqlshell> cd mysqlshell> scripts/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql datashell> chgrp -R mysql .shell> bin/mysqld_safe --user=mysql &

Installing MySQL

Source distributionshell> groupadd mysqlshell> useradd -g mysql mysqlshell> gunzip < mysql-VERSION.tar.gz | tar -xvf -shell> cd mysql-VERSIONshell> ./configure --prefix=/usr/local/mysqlshell> makeshell> make installshell> cp support-files/my-medium.cnf /etc/my.cnfshell> cd /usr/local/mysqlshell> bin/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql varshell> chgrp -R mysql .shell> bin/mysqld_safe --user=mysql &

Post-Instalation Procedures

Check instalation shell> bin/mysqladmin version

Create system tables shell> bin/mysql_install_db --user=mysql

Make nessesary databases and users CREATE DATABASE GRANT

MySQL directory structure

./ MySQL server control scripts

bin/ MySQL server, MySQL client and commandline tools

data/ Databases – directories Tables – files (MYD, MYI,FRM)

var/log Log files

MySQL binaries

mysql MySQL client

mysqladmin MySQL administration tool

mysqldump Tool for creating database dumps

MySQL commands

CREATE DATABASE <database name> DROP GRANT ALL PRIVILEGES on database.* to

user@localhost IDENTIFIED BY ‘password’ Privilege type (ALL, ALTER, CREATE, DELETE, INSERT,

SELECT, GRANT, ...) Privilege level (globa, database, table, column) User and host (localhost, IP address, network, %)

REVOKE

PHP and database example

MySQL and SQLite Examples

PHPMyAdmin

phpMyAdmin is a tool written in PHP intended to handle the administration of MySQL over the Web (http://www.phpmyadmin.net/)

CREATE/DROP databases CREATE/DROP/ALTER tables Delete/add/edit/search information Execute SQL queries Manage privileges Export data