A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first...

98
A202/A590 CGI/PHP Fall 2012 Class Notes c Adrian German August-October 2012

Transcript of A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first...

Page 1: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

A202/A590 CGI/PHPFall 2012 Class Notes

c⃝ Adrian German

August-October 2012

Page 2: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

To A202/A598, A290/A590 (both CGI/PHP and Java) and to A348/A548.

Page 3: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Preface

These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. Theywill be updated and supplemented with web notes during the semester.

1

Page 4: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...
Page 5: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Calendar

-bash-4.1$ cal 2012

2012

January February March

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7 1 2 3 4 1 2 3

8 9 10 11 12 13 14 5 6 7 8 9 10 11 4 5 6 7 8 9 10

15 16 17 18 19 20 21 12 13 14 15 16 17 18 11 12 13 14 15 16 17

22 23 24 25 26 27 28 19 20 21 22 23 24 25 18 19 20 21 22 23 24

29 30 31 26 27 28 29 25 26 27 28 29 30 31

April May June

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7 1 2 3 4 5 1 2

8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9

15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16

22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23

29 30 27 28 29 30 31 24 25 26 27 28 29 30

July August September

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7 1 2 3 4 1

8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8

15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15

22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22

29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29

30

October November December

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 1 2 3 1

7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8

14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15

21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22

28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29

30 31

3

Page 6: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

4

-bash-4.1$

Page 7: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Schedule

Two lectures per week, no lab, office hours1.

1http://silo.cs.indiana.edu:8346/cgi-bin/fall2012/schedule

5

Page 8: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

6

Page 9: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Instructors

Your primary instructor will be Adrian German. He will be assisted by SteveGehrig and Mike Bestvina.

7

Page 10: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

8

Page 11: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Grades

As discussed in the first lecture (info will be repeated here).

A Note on Assignments

Most everything can be made up if turned in on time, originally.

Class Website

There is a website2 in three parts that will be updated often.

2http://www.cs.indiana.edu/classes/a290-web/

9

Page 12: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

10

Page 13: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Part I

The Basic Web

11

Page 14: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...
Page 15: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 1

Unix

Each one of you will get an account on silo.cs.indiana.edu where you will domost of your work. You will need to become familiar with the Unix environment(file system and a number of Unix commands) on silo. We will guide youthrough an exercise below.

We start by identifying where we are. We then check that we don’t alreadyhave a folder named lab1 and create it. We move inside it, and create a foldernamed experiments inside. Move inside experiments and create two morefolders there, next to each other. Call them documents and programs.

Move inside the first and create two empty files with the names doc1.txt anddoc2.txt. Next open the doc1.txt file and write your name in it or somethingthat identifies you. Then open the second one and write something that wedon’t know about you, like I did below:

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld lab1

ls: cannot access lab1: No such file or directory

[tblgrant@silo ~]$ mkdir lab1

[tblgrant@silo ~]$ cd lab1

[tblgrant@silo lab1]$ mkdir experiments

[tblgrant@silo lab1]$ cd experiments/

[tblgrant@silo experiments]$ mkdir documents

[tblgrant@silo experiments]$ mkdir programs

[tblgrant@silo experiments]$ ls -l

total 8

drwx------ 2 tblgrant projects 4096 Jun 17 15:40 documents

drwx------ 2 tblgrant projects 4096 Jun 17 15:40 programs

[tblgrant@silo experiments]$ cd documents

[tblgrant@silo documents]$ ls

[tblgrant@silo documents]$ touch doc1.txt

[tblgrant@silo documents]$ ls -l

total 0

-rw------- 1 tblgrant projects 0 Jun 17 15:40 doc1.txt

13

Page 16: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

14

[tblgrant@silo documents]$ touch doc2.txt

[tblgrant@silo documents]$ ls -l

total 0

-rw------- 1 tblgrant projects 0 Jun 17 15:40 doc1.txt

-rw------- 1 tblgrant projects 0 Jun 17 15:40 doc2.txt

[tblgrant@silo documents]$ pico -w doc1.txt

[tblgrant@silo documents]$ ls -l

total 4

-rw------- 1 tblgrant projects 26 Jun 17 15:40 doc1.txt

-rw------- 1 tblgrant projects 0 Jun 17 15:40 doc2.txt

[tblgrant@silo documents]$ cat doc1.txt

My name is Adrian German.

[tblgrant@silo documents]$ cat doc2.txt

[tblgrant@silo documents]$ pico -w doc2.txt

[tblgrant@silo documents]$ ls -l

total 8

-rw------- 1 tblgrant projects 26 Jun 17 15:40 doc1.txt

-rw------- 1 tblgrant projects 85 Jun 17 15:42 doc2.txt

[tblgrant@silo documents]$ cat doc2.txt

There’s a book by Moore and Mertens, "Tha Nature of Computation, that I like a lot.

[tblgrant@silo documents]$ pico -w doc2.txt

[tblgrant@silo documents]$ ls -ld doc2.txt

-rw------- 1 tblgrant projects 94 Jun 17 15:43 doc2.txt

[tblgrant@silo documents]$ cat doc2.txt

There’s a book by Moore and Mertens, entitled

"The Nature of Computation" that I really like a lot.

[tblgrant@silo documents]$ cat doc1.txt

My name is Adrian German.

[tblgrant@silo documents]$ pwd

/u/tblgrant/lab1/experiments/documents

Now that you have your .txt files go to programs and create three folders.Name them perl, python and Java. Go into Java and write your first Javaprogram using the code we provide below. Compile and run it with javac1

and java2. Notice that these commands are available without any additionalsetting. On the PCs in the STC labs we may need to set the PATH environmentvariable. We will need to set the PATH environment variable (and a few otherenvironment variables) on the Unix account too, but not for compiling andrunning a Java program.

[tblgrant@silo documents]$ pwd

/u/tblgrant/lab1/experiments/documents

[tblgrant@silo documents]$ cd ..

[tblgrant@silo experiments]$ ls

documents programs

[tblgrant@silo experiments]$ cd programs/

[tblgrant@silo programs]$ mkdir python

1The Java compiler.2The Java interpreter.

Page 17: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

15

[tblgrant@silo programs]$ mkdir perl

[tblgrant@silo programs]$ mkdir Java

[tblgrant@silo programs]$ cd Java

[tblgrant@silo Java]$ pico -w One.java

[tblgrant@silo Java]$ ls -l

total 4

-rw------- 1 tblgrant projects 114 Jun 17 15:44 One.java

[tblgrant@silo Java]$ cat One.java

public class One {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

[tblgrant@silo Java]$ javac One.java

[tblgrant@silo Java]$ java One

Welcome to Java!

[tblgrant@silo Java]$ pwd

/u/tblgrant/lab1/experiments/programs/Java

Next move into each of the other two folders you have created for programsand write and run your first Python and Perl programs. Unlike the Java pro-grams here the interpreter is called implicitly and the file needs to be executable.

[tblgrant@silo Java]$ pwd

/u/tblgrant/lab1/experiments/programs/Java

[tblgrant@silo Java]$ cd ../python/

[tblgrant@silo python]$ pico -w one

[tblgrant@silo python]$ ls -l

total 4

-rw------- 1 tblgrant projects 34 Jun 17 15:45 one

[tblgrant@silo python]$ chmod 700 one

[tblgrant@silo python]$ ls -ld one

-rwx------ 1 tblgrant projects 34 Jun 17 15:45 one

[tblgrant@silo python]$ ./one

Howdy.

[tblgrant@silo python]$ cd ..

[tblgrant@silo programs]$ cd perl

[tblgrant@silo perl]$ pico -w one

[tblgrant@silo perl]$ ls -ld one

-rw------- 1 tblgrant projects 33 Jun 17 15:45 one

[tblgrant@silo perl]$ cat one

#!/usr/bin/perl

print "Howdy.";

[tblgrant@silo perl]$ chmod u+x one

[tblgrant@silo perl]$ ls -ld one

-rwx------ 1 tblgrant projects 33 Jun 17 15:45 one

[tblgrant@silo perl]$ ./one

Howdy.[tblgrant@silo perl]$ pwd

/u/tblgrant/lab1/experiments/programs/perl

Page 18: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

16

Time has come to wrap up all of this work. Go back up into lab1 and lookat all the files you have created. Use the du -a . command3. Create an archivewhoa.tar using the tar utility. Compress it with gzip.

[tblgrant@silo perl]$ pwd

/u/tblgrant/lab1/experiments/programs/perl

[tblgrant@silo perl]$ cd ..

[tblgrant@silo programs]$ pwd

/u/tblgrant/lab1/experiments/programs

[tblgrant@silo programs]$ cd ../../

[tblgrant@silo lab1]$ ls -l

total 4

drwx------ 4 tblgrant projects 4096 Jun 17 15:40 experiments

[tblgrant@silo lab1]$ du -a .

4 ./experiments/documents/doc2.txt

4 ./experiments/documents/doc1.txt

12 ./experiments/documents

4 ./experiments/programs/perl/one

8 ./experiments/programs/perl

4 ./experiments/programs/python/one

8 ./experiments/programs/python

4 ./experiments/programs/Java/One.java

4 ./experiments/programs/Java/One.class

12 ./experiments/programs/Java

32 ./experiments/programs

48 ./experiments

52 .

[tblgrant@silo lab1]$ tar cvf whoa.tar experiments/

experiments/

experiments/documents/

experiments/documents/doc2.txt

experiments/documents/doc1.txt

experiments/programs/

experiments/programs/perl/

experiments/programs/perl/one

experiments/programs/python/

experiments/programs/python/one

experiments/programs/Java/

experiments/programs/Java/One.java

experiments/programs/Java/One.class

[tblgrant@silo lab1]$ ls -l

total 16

drwx------ 4 tblgrant projects 4096 Jun 17 15:40 experiments

-rw------- 1 tblgrant projects 10240 Jun 17 15:46 whoa.tar

[tblgrant@silo lab1]$ gzip whoa.tar

[tblgrant@silo lab1]$ ls -l

total 8

drwx------ 4 tblgrant projects 4096 Jun 17 15:40 experiments

3By dot we indicate the current folder. Try man du as well

Page 19: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

17

-rw------- 1 tblgrant projects 888 Jun 17 15:46 whoa.tar.gz

[tblgrant@silo lab1]$

Copy the compressed archive4 to your ~/public folder. Make the archiveitself readable (chmod 644) by everybody, make the ~/public folder accessibleand readable to everybody (755, which is what a really public folder is supposedto be like) and make your home folder permissible (711) to those who try toaccess public resources that you’ve made available to the world.

[tblgrant@silo lab1]$ ls -ld ~/public

drwxr-xr-x 2 tblgrant projects 4096 Dec 9 2011 /u/tblgrant/public

[tblgrant@silo lab1]$ cp whoa.tar.gz ~/public/

[tblgrant@silo lab1]$ ls -ld ~/public/whoa.tar.gz

-rw-r--r-- 1 tblgrant projects 888 Jun 17 15:47 /u/tblgrant/public/whoa.tar.gz

[tblgrant@silo lab1]$ chmod 644 ~/public/whoa.tar.gz

[tblgrant@silo lab1]$ chmod 755 ~/public

[tblgrant@silo lab1]$ chmod 711 ~

[tblgrant@silo lab1]$

Now from my own account I will be able to access and grade your work:

-bash-4.1$ pwd

/u/dgerman/grading

-bash-4.1$ ls

alpha

-bash-4.1$ ./alpha tblgrant

8 experiments/programs/python/one

16 experiments/programs/python

8 experiments/programs/perl/one

16 experiments/programs/perl

8 experiments/programs/Java/One.java

8 experiments/programs/Java/One.class

24 experiments/programs/Java

64 experiments/programs

8 experiments/documents/doc1.txt

8 experiments/documents/doc2.txt

24 experiments/documents

96 experiments

::::::::::::::

experiments/documents/doc1.txt

::::::::::::::

My name is Adrian German.

::::::::::::::

experiments/documents/doc2.txt

::::::::::::::

There’s a book by Moore and Mertens, entitled

"The Nature of Computation, that I like a lot.

-bash-4.1$

4whoa.tar.gz

Page 20: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

18

Page 21: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 2

Apache

2.1 Downloading Apache

The Apache server can be downloaded online from the project website1. Thereis a special page for downloads2. The current stable release, as of the time Iwrite this, is 2.4.23. We however will start by installing the 2.2.2 version4 of theApache server. Furthermore, since all these links are likely to change in time,and to ensure a greater reliability for this process we have provided a local copyof the server software5 on silo. Here’s how you get your copy6:

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls httpd*

ls: cannot access httpd*: No such file or directory

[tblgrant@silo ~]$ ls -ld apache

ls: cannot access apache: No such file or directory

[tblgrant@silo ~]$ ls /l/www/classes/a202/software/httpd-2.2.2.tar.gz

/l/www/classes/a202/software/httpd-2.2.2.tar.gz

[tblgrant@silo ~]$ cp /l/www/classes/a202/software/httpd-2.2.2.tar.gz .

[tblgrant@silo ~]$ ls httpd*

httpd-2.2.2.tar.gz

[tblgrant@silo ~]$ ls -ld httpd-2.2.2.tar.gz

-rw------- 1 tblgrant projects 6282043 Jun 12 10:48 httpd-2.2.2.tar.gz

[tblgrant@silo ~]$

We start with pwd which identifies the location (the folder and the owner).

We then look for files (or folders) whose name is apache or start with �httpd.

1http://httpd.apache.org/2http://httpd.apache.org/download.cgi3http://httpd.apache.org/download.cgi#apache244http://httpd.apache.org/download.cgi#apache225You can find it in /l/www/classes/a202/software6Notice that the prompts appear in red, the commands issued appear in blue and the shell’s

responses are in black. In the snapshot session presented above my username is tblgrant.

19

Page 22: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

20

There aren’t any, so we look for the promised archive in the class softwarefolder. We find it so we copy it in the current folder (which is identified in thecommand line with a dot—–at the end of the line, as the second argument ofcp). As we copy the compressed (.gz) archive (tar) we also make a note of itssize (around 6.2 megabytes). It will have to be uncompressed. We will see thenif compression really makes any difference.

Installing Apache

Installing Apache starts with compressing and unarchiving the downloaded soft-ware bundle.

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls httpd*

httpd-2.2.2.tar.gz

[tblgrant@silo ~]$ ls -ld httpd-2.2.2.tar.gz

-rw------- 1 tblgrant projects 6282043 Jun 12 10:48 httpd-2.2.2.tar.gz

[tblgrant@silo ~]$ gunzip httpd-2.2.2.tar.gz

[tblgrant@silo ~]$ ls -ld htt*

-rw------- 1 tblgrant projects 30371840 Jun 12 10:48 httpd-2.2.2.tar

[tblgrant@silo ~]$ tar xvf httpd-2.2.2.tar

httpd-2.2.2/

httpd-2.2.2/os/

httpd-2.2.2/os/os2/

httpd-2.2.2/os/os2/os.h

httpd-2.2.2/os/os2/core.mk

[...]

httpd-2.2.2/include/http_core.h

httpd-2.2.2/include/httpd.h

httpd-2.2.2/include/http_main.h

httpd-2.2.2/include/ap_release.h

httpd-2.2.2/include/.indent.pro

httpd-2.2.2/include/util_cfgtree.h

[tblgrant@silo ~]$

The net result of these commands is that the compressed archive that hadjust been copied copied is uncompressed and unarchived in my account. Theunarchiving creates a folder httpd-2.2.2 which has everything I need to getstarted. I am now ready to really start installing Apache.

The only thing I need before I start is a port number. The instructor willdistribute the port numbers on the course website. With my identity (username)of tblgrant let’s assume that my assigned port number is 61200. Let’s alsoassume at this time Apache port numbers 61201 for Anna (annaeile) and61202 for Ryan (rypaulse).

Here’s how I install Apache: I go into the newly created folder and lookaround. There is a file INSTALL that I look at. It has very simple installationinstructions. I decide to follow them exactly. They ask me to run a piece of

Page 23: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

21

software (a program) that the developers have provided (it’s called configure)with a command line argument indicating the location of my installed server.The configuration utility produces a Makefile with instructions for compilingthe server from source and installing the resulting files. Once configure ends,I run make to compile and make install to place the compiled files and theirancillaries in the right place(s). At this point I’d be ready to start the server ifmy port had been specified anywhere (which it hasn’t—yet).

Step by step, up to this point, it looks like this:

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld apache

ls: cannot access apache: No such file or directory

[tblgrant@silo ~]$ ls -ld http*

drwx------ 11 tblgrant projects 4096 Apr 21 2006 httpd-2.2.2

-rw------- 1 tblgrant projects 30371840 Jun 12 10:48 httpd-2.2.2.tar

[tblgrant@silo ~]$ rm httpd-2.2.2.tar

[tblgrant@silo ~]$ cd httpd-2.2.2/

[tblgrant@silo httpd-2.2.2]$ ls

ABOUT_APACHE configure LAYOUT README

acinclude.m4 configure.in libhttpd.dsp README.platforms

Apache.dsw docs LICENSE ROADMAP

apachenw.mcp.zip emacs-style Makefile.in server

build httpd.dsp Makefile.win srclib

BuildBin.dsp httpd.spec modules support

buildconf include NOTICE test

CHANGES INSTALL NWGNUmakefile VERSIONING

config.layout InstallBin.dsp os

[tblgrant@silo httpd-2.2.2]$ more INSTALL

APACHE INSTALLATION OVERVIEW

Quick Start - Unix

------------------

For complete installation documentation, see [ht]docs/manual/install.html or

http://httpd.apache.org/docs-2.2/install.html

+-------------------------------------+

| |

| $ ./configure --prefix=PREFIX |

| $ make |

| $ make install |

| $ PREFIX/bin/apachectl start |

| |

+-------------------------------------+

NOTES: * Replace PREFIX with the filesystem path under which [...]

The box belongs to me, as a manner of highlighting, but the rest is exactly asyou will find it in the files. Next, I start configure (I run it from the current

Page 24: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

22

folder) with the expected prefix. Once it finishes I take a look at the file (it’sfairly big for a script, but it does a lot of things) and I check on the files created:

[tblgrant@silo httpd-2.2.2]$ pwd

/u/tblgrant/httpd-2.2.2

[tblgrant@silo httpd-2.2.2]$ ./configure --prefix=/u/tblgrant/apache

checking for chosen layout... Apache

checking for working mkdir -p... yes

checking build system type... x86_64-unknown-linux-gnu

checking host system type... x86_64-unknown-linux-gnu

checking target system type... x86_64-unknown-linux-gnu

Configuring Apache Portable Runtime library ...

[...]

config.status: creating build/rules.mk

config.status: creating build/pkg/pkginfo

config.status: creating build/config_vars.sh

config.status: creating include/ap_config_auto.h

config.status: executing default commands

[tblgrant@silo httpd-2.2.2]$

[tblgrant@silo httpd-2.2.2]$ ls -ld configure

-rwx------ 1 tblgrant projects 639862 Apr 21 2006 configure

[tblgrant@silo httpd-2.2.2]$ ls -ld Mak*

-rw------- 1 tblgrant projects 8754 Jun 12 11:16 Makefile

-rw------- 1 tblgrant projects 8559 Nov 13 2005 Makefile.in

-rw------- 1 tblgrant projects 35526 Mar 22 2006 Makefile.win

[tblgrant@silo httpd-2.2.2]$ date

Tue Jun 12 11:20:06 EDT 2012

[tblgrant@silo httpd-2.2.2]$

I take a snapshot of the date so you can better identify what files had justbeen created. Next I run make followed by make install. They take a bit torun and have considerably large outputs:

[tblgrant@silo httpd-2.2.2]$ make

Making all in srclib

make[1]: Entering directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib’

Making all in pcre

make[2]: Entering directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib/pcre’

make[3]: Entering directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib/pcre’

[...]

make[1]: Leaving directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2’

[tblgrant@silo httpd-2.2.2]$ make install

Making install in srclib

make[1]: Entering directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib’

Making install in pcre

make[2]: Entering directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib/pcre’

Page 25: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

23

make[3]: Entering directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib/pcre’

make[3]: Leaving directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2/srclib/pcre’

[...]

mkdir /u/tblgrant/apache/man/man8

mkdir /u/tblgrant/apache/manual

make[1]: Leaving directory ‘/nfs/nfs1/home/tblgrant/httpd-2.2.2’

[tblgrant@silo httpd-2.2.2]$

Once the installation ends I move out of the folder (actually at the top ofmy file system with cd) and I check that the ~/apache folder has been created:

[tblgrant@silo httpd-2.2.2]$ pwd

/u/tblgrant/httpd-2.2.2

[tblgrant@silo httpd-2.2.2]$ cd

[tblgrant@silo httpd-2.2.2]$ pwd

/u/tblgrant

[tblgrant@silo httpd-2.2.2]$ ls -ld apache

drwxr-xr-x 14 tblgrant projects 4096 Jun 12 11:34 apache

[tblgrant@silo httpd-2.2.2]$

Let’s see what we got.

2.2 A tour of the Apache folders

A tour of the apache folder reveals the following important sub-folders:

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls apache

bin cgi-bin error icons logs manual

build conf htdocs include man modules

[tblgrant@silo ~]$

For now just the conf folder is relevant.

2.3 Configuring Apache

We move into the conf folder and look for a file named httpd.conf whichwe need to modify. Open the file with pico -w (which disables automaticwrapping) and make two changes:

• on line 40 modify Listen 80 to refer to your port.

• on line 64 in User daemon replace daemon with your username.

Then exit and save. I list the actual steps I took below. You will recognize manyof the commands and actions but there will also be some new. As an example

Page 26: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

24

consider the use of grep. It is a Unix utility that can search its input for aregular pattern. We look for the word Listen first: there are four lines the oneI changed. I then decided to look for User followed by a space. To indicate thespace on the command line I include the string to search for in double quotes.When the search is repeated for Listen followed by a space, the result of thesearch is a bit different, as you would expect.

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ cd apache

[tblgrant@silo apache]$ cd conf

[tblgrant@silo conf]$ ls -ld httpd.conf

-rw-r--r-- 1 tblgrant projects 13298 Jun 12 11:33 httpd.conf

[tblgrant@silo conf]$ pico -w httpd.conf

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls apache

bin cgi-bin error icons logs manual

build conf htdocs include man modules

[tblgrant@silo ~]$ cd apache

[tblgrant@silo apache]$ cd conf

[tblgrant@silo conf]$ ls -ld httpd.conf

-rw-r--r-- 1 tblgrant projects 13298 Jun 12 11:33 httpd.conf

[tblgrant@silo conf]$ pico -w httpd.conf

[tblgrant@silo conf]$ grep Listen httpd.conf

# Listen: Allows you to bind Apache to specific IP addresses and/or

# Change this to Listen on specific IP addresses as shown below to

#Listen 12.34.56.78:80

Listen 61200

[tblgrant@silo conf]$ grep "User " httpd.conf

User tblgrant

# User home directories

[tblgrant@silo conf]$ grep "Listen " httpd.conf

# Change this to Listen on specific IP addresses as shown below to

#Listen 12.34.56.78:80

Listen 61200

[tblgrant@silo conf]$

2.4 Starting and stopping Apache

There is a utility ( a script) called apachectl and located in ~/apache/bin thatcan be used to start or stop Apache.

[tblgrant@silo conf]$ pwd

/u/tblgrant/apache/conf

[tblgrant@silo conf]$ cd ../bin

[tblgrant@silo bin]$ ls

ab checkgid envvars-std htdigest httxt2dbm

apachectl dbmmanage htcacheclean htpasswd logresolve

Page 27: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

25

apxs envvars htdbm httpd rotatelogs

[tblgrant@silo bin]$ ps -ef | grep tblgrant

tblgrant 15718 31344 1 12:23 pts/51 00:00:00 ps -ef

tblgrant 15719 31344 0 12:23 pts/51 00:00:00 grep tblgrant

dgerman 31340 31272 0 10:41 pts/30 00:00:00 ssh silo -l tblgrant

root 31341 23297 0 10:41 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 31343 31341 0 10:41 ? 00:00:00 sshd: tblgrant@pts/51

tblgrant 31344 31343 0 10:41 pts/51 00:00:00 -bash

[tblgrant@silo bin]$ ps -ef | grep dgerman

dgerman 4685 1 0 May07 ? 00:00:47 /u/dgerman/apache/bin/httpd -k restart

tblgrant 15724 31344 0 12:23 pts/51 00:00:00 grep dgerman

dgerman 23280 4685 0 08:12 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

dgerman 23281 4685 0 08:12 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

dgerman 23282 4685 0 08:12 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

dgerman 23283 4685 0 08:12 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

dgerman 23284 4685 0 08:12 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

dgerman 23537 4685 0 08:18 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

dgerman 24506 4685 0 08:37 ? 00:00:00 /u/dgerman/apache/bin/httpd -k restart

root 31143 23297 0 10:39 ? 00:00:00 sshd: dgerman [priv]

dgerman 31163 31143 0 10:40 ? 00:00:00 sshd: dgerman@pts/23

dgerman 31164 31163 0 10:40 pts/23 00:00:00 -bash

root 31249 23297 0 10:40 ? 00:00:00 sshd: dgerman [priv]

dgerman 31271 31249 0 10:41 ? 00:00:00 sshd: dgerman@pts/30

dgerman 31272 31271 0 10:41 pts/30 00:00:00 -bash

dgerman 31340 31272 0 10:41 pts/30 00:00:00 ssh silo -l tblgrant

[tblgrant@silo bin]$

Notice the use of the ps utility: it is called with the -ef command line switches,and its output is piped (using the Unix pipe | symbol) into grep. We look forlines that have tblgrant (or in your case, your username) on them. There is noindication at this time that your server is running. By comparison it looks likedgerman has an Apache server up and running. Also it looks like7 he is loggedinto silo as tblgrant. Next we start the server and check to see the difference.

[tblgrant@silo bin]$ ./apachectl start

[tblgrant@silo bin]$ ps -ef | grep tblgrant

tblgrant 15733 1 0 12:23 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15734 15733 0 12:23 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15735 15733 0 12:23 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15736 15733 0 12:23 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15737 15733 0 12:23 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15738 15733 0 12:23 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15740 31344 2 12:23 pts/51 00:00:00 ps -ef

tblgrant 15741 31344 0 12:23 pts/51 00:00:00 grep tblgrant

dgerman 31340 31272 0 10:41 pts/30 00:00:00 ssh silo -l tblgrant

root 31341 23297 0 10:41 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 31343 31341 0 10:41 ? 00:00:00 sshd: tblgrant@pts/51

tblgrant 31344 31343 0 10:41 pts/51 00:00:00 -bash

7You can always find out what any user is running at any given time, this way

Page 28: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

26

We stop it, check, start it again. Notice that we can also check activity ona certain port with netstat too:

[tblgrant@silo bin]$ ~/apache/bin/apachectl stop

[tblgrant@silo bin]$ ps -ef | grep tblgrant

tblgrant 15751 31344 0 12:23 pts/51 00:00:00 ps -ef

tblgrant 15752 31344 0 12:23 pts/51 00:00:00 grep tblgrant

dgerman 31340 31272 0 10:41 pts/30 00:00:00 ssh silo -l tblgrant

root 31341 23297 0 10:41 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 31343 31341 0 10:41 ? 00:00:00 sshd: tblgrant@pts/51

tblgrant 31344 31343 0 10:41 pts/51 00:00:00 -bash

[tblgrant@silo bin]$ ~/apache/bin/apachectl start

[tblgrant@silo bin]$ netstat -a | grep 61200

tcp 0 0 *:61200 *:* LISTEN

[tblgrant@silo bin]$ ~/apache/bin/apachectl stop

[tblgrant@silo bin]$ netstat -a | grep 61200

[tblgrant@silo bin]$ ~/apache/bin/apachectl start

[tblgrant@silo bin]$

When the server is running a process is listed listening on that port. If theserver is stopped, no activity is detected on that port. Before you start yourinstallation of Apache this is also a good way to verify that your port numberhas not been taken already.

2.5 Accessing your Apache server with a browser

I point my browser to http://silo.cs.indiana.edu:61200/ and I see thatmy server is running. I stop it and I don’t get any reply. Do the same, justuse your own port number. Port numbers will be distributed in the first day ofclasses.

Page 29: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 3

HTML

You will go into the htdocs folder and change your page.

27

Page 30: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

28

Page 31: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 4

A Note on Open-Source

Open-Source software means two things:

1. free software, and

2. no customer support

Free software doesn’t mean low quality software. Lack of support is a resultof the uncoordinated, distributed development that is going on in the open-source community. There’s more to say here with lots of consequences directlyapplicable to us this semester so this discussion will reappear as a section inmany future chapters. Here I only discuss the installation of apache2.4 onsilo. Key points to remember: (a) developers want you to be able to use theirprogram(s): documentation is provided; (b) however for the most part you’reon your own as you might find their docs kinda brief (or pithy, however youprefer to think of them); and (c) integration depends crucially on the version ofthe open source software you use.

This will become clearer as we survey the various software products we willdiscuss in this class: Apache, MySQL, PHP, CakePHP, Python and Django.We will need to orchestrate carefully chosen versions of these programs so theywork together reliably (or, at all).

29

Page 32: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

30

Page 33: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 5

Python

This is a short introduction to Python. We’ll need to be careful with whatversion we have and remain alert in identifying what version we might need, asmay be the case. Regarding the summary, here’s a basic outline1, just as anexample, of how we might get started.

Python is already installed on silo and we can choose among several ver-sions.

1http://www.cs.indiana.edu/classes/a202/spr2010/quick start.pdf

31

Page 34: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

32

Page 35: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 6

CGI

Most of the reference materials that we need are here1.

1http://docs.python.org/library/cgi.html

33

Page 36: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

34

Page 37: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 7

HTTP

You’re going to learn that HTTP is really a very simple protocol.

35

Page 38: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

36

Page 39: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 8

MySQL

8.1 Downloading MySQL

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ cp /l/www/classes/a202/software/mysql-5.1.58.tar.gz .

[tblgrant@silo ~]$ ls -ld mysql-5.1.58.tar.gz

-rw------- 1 tblgrant projects 24333490 Jun 14 11:59 mysql-5.1.58.tar.gz

[tblgrant@silo ~]$

8.2 Uncompressing

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld mysql-5.1.58.tar.gz

-rw------- 1 tblgrant projects 24333490 Jun 14 11:59 mysql-5.1.58.tar.gz

[tblgrant@silo ~]$ gunzip mysql-5.1.58.tar.gz

[tblgrant@silo ~]$ ls -ld mysql-5.1.58*

-rw------- 1 tblgrant projects 185845760 Jun 14 11:59 mysql-5.1.58.tar

[tblgrant@silo ~]$

8.3 Unarchiving

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld mysql-5.1.58*

-rw------- 1 tblgrant projects 185845760 Jun 14 11:59 mysql-5.1.58.tar

[tblgrant@silo ~]$ tar xvf mysql-5.1.58.tar

mysql-5.1.58/

mysql-5.1.58/dbug/

mysql-5.1.58/dbug/dbug.c

mysql-5.1.58/dbug/example3.c

37

Page 40: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

38

mysql-5.1.58/dbug/example2.c

mysql-5.1.58/dbug/main.c

[...]

mysql-5.1.58/include/t_ctype.h

mysql-5.1.58/include/my_compiler.h

mysql-5.1.58/include/my_bit.h

mysql-5.1.58/include/my_sys.h

mysql-5.1.58/include/heap.h

[tblgrant@silo ~]$ ls -ld mysql-5.1.58*

drwx------ 32 tblgrant projects 4096 Jul 1 2011 mysql-5.1.58

-rw------- 1 tblgrant projects 185845760 Jun 14 11:59 mysql-5.1.58.tar

[tblgrant@silo ~]$

This takes a bit of time so just keep watching the screen as the informationscrolls up. When the process ends you’re ready to start the actual installation.

8.4 Installing MySQL

A folder mysql-5.1.58 has been created by tar xvf. We need to go inside itand set up an installation script; then run the script.

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld mysql-5.1.58*

drwx------ 32 tblgrant projects 4096 Jul 1 2011 mysql-5.1.58

-rw------- 1 tblgrant projects 185845760 Jun 14 11:59 mysql-5.1.58.tar

[tblgrant@silo ~]$ cd mysql-5.1.58

[tblgrant@silo mysql-5.1.58]$ pwd

/u/tblgrant/mysql-5.1.58

[tblgrant@silo mysql-5.1.58]$ ls

aclocal.m4 configure.in libmysql netware strings

BUILD COPYING libmysqld plugin support-files

ChangeLog dbug libmysql_r README tests

client depcomp ltmain.sh regex unittest

CMakeLists.txt Docs Makefile.am scripts vio

cmd-line-utils extra Makefile.in server-tools win

config include man sql ylwrap

config.guess install-sh missing sql-bench zlib

config.sub INSTALL-SOURCE mysql-test sql-common

configure INSTALL-WIN-SOURCE mysys storage

[tblgrant@silo mysql-5.1.58]$

There’s no file install so we choose that name for our script.

[tblgrant@silo mysql-5.1.58]$ pico -w install

[tblgrant@silo mysql-5.1.58]$ ls -ld install

-rw------- 1 tblgrant projects 57 Jun 14 12:42 install

[tblgrant@silo mysql-5.1.58]$ cat install

./configure --prefix=/u/tblgrant/mysql

Page 41: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

39

make

make install

[tblgrant@silo mysql-5.1.58]$ ls -ld configure

-rwx------ 1 tblgrant projects 1431114 Jul 1 2011 configure

[tblgrant@silo mysql-5.1.58]$ chmod 700 install

[tblgrant@silo mysql-5.1.58]$ ls -ld install

-rwx------ 1 tblgrant projects 57 Jun 14 12:42 install

[tblgrant@silo mysql-5.1.58]$

The script runs configure just like we did for Apache. However this is adifferent configure in a different folder and for a different server. If we take alook at configure we see that it is an executable file.

We make our script executable too, so we can run it:

[tblgrant@silo mysql-5.1.58]$ ./install

checking build system type... x86_64-unknown-linux-gnu

checking host system type... x86_64-unknown-linux-gnu

checking target system type... x86_64-unknown-linux-gnu

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

[...]

make[1]: Leaving directory ‘/nfs/nfs1/home/tblgrant/mysql-5.1.58/server-tools’

Making install in win

make[1]: Entering directory ‘/nfs/nfs1/home/tblgrant/mysql-5.1.58/win’

make[2]: Entering directory ‘/nfs/nfs1/home/tblgrant/mysql-5.1.58/win’

make[2]: Nothing to be done for ‘install-exec-am’.

make[2]: Nothing to be done for ‘install-data-am’.

make[2]: Leaving directory ‘/nfs/nfs1/home/tblgrant/mysql-5.1.58/win’

make[1]: Leaving directory ‘/nfs/nfs1/home/tblgrant/mysql-5.1.58/win’

[tblgrant@silo mysql-5.1.58]$

Expect this to take even longer than the previous step.

8.5 Configuring MySQL

This process has two steps: first we run mysql install db, then we need tospecify somewhere the server’s port, like we did for Apache. At this stage youneed to figure out what port you will be running the MySQL server on. It’s adifferent server than Apache so it will need its own port number. Here’s thesequence of actions that completes the first step of this stage:

[tblgrant@silo mysql-5.1.58]$ pwd

/u/tblgrant/mysql-5.1.58

[tblgrant@silo mysql-5.1.58]$ cd ../mysql

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

Page 42: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

40

[tblgrant@silo mysql]$ ls

bin ibdata1 include libexec mysqld.log mysql-test share

docs ib_logfile0 info man mysqld.pid onmymind sql-bench

friday ib_logfile1 lib mysql mysql.sock project test

[tblgrant@silo mysql]$ pico -w setup

[tblgrant@silo mysql]$ ls -ld setup

-rw------- 1 tblgrant projects 92 Jun 14 13:24 setup

[tblgrant@silo mysql]$ chmod +x setup

[tblgrant@silo mysql]$ ls -ld setup

-rwx------ 1 tblgrant projects 92 Jun 14 13:24 setup

[tblgrant@silo mysql]$ cat setup

/u/tblgrant/mysql/bin/mysql_install_db \

--user=tblgrant \

--datadir=/u/tblgrant/mysql

[tblgrant@silo mysql]$ ./setup

Installing MySQL system tables...

[...]

[tblgrant@silo mysql]$

We created a file (called setup) and placed the actual command (completewith command line arguments and all) in it. Made it executable. Run it.The output is not very large but it has been suppressed because it’s not wellformatted and is very wide. Ignore the warnings and move on.

The second step of the configuration is included in the next section wherewe show how to start the server: the port number can be specified at run time.

8.6 Starting MySQL

You will need a port number for this new server. You already have an identity(mine is tblgrant). You also have a port for Apache. My Apache port numberis 61200. Yours should be listed on the website along with all the other portsyou will need. My mysql port number will be 61230 and let’s decide that Ryan’swill be 61231 while Anna’s 61232.

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ls -ld start*

ls: cannot access start*: No such file or directory

[tblgrant@silo mysql]$ ls stop*

ls: cannot access stop*: No such file or directory

[tblgrant@silo mysql]$

We check to see if files named start and stop exist already or not. We alsocheck (using ps -ef) if the server is running already or not1.

1It’s not, because we didn’t start it yet.

Page 43: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

41

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16607 26661 0 13:50 pts/32 00:00:00 ps -ef

tblgrant 16608 26661 0 13:50 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:00 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ pico -w start

[tblgrant@silo mysql]$ ls -ld start

-rw------- 1 tblgrant projects 307 Jun 14 13:53 start

[tblgrant@silo mysql]$ cat start

/u/tblgrant/mysql/bin/mysqld_safe \

--user=tblgrant \

--pid-file=/u/tblgrant/mysql/mysqld.pid \

--log=/u/tblgrant/mysql/mysqld.log \

--socket=/u/tblgrant/mysql/mysql.sock \

--basedir=/u/tblgrant/mysql \

--log-error=/u/tblgrant/mysqld-error.log \

--datadir=/u/tblgrant/mysql \

--port=61230 &

[tblgrant@silo mysql]$ chmod u+x start

[tblgrant@silo mysql]$ ls -ld start

-rwx------ 1 tblgrant projects 307 Jun 14 13:53 start

[tblgrant@silo mysql]$

We create a file, name it start, make it executable and run it. Special caremust be had with the backslashes at the end of line: their purpose is to escapethe newline character, so they must be followed by the end of line so that theentire command looks like it has been typed on one and the same line. A singlespace placed after one of those would be enough to break this convention.

[tblgrant@silo mysql]$ ./start

[tblgrant@silo mysql]$ 120614 13:54:13 mysqld_safe Logging to ’/u/tblgrant/mysqld-error.log’.

120614 13:54:13 mysqld_safe Starting mysqld daemon with databases from /u/tblgrant/mysql

[tblgrant@silo mysql]$

Now the server is up and we can see that:

Page 44: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

42

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16820 1 0 13:54 pts/32 00:00:00 /bin/sh /u/tblgrant/mysql/bin/mysqld_[...]

tblgrant 16935 16820 9 13:54 pts/32 00:00:00 /u/tblgrant/mysql/libexec/mysqld --ba[...]

tblgrant 16941 26661 0 13:54 pts/32 00:00:00 ps -ef

tblgrant 16942 26661 0 13:54 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:01 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

[tblgrant@silo mysql]$

8.7 Checking MySQL

The command to see if something is running is: ps. You will see me using it inthe following section as follows:

ps -ef | grep tblgrant

Obviously, you would need to use your own username if you want to seewhat you’re running.

8.8 Stopping MySQL

Check whether the server is running or not, using ps -ef. Two long lines(clipped below) indicate that it is.

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16820 1 0 13:54 pts/32 00:00:00 /bin/sh /u/tblgrant/mysql/bin/mysqld_[...]

tblgrant 16935 16820 0 13:54 pts/32 00:00:00 /u/tblgrant/mysql/libexec/mysqld --ba[...]

tblgrant 18246 26661 0 14:26 pts/32 00:00:00 ps -ef

Page 45: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

43

tblgrant 18247 26661 0 14:26 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:01 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

[tblgrant@silo mysql]$ ls -ld start

-rwx------ 1 tblgrant projects 307 Jun 14 13:53 start

[tblgrant@silo mysql]$ ls -ld stop

ls: cannot access stop: No such file or directory

[tblgrant@silo mysql]$ pico -w stop

[tblgrant@silo mysql]$ cat stop

/u/tblgrant/mysql/bin/mysqladmin \

--socket=/u/tblgrant/mysql/mysql.sock \

--port=61230 -u root -p shutdown

[tblgrant@silo mysql]$ ls -ld stop

-rw------- 1 tblgrant projects 113 Jun 14 14:29 stop

We create a file (stop) that will hold the instructions to stop the server.We make it executable, then check one more time to see that the server is stillrunning (and it is) so we run stop, then check to see that the server is nolonger visible using ps -ef | grep tblgrant. Replace tblgrant with yourown username.

[tblgrant@silo mysql]$ ls -ld stop

-rw------- 1 tblgrant projects 113 Jun 14 14:29 stop

[tblgrant@silo mysql]$ chmod 700 stop

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16820 1 0 13:54 pts/32 00:00:00 /bin/sh /u/tblgrant/mysql/bin/mysqld_[...]

tblgrant 16935 16820 0 13:54 pts/32 00:00:00 /u/tblgrant/mysql/libexec/mysqld --ba[...]

tblgrant 18376 26661 1 14:29 pts/32 00:00:00 ps -ef

tblgrant 18377 26661 0 14:29 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:01 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

[tblgrant@silo mysql]$ ./stop

Enter password:

120614 14:29:56 mysqld_safe mysqld from pid file /u/tblgrant/mysql/mysqld.pid ended

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

Page 46: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

44

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 18386 26661 0 14:29 pts/32 00:00:00 ps -ef

tblgrant 18387 26661 0 14:29 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:01 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

[tblgrant@silo mysql]$

Hit the Enter key when prompted for a password since the password for theroot account is not set yet (thus, it’s empty). The next section shows how youcan change the root password to a password of your choice: you will create afile that contains the command, make it executable and then run it.

8.9 Connecting

8.9.1 Connecting as root

Let’s create a file with the command to connect to the MySQL server as root.

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ls -ld connect*

ls: cannot access connect*: No such file or directory

[tblgrant@silo mysql]$ pico -w connect_as_root

[tblgrant@silo mysql]$ ls -ld connect*

-rw------- 1 tblgrant projects 76 Jun 14 16:23 connect_as_root

[tblgrant@silo mysql]$ cat connect_as_root

mysql --socket=/u/tblgrant/mysql/mysql.sock \

--port=61230 -u root -p

[tblgrant@silo mysql]$ chmod +x connect_as_root

[tblgrant@silo mysql]$ ls -ld connect_as_root

-rwx------ 1 tblgrant projects 76 Jun 14 16:23 connect_as_root

[tblgrant@silo mysql]$

We should now be able to run this script from the command line and loginto the server. Here’s how I did it. Was I successful?

[tblgrant@silo mysql]$ ./connect_as_root

Enter password:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket [...]

[tblgrant@silo mysql]$

It looks like I can’t connect. Any idea what went wrong2?

2Is the server in fact running?

Page 47: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

45

Let’s be sure we have a server running:

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 24096 26661 4 16:23 pts/32 00:00:00 ps -ef

tblgrant 24097 26661 0 16:23 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:01 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

Turns out we don’t (but, of course!) so we’d better turn it on3:

[tblgrant@silo mysql]$ ./start

[tblgrant@silo mysql]$ 120614 16:23:55 mysqld_safe Logging to ’/u/tblgrant/mysqld-error.log’.

120614 16:23:55 mysqld_safe Starting mysqld daemon with databases from /u/tblgrant/mysql

[tblgrant@silo mysql]$ ps -ef | grep tblgrant

tblgrant 15796 1 0 Jun12 ? 00:00:02 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 24100 1 0 16:23 pts/32 00:00:00 /bin/sh /u/tblgrant/mysql/bin/mysqld_[...]

tblgrant 24211 24100 37 16:23 pts/32 00:00:00 /u/tblgrant/mysql/libexec/mysqld --ba[...]

tblgrant 24215 26661 0 16:23 pts/32 00:00:00 ps -ef

tblgrant 24216 26661 0 16:23 pts/32 00:00:00 grep tblgrant

dgerman 26657 26494 0 10:59 pts/30 00:00:01 ssh silo -l tblgrant

root 26658 23297 0 10:59 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 26660 26658 0 10:59 ? 00:00:01 sshd: tblgrant@pts/32

tblgrant 26661 26660 0 10:59 pts/32 00:00:00 -bash

[tblgrant@silo mysql]$ ./connect_as_root

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.58-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

3Don’t forget this, when debugging. Also, please remember to not start the server whenit’s already running.

Page 48: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

46

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ’help;’ or ’

h’ for help. Type ’\c’ to clear the current input statement.

mysql> exit

Bye

[tblgrant@silo mysql]$ ls

bin ibdata1 info mysql mysql-test share test

connect_as_root ib_logfile0 lib mysqld.log onmymind sql-bench

docs ib_logfile1 libexec mysqld.pid project start

friday include man mysql.sock setup stop

[tblgrant@silo mysql]$ cat connect_as_root

mysql --socket=/u/tblgrant/mysql/mysql.sock \

--port=61230 -u root -p

[tblgrant@silo mysql]$

As we can see, the mysql server starts, then connect as root is invoked andthe password requested in the ensuing dialog is in fact not needed (because it’sempty, so we hit the Enter key when prompted). We should set a real passwordfor the root.

8.9.2 Changing the root password

In order to do that we set up a file (setpass) with the command. Next, wemake it executable and run it.

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ls

bin ibdata1 info mysql mysql-test share test

connect_as_root ib_logfile0 lib mysqld.log onmymind sql-bench

docs ib_logfile1 libexec mysqld.pid project start

friday include man mysql.sock setup stop

[tblgrant@silo mysql]$ pico -w setpass

[tblgrant@silo mysql]$ ls -ld setpass

-rw------- 1 tblgrant projects 121 Jun 14 17:06 setpass

[tblgrant@silo mysql]$ chmod 700 setpass

[tblgrant@silo mysql]$ cat setpass

/u/tblgrant/mysql/bin/mysqladmin \

--port=61230 \

--socket=/u/tblgrant/mysql/mysql.sock \

-u root password ’sp00n’

[tblgrant@silo mysql]$

The root password will be sp00n in my case. I will keep this file in case Iforget the password. You should do the same to minimize unpleasant surprises.

Page 49: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

47

It’s better to do it this way, if we have that option, and we do.However, there is a way to recover (or rather reset) a lost root password and

I will show that later.I try to log in below with the empty password: it no longer works. Only

when I type the correct, new password am I allowed to get in. Once I get in Isimply exit to make sure I know how to do it.

[tblgrant@silo mysql]$ ls

bin ibdata1 info mysql mysql-test setup stop

connect_as_root ib_logfile0 lib mysqld.log onmymind share test

docs ib_logfile1 libexec mysqld.pid project sql-bench

friday include man mysql.sock setpass start

[tblgrant@silo mysql]$ ./setpass

[tblgrant@silo mysql]$ ./connect_as_root

Enter password:

ERROR 1045 (28000): Access denied for user ’root’@’localhost’ (using password: NO)

[tblgrant@silo mysql]$ ./setpass

/u/tblgrant/mysql/bin/mysqladmin: connect to server at ’localhost’ failed

error: ’Access denied for user ’root’@’localhost’ (using password: NO)’

[tblgrant@silo mysql]$ ./connect_as_root

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.1.58-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ’help;’ or ’\h’ for help. Type ’\c’ to clear the current input statement.

mysql> exit

Bye

[tblgrant@silo mysql]$

8.9.3 Creating a user account

Only the superuser (root) can create a new user account (or those that havebeen authorized to do so). The following snapshot shows how we connect as root,create a new database (called thursday, any other name would have worked justas well), a new user (username: fabregas, password: c3sc) and grant the newuser all rights on this newly created database. I log in with the new password.

[tblgrant@silo mysql]$ ./connect_as_root

Enter password:

Page 50: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

48

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 5.1.58-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ’help;’ or ’\h’ for help. Type ’\c’ to clear the current input statement.

mysql> show databases like ’thursday’;

Empty set (0.01 sec)

mysql> create database thursday;

Query OK, 1 row affected (0.06 sec)

mysql> create user ’fabregas’@’silo.cs.indiana.edu’ identified by ’c3sc’;

Query OK, 0 rows affected (0.00 sec)

mysql> grant all on thursday.* to ’fabregas’@’silo.cs.indiana.edu’;

Query OK, 0 rows affected (0.00 sec)

mysql> show databases like ’thursday’;

+---------------------+

| Database (thursday) |

+---------------------+

| thursday |

+---------------------+

1 row in set (0.01 sec)

mysql> select * from mysql.user;

+-----------------------+----------+-------------------------------------------+----

| Host | User | Password | Sel

+-----------------------+----------+-------------------------------------------+----

| localhost | root | *42BD54D7AE52C67E243AE04CBB8ECC68369C8FAD | Y

| silo.cs.indiana.edu | root | | Y

| silo.cs.indiana.edu | | | N

| localhost | | | N

| silo.cs.indiana.edu | fabregas | *F668E44781BF3BA35CCA17C1A68565428CE0A574 | N

+-----------------------+----------+-------------------------------------------+----

7 rows in set (0.00 sec)

Note that the software comes with more than one account without passwordso in a production environment those holes should definitely be plugged in. Weare writing SQL and we can restrict the output by selection: only those recordsthat satisfy a certain criteria will be shown.

At the same time, by projection, the number of columns shown also can berestricted. Additionally we can list all the rights given to a specific user.

Page 51: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

49

mysql> select Host, User, Password from mysql.user where User = ’fabregas’;;

+---------------------+----------+-------------------------------------------+

| Host | User | Password |

+---------------------+----------+-------------------------------------------+

| silo.cs.indiana.edu | fabregas | *F668E44781BF3BA35CCA17C1A68565428CE0A574 |

+---------------------+----------+-------------------------------------------+

1 row in set (0.00 sec)

mysql> show grants for ’fabregas’@’silo.cs.indiana.edu’;

+------------------------------------------------------------------------------------

| Grants for [email protected]

+------------------------------------------------------------------------------------

| GRANT USAGE ON *.* TO ’fabregas’@’silo.cs.indiana.edu’ IDENTIFIED BY PASSWORD ’*F66

| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREAT

+------------------------------------------------------------------------------------

2 rows in set (0.00 sec)

mysql> exit

Bye

[tblgrant@silo mysql]$

8.9.4 Creating a database

The command (issued above with the right authority) is:

create database thursday

The first two words are keywords, and part of the syntax hence, non-negotiable.The third one is the name you chose for the database you want to create. Theonly restriction there is for the name to not have been used yet.

8.9.5 Creating a user

Here’s the command I used:

create user ’fabregas’@’silo.cs.indiana.edu’ identified by ’c3sc’;

The general syntax of these commands4 can be very complex so we think it’sbetter to learn from simple examples such as the one above. We create a userthat has a username and a host of origin. The user identifies herself/himselfwith a password. Literals are written inside single quotes.

8.9.6 Granting access rights

The following command grants all access rights to a certain user with a certainhost of origin on all the components of a certain database.

grant all on thursday.* to ’fabregas’@’silo.cs.indiana.edu’;

4http://dev.mysql.com/doc/refman/5.1/en/create-user.html

Page 52: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

50

8.9.7 Connecting as a regular user

Now that we have a user with a password and a database that the user isauthorized to access we should connect to the database as the regular user whohas access to it and see what we can do with it:

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ls -ld conn*

-rwx------ 1 tblgrant projects 76 Jun 14 16:23 connect_as_root

[tblgrant@silo mysql]$ pico -w connect_as_fabregas

[tblgrant@silo mysql]$ ls -ld connect_as_fabregas

-rw------- 1 tblgrant projects 107 Jun 14 21:06 connect_as_fabregas

[tblgrant@silo mysql]$ chmod 700 connect_as_fabregas

l[tblgrant@silo mysql]$ ls -ld connect_as_fabregas

-rwx------ 1 tblgrant projects 107 Jun 14 21:06 connect_as_fabregas

[tblgrant@silo mysql]$ ./connect_as_fabregas

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 10

Server version: 5.1.58-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ’help;’ or ’\h’ for help. Type ’\c’ to clear the current input statement.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| test |

| thursday |

+--------------------+

3 rows in set (0.00 sec)

mysql> use thursday;

Database changed

mysql> show tables;

Empty set (0.00 sec)

mysql> exit

Bye

[tblgrant@silo mysql]$

Page 53: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 9

Database Design

9.1 Entity-relationship model

A database is a collection1 of tables. Each table represents either a entity ora relationship. We will discuss normal forms and describe how the design of adatabase proceeds from a specification given by a user.

9.2 A case study

In the example2 discussed we have a number of tables and some initial data.Here’s how I create one of those tables, from logging in, to selecting the databaseand running the appropriate SQL query to have the table created.

[tblgrant@silo mysql]$ pwd

/u/tblgrant/mysql

[tblgrant@silo mysql]$ ./connect_as_fabregas

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 11

Server version: 5.1.58-log Source distribution

[...]

mysql> use thursday;

Database changed

mysql> create table enrollment (

-> customer varchar(25),

-> matchid varchar(30) ,

-> tickets decimal(4,0) ,

-> primary key (customer, matchid)

-> );

Query OK, 0 rows affected (0.12 sec)

1http://www.cs.indiana.edu/classes/a114-dger/fall2003/three/erdiagram.jpg2http://www.cs.indiana.edu/ dgerman/homeworkThree.html

51

Page 54: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

52

mysql> show tables;

+--------------------+

| Tables_in_thursday |

+--------------------+

| enrollment |

+--------------------+

1 row in set (0.00 sec)

mysql> describe enrollment;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| customer | varchar(25) | NO | PRI | | |

| matchid | varchar(30) | NO | PRI | | |

| tickets | decimal(4,0) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> select * from enrollment;

Empty set (0.00 sec)

mysql>

The fragment above shows the following actions in order:

1. connecting to the server

2. selecting the database

3. creating a table (empty currently)

4. showing all tables (one, for now)

5. selecting all info from the one created (empty set)

9.2.1 Tables

Here’s the SQL for the creation3 of the other tables:

Matches

create table matches (

matchnum int primary key,

matchid varchar(30),

round varchar(30),

venue varchar(30),

ticketPrice double

);

3http://silo.cs.indiana.edu:8346/sum2011/a202/lec0707.phps

Page 55: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

53

Participation

create table participation (

username varchar(8) ,

matchid varchar(30),

points double ,

primary key (username, matchid)

);

Players

create table players (

username varchar(8) primary key,

firstName varchar(20),

lastName varchar(20),

picture varchar(120),

address varchar(120),

city varchar(30),

state varchar(2),

playerRatePerMatch

decimal(12, 2)

);

Spectators

create table spectators (

customer varchar(30) primary key ,

logourl varchar(120),

address varchar(120),

city varchar(30) ,

state varchar(2)

);

Venues

create table venues

(

name varchar(30) primary key,

address varchar(120),

city varchar(30) ,

state varchar(2) ,

rent double ,

capacity int

);

These are all the tables we need, all the tables shown in the E-R diagram4:players stores the info about each player, spectators stores the info abouteach potential ticket-buyer; matches is the equivalent of the TV Guide that

4http://www.cs.indiana.edu/classes/a114-dger/fall2003/three/erdiagram.jpg

Page 56: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

54

you can buy from Kroger, or the Schedule of Classes you can buy from the IUBookstore (true, these days the same can be picked up from a web address); thevenues tables also describes an entity (it’s the equivalent of the list of classroomson a campus); and the other two tables (participation and enrollment) arerelationship tables. We type these definitions in then we check they’re there:

mysql> show tables;

+--------------------+

| Tables_in_thursday |

+--------------------+

| enrollment |

| matches |

| participation |

| players |

| spectators |

| venues |

+--------------------+

6 rows in set (0.00 sec)

We can also check that they have been created the way we wanted. We take alook at the columns in each table, their types and attributes (e.g., whether theyare (part of) a primary key, etc.):

mysql> describe players;

+--------------------+---------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------------------+---------------+------+-----+---------+-------+

| username | varchar(8) | NO | PRI | NULL | |

| firstName | varchar(20) | YES | | NULL | |

| lastName | varchar(20) | YES | | NULL | |

| picture | varchar(120) | YES | | NULL | |

| address | varchar(120) | YES | | NULL | |

| city | varchar(30) | YES | | NULL | |

| state | varchar(2) | YES | | NULL | |

| playerRatePerMatch | decimal(12,2) | YES | | NULL | |

+--------------------+---------------+------+-----+---------+-------+

8 rows in set (0.00 sec)

mysql> describe spectators;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| customer | varchar(30) | NO | PRI | NULL | |

| logourl | varchar(120) | YES | | NULL | |

| address | varchar(120) | YES | | NULL | |

| city | varchar(30) | YES | | NULL | |

| state | varchar(2) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

Page 57: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

55

mysql> describe matches;

+-------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+

| matchnum | int(11) | NO | PRI | NULL | |

| matchid | varchar(30) | YES | | NULL | |

| round | varchar(30) | YES | | NULL | |

| venue | varchar(30) | YES | | NULL | |

| ticketPrice | double | YES | | NULL | |

+-------------+-------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

mysql> describe venues;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| name | varchar(30) | NO | PRI | NULL | |

| address | varchar(120) | YES | | NULL | |

| city | varchar(30) | YES | | NULL | |

| state | varchar(2) | YES | | NULL | |

| rent | double | YES | | NULL | |

| capacity | int(11) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

mysql> describe participation;

+----------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| username | varchar(8) | NO | PRI | | |

| matchid | varchar(30) | NO | PRI | | |

| points | double | YES | | NULL | |

+----------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> describe enrollment;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| customer | varchar(25) | NO | PRI | | |

| matchid | varchar(30) | NO | PRI | | |

| tickets | decimal(4,0) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

Including the description of table enrollment is a bit redundant, but at leastthis is all consistent. The design (or structure) of our database is complete; timeto populate the database (that is, its tables) with some data.

Page 58: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

56

9.2.2 Data

The data we put into these tables comes from the user specifications5.

Enrollment

We are writing INSERT queries6. More varied SQL will be shown shortly.

insert into enrollment values

(’Illinois’ , ’dumi’ , 40),

(’Illinois’ , ’joba’ , 120),

(’Illinois’ , ’kuba’ , 180),

(’Illinois’ , ’mibi’ , 140),

(’Iowa’ , ’bijo’ , 200),

(’Iowa’ , ’dubi’ , 120),

(’Iowa’ , ’kubi’ , 180),

(’Indiana’ , ’bijo’ , 150),

(’Indiana’ , ’joba’ , 100),

(’Indiana’ , ’joku’ , 60),

(’Indiana’ , ’mijo’ , 210),

(’Michigan’ , ’duba’ , 300),

(’Michigan’ , ’joku’ , 240),

(’Michigan’ , ’kudu’ , 220),

(’Purdue’ , ’dubi’ , 80),

(’Purdue’ , ’miba’ , 200),

(’Purdue’ , ’mibi’ , 60),

(’Purdue’ , ’mijo’ , 120),

(’Minnesota’, ’duba’ , 80),

(’Minnesota’, ’dumi’ , 120),

(’Minnesota’, ’joku’ , 60),

(’Minnesota’, ’kudu’ , 20);

Participation

insert into participation values (’cbarkley’, ’duba’ , 0.5 ),

(’cbarkley’, ’joba’ , 0 ), (’cbarkley’, ’kuba’ , 0 ),

(’cbarkley’, ’miba’ , 0.5 ), (’lbird’ , ’bijo’ , 1 ),

(’lbird’ , ’dubi’ , 1 ), (’lbird’ , ’kubi’ , 1 ),

(’lbird’ , ’mibi’ , 1 ), (’mjordan’ , ’bijo’ , 0 ),

(’mjordan’ , ’joba’ , 1 ), (’mjordan’ , ’joku’ , 1 ),

(’mjordan’ , ’mijo’ , 0.5 ), (’rmiller’ , ’dumi’ , 0.5 ),

(’rmiller’ , ’miba’ , 0.5 ), (’rmiller’ , ’mibi’ , 0 ),

(’rmiller’ , ’mijo’ , 0.5 ), (’tduncan’ , ’duba’ , 0.5 ),

(’tduncan’ , ’dubi’ , 0 ), (’tduncan’ , ’dumi’ , 0.5 ),

(’tduncan’ , ’kudu’ , 0.5 ), (’tkukoc’ , ’joku’ , 0 ),

(’tkukoc’ , ’kuba’ , 1 ), (’tkukoc’ , ’kubi’ , 0 ), (’tkukoc’ , ’kudu’ , 0.5 );

5Pages 41-42 of http://www.cs.indiana.edu/ dgerman/eowp06.pdf6http://dev.mysql.com/doc/refman/5.5/en/insert.html

Page 59: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

57

Spectators

insert into spectators values

(’Illinois’ , ’’ , ’’ , ’’ , ’IL’),

(’Iowa’ , ’’ , ’’ , ’’ , ’IO’),

(’Indiana’ , ’’ , ’’ , ’’ , ’IN’),

(’Michigan’ , ’’ , ’’ , ’’ , ’MI’),

(’Purdue’ , ’’ , ’’ , ’’ , ’IN’),

(’UCLA’ , ’’ , ’’ , ’’ , ’CA’),

(’Minnesota’ , ’’ , ’’ , ’’ , ’MN’);

Venues

insert into venues values

( ’Madison Square Garden’, ’’ , ’New York’ , ’NY’, 1200.00, 4000),

( ’Rawles Arena’ , ’Math Magic Alley’ , ’Las Vegas’ , ’NV’, 740.00, 3400),

( ’RCA Dome’ , ’Hoosier Center Avenue’ , ’Indianapolis’ , ’IN’, 230.00, 1800),

( ’Soldier Field’ , ’One Soldier Field Avenue’ , ’Chicago’ , ’IL’, 800.00, 2800),

( ’Staples Center’, ’One Big Office Supplies Ave.’ , ’Los Angeles’ , ’LA’, 500.00, 3200),

( ’Woodburn Hall’ , ’Woodburn Avenue’ , ’Atlanta’ , ’GA’, 600.00, 2000),

( ’Wrigley Field’ , ’Wrigley Shore Drive’ , ’Chicago’ , ’IL’, 800.00, 1200);

Matches

insert into matches values

( 1 , ’joba’ , ’round one’ , ’Soldier Field ’ , 5.50),

( 2 , ’mibi’ , ’round one’ , ’Madison Square Garden’ , 8.00),

( 3 , ’kudu’ , ’round one’ , ’Wrigley Field ’ , 4.25),

( 4 , ’duba’ , ’round two’ , ’Woodburn Hall ’ , 6.50),

( 5 , ’kubi’ , ’round two’ , ’Staples Center ’ , 5.50),

( 6 , ’mijo’ , ’round two’ , ’RCA Dome ’ , 5.75),

( 7 , ’bijo’ , ’round three’ , ’Rawles Arena ’ , 7.35),

( 8 , ’kuba’ , ’round three’ , ’Soldier Field ’ , 8.00),

( 9 , ’dumi’ , ’round three’ , ’Madison Square Garden’ , 9.99),

( 10 , ’miba’ , ’round four’ , ’Wrigley Field ’ , 9.99),

( 11 , ’dubi’ , ’round four’ , ’Woodburn Hall ’ , 8.50),

( 12 , ’joku’ , ’round four’ , ’Staples Center ’ , 12.00);

Players

insert into players values

(’cbarkley’, ’Charles’, ’Barkley’, "", "" , ’Phoenix’ , ’AZ’, 450.00),

(’lbird’ , ’Larry’ , ’Bird’ , "", "" , ’Naples’ , ’FL’, 350.00),

(’mjordan’ , ’Michael’, ’Jordan’ , "", "" , ’Chicago’ , ’IL’, 500.00),

(’rmiller’ , ’Reggie’ , ’Miller’ , "", "" , ’Indianapolis’, ’IN’, 150.00),

(’tduncan’ , ’Tim’ , ’Duncan’ , "", "" , ’Houston’ , ’TX’, 210.00),

(’tkukoc’ , ’Toni’ , ’Kukoc’ , "", "Petar Zrinski", ’New Zagreb’ , ’PA’, 50.00);

Here is7 how we did this last year in class.

7http://silo.cs.indiana.edu:8346/sum2011/a202/lec0707.phps

Page 60: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

58

9.2.3 Queries

Page 43 of this8 document has a set of sample queries. This9 document describeshow these queries were in fact developed. Below, we will just give a hint of that.

1. List all the matches with number of tickets sold (per match).

We start by identifying our data sources.

mysql> describe enrollment;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| customer | varchar(25) | NO | PRI | | |

| matchid | varchar(30) | NO | PRI | | |

| tickets | decimal(4,0) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> select * from enrollment;

+-----------+---------+---------+

| customer | matchid | tickets |

+-----------+---------+---------+

| Illinois | dumi | 40 |

| Illinois | joba | 120 |

| Illinois | kuba | 180 |

| Illinois | mibi | 140 |

| Iowa | bijo | 200 |

| Iowa | dubi | 120 |

| Iowa | kubi | 180 |

| Indiana | bijo | 150 |

| Indiana | joba | 100 |

| Indiana | joku | 60 |

| Indiana | mijo | 210 |

| Michigan | duba | 300 |

| Michigan | joku | 240 |

| Michigan | kudu | 220 |

| Purdue | dubi | 80 |

| Purdue | miba | 200 |

| Purdue | mibi | 60 |

| Purdue | mijo | 120 |

| Minnesota | duba | 80 |

| Minnesota | dumi | 120 |

| Minnesota | joku | 60 |

| Minnesota | kudu | 20 |

+-----------+---------+---------+

22 rows in set (0.00 sec)

8http://www.cs.indiana.edu/ dgerman/eowp06.pdf9http://www.cs.indiana.edu/ dgerman/homeworkThree.html

Page 61: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

59

Note that SQL queries can contain aggregate functions10 and sorting11.

mysql> select matchid, sum(tickets) from enrollment group by matchid;

+---------+--------------+

| matchid | sum(tickets) |

+---------+--------------+

| bijo | 350 |

| duba | 380 |

| dubi | 200 |

| dumi | 160 |

| joba | 220 |

| joku | 360 |

| kuba | 180 |

| kubi | 180 |

| kudu | 240 |

| miba | 200 |

| mibi | 200 |

| mijo | 330 |

+---------+--------------+

12 rows in set (0.00 sec)

mysql> select matchid,

sum(tickets) as total

from enrollment

group by matchid

order by total desc;

+---------+-------+

| matchid | total |

+---------+-------+

| duba | 380 |

| joku | 360 |

| bijo | 350 |

| mijo | 330 |

| kudu | 240 |

| joba | 220 |

| mibi | 200 |

| dubi | 200 |

| miba | 200 |

| kubi | 180 |

| kuba | 180 |

| dumi | 160 |

+---------+-------+

12 rows in set (0.00 sec)

mysql>

This essentially concludes the query.

10http://www.w3schools.com/sql/sql groupby.asp11http://www.w3schools.com/sql/sql orderby.asp

Page 62: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

60

2. List all the matches, number of tickets sold, and the capacity of the venuefor each game. As before we start by identifying the data sources.

mysql> describe matches;

+-------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+

| matchnum | int(11) | NO | PRI | | |

| matchid | varchar(30) | YES | | NULL | |

| round | varchar(30) | YES | | NULL | |

| venue | varchar(30) | YES | | NULL | |

| ticketPrice | double | YES | | NULL | |

+-------------+-------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

mysql> describe venues;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| name | varchar(30) | NO | PRI | | |

| address | varchar(120) | YES | | NULL | |

| city | varchar(30) | YES | | NULL | |

| state | varchar(2) | YES | | NULL | |

| rent | double | YES | | NULL | |

| capacity | int(11) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

mysql> select matchid from matches;

+---------+

| matchid |

+---------+

| joba |

| mibi |

| kudu |

| duba |

| kubi |

| mijo |

| bijo |

| kuba |

| dumi |

| miba |

| dubi |

| joku |

+---------+

12 rows in set (0.00 sec)

So we can easily list all the matches. The rest of the information is in theother tables so we must start joining them:

Page 63: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

61

mysql> select matchid, capacity from matches, venues where venue = name;

+---------+----------+

| matchid | capacity |

+---------+----------+

| joba | 2800 |

| mibi | 4000 |

| kudu | 1200 |

| duba | 2000 |

| kubi | 3200 |

| mijo | 1800 |

| bijo | 3400 |

| kuba | 2800 |

| dumi | 4000 |

| miba | 1200 |

| dubi | 2000 |

| joku | 3200 |

+---------+----------+

12 rows in set (0.00 sec)

A join operation is simply a consistent subset of a cartesian product12.

mysql> select matchid, venue, capacity

from matches, venues

where venue = name;

+---------+-----------------------+----------+

| matchid | venue | capacity |

+---------+-----------------------+----------+

| joba | Soldier Field | 2800 |

| mibi | Madison Square Garden | 4000 |

| kudu | Wrigley Field | 1200 |

| duba | Woodburn Hall | 2000 |

| kubi | Staples Center | 3200 |

| mijo | RCA Dome | 1800 |

| bijo | Rawles Arena | 3400 |

| kuba | Soldier Field | 2800 |

| dumi | Madison Square Garden | 4000 |

| miba | Wrigley Field | 1200 |

| dubi | Woodburn Hall | 2000 |

| joku | Staples Center | 3200 |

+---------+-----------------------+----------+

12 rows in set (0.00 sec)

The first step performed the basic join, the next one enlarges the pro-jection by adding extra attributes (columns) to the output. We can nowadd sorting to our output: first we rely on the default ordering, later weexplicitly ask for the output to be listed in descending order of the venue,by capacity of the venue.

12Basically we chain the records that refer to the same thing.

Page 64: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

62

mysql> select matchid, venue, capacity

from matches, venues

where venue = name

order by capacity;

+---------+-----------------------+----------+

| matchid | venue | capacity |

+---------+-----------------------+----------+

| miba | Wrigley Field | 1200 |

| kudu | Wrigley Field | 1200 |

| mijo | RCA Dome | 1800 |

| dubi | Woodburn Hall | 2000 |

| duba | Woodburn Hall | 2000 |

| joba | Soldier Field | 2800 |

| kuba | Soldier Field | 2800 |

| joku | Staples Center | 3200 |

| kubi | Staples Center | 3200 |

| bijo | Rawles Arena | 3400 |

| dumi | Madison Square Garden | 4000 |

| mibi | Madison Square Garden | 4000 |

+---------+-----------------------+----------+

12 rows in set (0.00 sec)

mysql> select matchid, venue, capacity

from matches, venues

where venue = name order by capacity desc;

+---------+-----------------------+----------+

| matchid | venue | capacity |

+---------+-----------------------+----------+

| dumi | Madison Square Garden | 4000 |

| mibi | Madison Square Garden | 4000 |

| bijo | Rawles Arena | 3400 |

| joku | Staples Center | 3200 |

| kubi | Staples Center | 3200 |

| joba | Soldier Field | 2800 |

| kuba | Soldier Field | 2800 |

| dubi | Woodburn Hall | 2000 |

| duba | Woodburn Hall | 2000 |

| mijo | RCA Dome | 1800 |

| miba | Wrigley Field | 1200 |

| kudu | Wrigley Field | 1200 |

+---------+-----------------------+----------+

12 rows in set (0.00 sec)

Two or more tables can be joined. Here we add a third table to our join:

mysql> select matches.matchid, venue, tickets, capacity

from matches, venues, enrollment

where venue = name

and enrollment.matchid = matches.matchid

order by capacity desc;

Page 65: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

63

+---------+-----------------------+---------+----------+

| matchid | venue | tickets | capacity |

+---------+-----------------------+---------+----------+

| mibi | Madison Square Garden | 140 | 4000 |

| dumi | Madison Square Garden | 40 | 4000 |

| dumi | Madison Square Garden | 120 | 4000 |

| mibi | Madison Square Garden | 60 | 4000 |

| bijo | Rawles Arena | 150 | 3400 |

| bijo | Rawles Arena | 200 | 3400 |

| joku | Staples Center | 240 | 3200 |

| joku | Staples Center | 60 | 3200 |

| joku | Staples Center | 60 | 3200 |

| kubi | Staples Center | 180 | 3200 |

| joba | Soldier Field | 100 | 2800 |

| joba | Soldier Field | 120 | 2800 |

| kuba | Soldier Field | 180 | 2800 |

| dubi | Woodburn Hall | 80 | 2000 |

| duba | Woodburn Hall | 80 | 2000 |

| duba | Woodburn Hall | 300 | 2000 |

| dubi | Woodburn Hall | 120 | 2000 |

| mijo | RCA Dome | 210 | 1800 |

| mijo | RCA Dome | 120 | 1800 |

| kudu | Wrigley Field | 20 | 1200 |

| miba | Wrigley Field | 200 | 1200 |

| kudu | Wrigley Field | 220 | 1200 |

+---------+-----------------------+---------+----------+

22 rows in set (0.00 sec)

Sorting can also be done via a derived (not necessarily visible) field:

mysql> select matches.matchid, venue, tickets, capacity

from matches, venues, enrollment

where venue = name

and enrollment.matchid = matches.matchid

order by tickets/capacity desc;

+---------+-----------------------+---------+----------+

| matchid | venue | tickets | capacity |

+---------+-----------------------+---------+----------+

| kudu | Wrigley Field | 220 | 1200 |

| miba | Wrigley Field | 200 | 1200 |

| duba | Woodburn Hall | 300 | 2000 |

| mijo | RCA Dome | 210 | 1800 |

| joku | Staples Center | 240 | 3200 |

| mijo | RCA Dome | 120 | 1800 |

| kuba | Soldier Field | 180 | 2800 |

| dubi | Woodburn Hall | 120 | 2000 |

| bijo | Rawles Arena | 200 | 3400 |

| kubi | Staples Center | 180 | 3200 |

| bijo | Rawles Arena | 150 | 3400 |

| joba | Soldier Field | 120 | 2800 |

Page 66: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

64

| dubi | Woodburn Hall | 80 | 2000 |

| duba | Woodburn Hall | 80 | 2000 |

| joba | Soldier Field | 100 | 2800 |

| mibi | Madison Square Garden | 140 | 4000 |

| dumi | Madison Square Garden | 120 | 4000 |

| joku | Staples Center | 60 | 3200 |

| joku | Staples Center | 60 | 3200 |

| kudu | Wrigley Field | 20 | 1200 |

| mibi | Madison Square Garden | 60 | 4000 |

| dumi | Madison Square Garden | 40 | 4000 |

+---------+-----------------------+---------+----------+

22 rows in set (0.00 sec)

Derived fields can be named too (and shown):

mysql> select matches.matchid,

venue,

tickets,

capacity,

tickets / capacity as percent

from matches, venues, enrollment

where venue = name

and enrollment.matchid = matches.matchid

order by percent desc;

+---------+-----------------------+---------+----------+---------+

| matchid | venue | tickets | capacity | percent |

+---------+-----------------------+---------+----------+---------+

| kudu | Wrigley Field | 220 | 1200 | 0.1833 |

| miba | Wrigley Field | 200 | 1200 | 0.1667 |

| duba | Woodburn Hall | 300 | 2000 | 0.1500 |

| mijo | RCA Dome | 210 | 1800 | 0.1167 |

| joku | Staples Center | 240 | 3200 | 0.0750 |

| mijo | RCA Dome | 120 | 1800 | 0.0667 |

| kuba | Soldier Field | 180 | 2800 | 0.0643 |

| dubi | Woodburn Hall | 120 | 2000 | 0.0600 |

| bijo | Rawles Arena | 200 | 3400 | 0.0588 |

| kubi | Staples Center | 180 | 3200 | 0.0563 |

| bijo | Rawles Arena | 150 | 3400 | 0.0441 |

| joba | Soldier Field | 120 | 2800 | 0.0429 |

| dubi | Woodburn Hall | 80 | 2000 | 0.0400 |

| duba | Woodburn Hall | 80 | 2000 | 0.0400 |

| joba | Soldier Field | 100 | 2800 | 0.0357 |

| mibi | Madison Square Garden | 140 | 4000 | 0.0350 |

| dumi | Madison Square Garden | 120 | 4000 | 0.0300 |

| joku | Staples Center | 60 | 3200 | 0.0188 |

| joku | Staples Center | 60 | 3200 | 0.0188 |

| kudu | Wrigley Field | 20 | 1200 | 0.0167 |

| mibi | Madison Square Garden | 60 | 4000 | 0.0150 |

| dumi | Madison Square Garden | 40 | 4000 | 0.0100 |

+---------+-----------------------+---------+----------+---------+

Page 67: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

65

22 rows in set (0.00 sec)

Notice that the dot notation helps with disambiguation when a columnhas the same name in more than one table.

3. List all the matches and the percentage of seats they sold. What’s thematch that had the least percent of seats sold? We almost did this whenwe discussed the previous query. Notice that we can rely on nested queries.

mysql> select matches.matchid, venue, tickets, capacity, tickets / capacity as percent

-> from matches, venues, enrollment

-> where venue = name and

-> enrollment.matchid = matches.matchid and

-> tickets / capacity = (select min(tickets/capacity)

-> from enrollment, matches, venues

-> where venue = name

-> and enrollment.matchid = matches.matchid);

+---------+-----------------------+---------+----------+---------+

| matchid | venue | tickets | capacity | percent |

+---------+-----------------------+---------+----------+---------+

| dumi | Madison Square Garden | 40 | 4000 | 0.0100 |

+---------+-----------------------+---------+----------+---------+

1 row in set (0.00 sec)

mysql>

We’ll discuss many more examples in class.

Page 68: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

66

Page 69: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 10

PHP

We download and install1 PHP. You will then be able to program your webserver’s functionality using PHP, much like we did using CGI/Python.

The version of PHP we will use is 5.1.4 and it can be downloaded on-line.For your convenience we have a copy of the compressed archive for this versionof PHP accessible from the class folder2.

10.1 Downloading PHP

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld php*

ls: cannot access php*: No such file or directory

[tblgrant@silo ~]$ ls /l/www/classes/a202/software/php-5.1.4.tar.gz

/l/www/classes/a202/software/php-5.1.4.tar.gz

[tblgrant@silo ~]$ cp /l/www/classes/a202/software/php-5.1.4.tar.gz .

[tblgrant@silo ~]$ gunzip php-5.1.4.tar.gz

[tblgrant@silo ~]$ ls -ld php-5.1.4.tar

-rw------- 1 tblgrant projects 45649920 Jun 15 22:01 php-5.1.4.tar

[tblgrant@silo ~]$

10.2 Uncompressing PHP

Already achieved in the snapshot above, via gunzip.

10.3 Unarchiving PHP

We unpack the archive with tar xvf. A new folder will get created. Once thathappens we can safely remove the archive, because it just takes extra space, and

1http://silo.cs.indiana.edu:8346/2011/a202/0922.phps2/l/www/classes/a202/software/php-5.1.4.tar.gz

67

Page 70: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

68

we move inside the new folder.

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld php-5.1.4.tar

-rw------- 1 tblgrant projects 45649920 Jun 15 22:01 php-5.1.4.tar

[tblgrant@silo ~]$ tar xvf php-5.1.4.tar

php-5.1.4/

php-5.1.4/ext/

php-5.1.4/ext/gd/

php-5.1.4/ext/gd/gd.c

[...]

php-5.1.4/README.UNIX-BUILD-SYSTEM

php-5.1.4/buildconf.bat

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ ls -ld php*

drwx------ 14 tblgrant projects 4096 May 12 2006 php-5.1.4

-rw------- 1 tblgrant projects 45649920 Jun 15 22:01 php-5.1.4.tar

[tblgrant@silo ~]$ rm php-5.1.4.tar

[tblgrant@silo ~]$ cd php-5.1.4/

[tblgrant@silo php-5.1.4]$ ls

acconfig.h ltmain.sh README.STREAMS

acconfig.h.in main README.SUBMITTING_PATCH

acinclude.m4 makedist README.TESTING

aclocal.m4 Makefile.frag README.TESTING2

build Makefile.gcov README.UNIX-BUILD-SYSTEM

buildconf Makefile.global README.WIN32-BUILD-SYSTEM

buildconf.bat makerpm README.Zeus

CODING_STANDARDS missing regex

config.guess mkinstalldirs run-tests.php

config.sub netware sapi

configure NEWS scripts

configure.in pear server-tests-config.php

CREDITS php5.spec.in server-tests.php

cvsclean php.gif snapshot

cvsclean.bat php.ini-dist stamp-h.in

ext php.ini-recommended stub.c

EXTENSIONS README.CVS-RULES tests

footer README.EXTENSIONS TODO

generated_lists README.EXT_SKEL TODO-5.1

genfiles README.input_filter TODO-PHP5

header README.PARAMETER_PARSING_API TSRM

INSTALL README.PHP4-TO-PHP5-THIN-CHANGES UPGRADING

install-sh README.QNX win32

LICENSE README.SELF-CONTAINED-EXTENSIONS Zend

[tblgrant@silo php-5.1.4]$

We are now ready to configure, build and install PHP.

Page 71: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

69

10.4 Configuring PHP

Move into the folder created by tar xvf and create a file install as indicatedbelow. Make it executable and run it. The first six lines end in backslashes.As you know a backslash at the end of a line escapes the newline characterthat follows, keeping everything as if it were written on one single line. The filecontains three commands the first being the configuration part.

[tblgrant@silo php-5.1.4]$ pico -w install

[tblgrant@silo php-5.1.4]$ ls -ld install

-rw------- 1 tblgrant projects 230 Jun 15 22:38 install

[tblgrant@silo php-5.1.4]$ ls -ld configure

-rwx------ 1 tblgrant projects 2948317 May 12 2006 configure

[tblgrant@silo php-5.1.4]$ chmod u+x install

[tblgrant@silo php-5.1.4]$ cat install

./configure \

--with-mysql=/u/tblgrant/mysql \

--with-apxs2=/u//tblgrant/apache/bin/apxs \

--with-config-file-path=/u/tblgrant/apache/conf \

--with-xml --enable-track-vars \

--prefix=/u/tblgrant/apache

make

make install

[tblgrant@silo php-5.1.4]$ ./install

creating cache ./config.cache

checking for Cygwin environment... no

checking for mingw32 environment... no

checking for egrep... grep -E

checking for a sed that does not truncate output... /bin/sed

checking host system type... x86_64-unknown-linux-gnu

checking target system type... x86_64-unknown-linux-gnu

checking for gcc... gcc

checking whether the C compiler (gcc ) works... yes

checking whether the C compiler (gcc ) is a cross-compiler... no

checking whether we are using GNU C... yes

checking whether gcc accepts -g... yes

checking whether gcc and cc understand -c and -o together...

[...]

Build complete.

(It is safe to ignore warnings about tempnam and tmpnam).

Installing PHP SAPI module: apache2handler

[...]

Installing PHP CLI binary: /u/tblgrant/apache/bin/

Installing PHP CLI man page: /u/tblgrant/apache/man/man1/

Installing build environment: /u/tblgrant/apache/lib/php/build/

Installing header files: /u/tblgrant/apache/include/php/

Installing helper programs: /u/tblgrant/apache/bin/

program: phpize

Page 72: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

70

program: php-config

Installing man pages: /u/tblgrant/apache/man/man1/

page: phpize.1

page: php-config.1

Installing PEAR environment: /u/tblgrant/apache/lib/php/

[PEAR] Archive_Tar - installed: 1.3.1

[PEAR] Console_Getopt - installed: 1.2

pear/PEAR can optionally use package "pear/XML_RPC" (version >= 1.4.0)

[PEAR] PEAR - installed: 1.4.9

Wrote PEAR system config file at: /u/tblgrant/apache/etc/pear.conf

You may want to add: /u/tblgrant/apache/lib/php to your php.ini include_path

Installing PDO headers: /u/tblgrant/apache/include/php/ext/pdo/

[tblgrant@silo php-5.1.4]$

10.5 Building PHP

Already done in the previous section, via make.

10.6 Installing PHP

Already done in the previous section, via make install.

10.7 Final configuration of PHP

Start by moving the php.ini-dist into ~/apache/conf:

[tblgrant@silo php-5.1.4]$ pwd

/u/tblgrant/php-5.1.4

[tblgrant@silo php-5.1.4]$ ls -ld php.ini-dist

-rw------- 1 tblgrant projects 41989 Feb 8 2006 php.ini-dist

[tblgrant@silo php-5.1.4]$ ls -ld ~/apache/conf/php*

ls: cannot access /u/tblgrant/apache/conf/php*: No such file or directory

[tblgrant@silo php-5.1.4]$ cp php.ini-dist ~/apache/conf/php.ini

[tblgrant@silo php-5.1.4]$ ls -ld ~/apache/conf/php*

-rw------- 1 tblgrant projects 41989 Jun 16 16:55 /u/tblgrant/apache/conf/php.ini

[tblgrant@silo php-5.1.4]$

Move into ~/apache/conf and make the following change3:

[tblgrant@silo php-5.1.4]$ pwd

/u/tblgrant/php-5.1.4

[tblgrant@silo php-5.1.4]$ cd ~/apache/conf/

[tblgrant@silo conf]$ pwd

/u/tblgrant/apache/conf

3That essentially indicates a folder where sessions will be stored. Notice that you also needto create this folder via mkdir.

Page 73: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

71

[tblgrant@silo conf]$ pico -w php.ini

[tblgrant@silo conf]$ diff php.ini ~/php-5.1.4/php.ini-dist

891c891

< session.save_path = "/u/tblgrant/apache/phpsessions"

---

> ;session.save_path = "/tmp"

[tblgrant@silo conf]$ ^C

[tblgrant@silo conf]$

Create the folder discussed above then make the following two changes insidethe file httpd.conf:

[tblgrant@silo conf]$ pwd

/u/tblgrant/apache/conf

[tblgrant@silo conf]$ mkdir ~/apache/phpsessions

[tblgrant@silo conf]$ cp httpd.conf httpd.conf-backup

[tblgrant@silo conf]$ pico -w httpd.conf

[tblgrant@silo conf]$ diff httpd.conf httpd.conf-backup

290,292d289

< AddType application/x-httpd-php .php .phtml

< AddType application/x-httpd-php-source .phps

<

[tblgrant@silo conf]$

Stop Apache, make sure that it’s stopped, then start it again. You shouldhave PHP installed. You can verify that by looking into ~/apache/logs/error log:

[tblgrant@silo conf]$ pwd

/u/tblgrant/apache/conf

[tblgrant@silo conf]$ ps -ef | grep tblgrant

dgerman 12212 12145 0 Jun15 pts/27 00:00:00 ssh silo -l tblgrant

root 12213 23297 0 Jun15 ? 00:00:00 sshd: tblgrant [priv]

tblgrant 12215 12213 0 Jun15 ? 00:00:00 sshd: tblgrant@pts/30

tblgrant 12216 12215 0 Jun15 pts/30 00:00:00 -bash

tblgrant 15796 1 0 Jun12 ? 00:00:04 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15797 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15798 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15799 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15800 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 15801 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16533 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 16535 15796 0 Jun12 ? 00:00:00 /u/tblgrant/apache/bin/httpd -k start

tblgrant 18417 12216 0 17:14 pts/30 00:00:00 ps -ef

tblgrant 18418 12216 0 17:14 pts/30 00:00:00 grep tblgrant

tblgrant 24100 1 0 Jun14 ? 00:00:00 /bin/sh /u/tblgrant/mysql/bin/mysqld_[...]

tblgrant 24211 24100 0 Jun14 ? 00:00:00 /u/tblgrant/mysql/libexec/mysqld --ba[...]

[tblgrant@silo conf]$ ~/apache/bin/apachectl stop

[tblgrant@silo conf]$ ps -ef | grep tblgrant

dgerman 12212 12145 0 Jun15 pts/27 00:00:00 ssh silo -l tblgrant

root 12213 23297 0 Jun15 ? 00:00:00 sshd: tblgrant [priv]

Page 74: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

72

tblgrant 12215 12213 0 Jun15 ? 00:00:00 sshd: tblgrant@pts/30

tblgrant 12216 12215 0 Jun15 pts/30 00:00:00 -bash

tblgrant 18431 12216 1 17:15 pts/30 00:00:00 ps -ef

tblgrant 18432 12216 0 17:15 pts/30 00:00:00 grep tblgrant

tblgrant 24100 1 0 Jun14 ? 00:00:00 /bin/sh /u/tblgrant/mysql/bin/mysqld_[...]

tblgrant 24211 24100 0 Jun14 ? 00:00:00 /u/tblgrant/mysql/libexec/mysqld --ba[...]

[tblgrant@silo conf]$ tail -6 ~/apache/logs/error_log

[Tue Jun 12 12:23:56 2012] [notice] caught SIGTERM, shutting down

[Tue Jun 12 12:24:01 2012] [notice] Apache/2.2.2 (Unix) configured -- resuming normal[...]

[Tue Jun 12 12:24:32 2012] [notice] caught SIGTERM, shutting down

[Tue Jun 12 12:24:41 2012] [notice] Apache/2.2.2 (Unix) configured -- resuming normal[...]

[Tue Jun 12 12:40:14 2012] [error] [client 75.15.13.161] File does not exist: /u/tblg[...]

[Sat Jun 16 17:15:03 2012] [notice] caught SIGTERM, shutting down

[tblgrant@silo conf]$ ~/apache/bin/apachectl start

[tblgrant@silo conf]$ tail -6 ~/apache/logs/error_log

[Tue Jun 12 12:24:01 2012] [notice] Apache/2.2.2 (Unix) configured -- resuming normal[...]

[Tue Jun 12 12:24:32 2012] [notice] caught SIGTERM, shutting down

[Tue Jun 12 12:24:41 2012] [notice] Apache/2.2.2 (Unix) configured -- resuming normal[...]

[Tue Jun 12 12:40:14 2012] [error] [client 75.15.13.161] File does not exist: /u/tblg[...]

[Sat Jun 16 17:15:03 2012] [notice] caught SIGTERM, shutting down

[Sat Jun 16 17:15:39 2012] [notice] Apache/2.2.2 (Unix) PHP/5.1.4 configured -- resum[...]

[tblgrant@silo conf]$

10.8 Verifying your PHP installation

The plan is very simple: from conf you need to move in htdocs where your.html and .php files are located. Create a file with the contents below, call itone.php. Create a symbolic link to it, in the same folder, and call it one.phps.The extension will help the server understand that the output should be format-ted as source code. As soon as you have everything done on silo you shouldopen a browser and access the file(s) online. First go here:

http://silo.cs.indiana.edu:61200/one.phps

This will access the symbolic link and will show the source code of the file.Try it, it’s worth seeing it in the browser. Next access the program directly:

http://silo.cs.indiana.edu:61200/one.phps

Play with it a little. It’s a simple ATM program that keeps track of a ficti-tious balance. What follows is a snapshot of what I did to create the program:

[tblgrant@silo conf]$ pwd

/u/tblgrant/apache/conf

[tblgrant@silo conf]$ cd ../htdocs/

[tblgrant@silo htdocs]$ pico one.php

[tblgrant@silo htdocs]$ ls -ld one*

-rw------- 1 tblgrant projects 1004 Jun 16 17:25 one.php

[tblgrant@silo htdocs]$ ln -s one.php one.phps

Page 75: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

73

[tblgrant@silo htdocs]$ ls -ld one*

-rw------- 1 tblgrant projects 1004 Jun 16 17:25 one.php

lrwxrwxrwx 1 tblgrant projects 7 Jun 16 17:25 one.phps -> one.php

[tblgrant@silo htdocs]$ cat one.php

<? session_start();

$balance = $_SESSION["balance"];

$action = $_REQUEST["action"];

$amount = $_REQUEST["amount"];

if ($balance == NULL || $action == "Reset") {

$balance = "0";

$feedback = "Welcome!";

} else {

if ($action == "Deposit") {

$balance = $balance + $amount;

$feedback = "You just deposited " . $amount;

} else if ($action == "Withdraw") {

$balance = $balance - $amount;

$feedback = "You just withdrew " . $amount;

} else { // not a safe assumption, but a safe reaction to this input

$feedback = "You’re checking the balance now (" . $balance . ")";

}

}

$_SESSION["balance"] = $balance . "";

?>

<form>

<?=$feedback?> <p>

Balance is: <?=$balance?> <p>

Amount: <input type=text name=amount>

<input type=submit name=action value="Deposit">

<input type=submit name=action value="Withdraw">

<input type=submit name=action value="Check Balance">

<input type=submit name=action value="Reset">

</form>

[tblgrant@silo htdocs]$

The session this program works with is in ~/apache/phpsessions. Take alook: the name is fairly random and the contents depends on what the programwants to store in the session (in this case, only one value, identified by the namebalance; for me, below, its value is "0").

[tblgrant@silo htdocs]$ pwd

/u/tblgrant/apache/htdocs

[tblgrant@silo htdocs]$ ls -ld ../phpsessions/

drwx------ 2 tblgrant projects 4096 Jun 16 17:28 ../phpsessions/

Page 76: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

74

[tblgrant@silo htdocs]$ cd ../phpsessions/

[tblgrant@silo phpsessions]$ ls

sess_3b2bc760df27cf02dc8bdbc1d3028ac7

[tblgrant@silo phpsessions]$ cat sess_3b2bc760df27cf02dc8bdbc1d3028ac7

balance|s:1:"0";[tblgrant@silo phpsessions]$

[tblgrant@silo phpsessions]$

Page 77: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Part II

The MVC Web

75

Page 78: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...
Page 79: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 11

CakePHP

11.1 Getting ready

Now that you have PHP installed let’s create a place in htdocs for your CakePHPdevelopment:

[tblgrant@silo htdocs]$ pwd

/u/tblgrant/apache/htdocs

[tblgrant@silo htdocs]$ mkdir cakewalks

[tblgrant@silo htdocs]$ cd cakewalks/

[tblgrant@silo cakewalks]$ pwd

/u/tblgrant/apache/htdocs/cakewalks

[tblgrant@silo cakewalks]$ ls

[tblgrant@silo cakewalks]$

11.2 Downloading CakePHP

Make a folder one inside cakewalks and copy CakePHP in there:

[tblgrant@silo cakewalks]$ pwd

/u/tblgrant/apache/htdocs/cakewalks

[tblgrant@silo cakewalks]$ ls

[tblgrant@silo cakewalks]$ ls /l/www/classes/a202/software/cake*

/l/www/classes/a202/software/cakephp-cakephp-1.3.11-0-gc73ae84.tar.gz

[tblgrant@silo cakewalks]$ cp /l/www/classes/a202/software/cake* .

[tblgrant@silo cakewalks]$ ls

cakephp-cakephp-1.3.11-0-gc73ae84.tar.gz

[tblgrant@silo cakewalks]$ ls -l

total 968

-rw------- 1 tblgrant projects 983047 Jun 16 18:59 cakephp-cakephp-1.3.11-0-gc73ae84.tar.gz

[tblgrant@silo cakewalks]$ gunzip cakephp-cakephp-1.3.11-0-gc73ae84.tar.gz

[tblgrant@silo cakewalks]$ ls -l

total 7024

77

Page 80: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

78

-rw------- 1 tblgrant projects 7178240 Jun 16 18:59 cakephp-cakephp-1.3.11-0-gc73ae84.tar

[tblgrant@silo cakewalks]$

11.3 Uncompressing CakePHP

Already done in the previous section, via gunzip.

11.4 Installing CakePHP

Un-tar the archive, remove it. Rename the folder created and move in.

[tblgrant@silo cakewalks]$ pwd

/u/tblgrant/apache/htdocs/cakewalks

[tblgrant@silo cakewalks]$ ls

cakephp-cakephp-1.3.11-0-gc73ae84.tar

[tblgrant@silo cakewalks]$ tar xvf cakephp-cakephp-1.3.11-0-gc73ae84.tar

cakephp-cakephp-3b830a4/

cakephp-cakephp-3b830a4/.gitignore

cakephp-cakephp-3b830a4/.htaccess

[...]

cakephp-cakephp-3b830a4/vendors/shells/templates/

cakephp-cakephp-3b830a4/vendors/shells/templates/empty

[tblgrant@silo cakewalks]$ ls

cakephp-cakephp-1.3.11-0-gc73ae84.tar cakephp-cakephp-3b830a4

[tblgrant@silo cakewalks]$ rm *.tar

[tblgrant@silo cakewalks]$ ls

cakephp-cakephp-3b830a4

[tblgrant@silo cakewalks]$ mv cakephp-cakephp-3b830a4/ one

[tblgrant@silo cakewalks]$ ls

one

[tblgrant@silo cakewalks]$ cd one

[tblgrant@silo one]$ pwd

/u/tblgrant/apache/htdocs/cakewalks/one

[tblgrant@silo one]$ ls

app cake index.php plugins README vendors

[tblgrant@silo one]$ ls -a

. .. app cake .gitignore .htaccess index.php plugins README vendors

[tblgrant@silo one]$

11.5 Configuring CakePHP

We will follow the on-line manual1 at first. It doesn’t really matter in whatorder you choose to perform the steps below, as long as the steps are indepen-dent. The manual no longer says that but since our Apache is installed withoutmod rewrite we need to configure CakePHP accordingly. This used to be one

1http://book.cakephp.org/1.3/view/876/The-Manual

Page 81: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

79

of the first steps, followed by the database configuration step. Let’s approachthem in the same order below.

First, in your ~/apache/htdocs/cakewalks/one/app/config/core.php filesearch for a section that looks like this:

/**

* To configure CakePHP *not* to use mod_rewrite and to

* use CakePHP pretty URLs, remove these .htaccess

* files:

*

* /.htaccess

* /app/.htaccess

* /app/webroot/.htaccess

*

* And uncomment the App.baseUrl below:

*/

//Configure::write(’App.baseUrl’, env(’SCRIPT_NAME’));

Before you do anything access your CakePHP online as follows:

http://silo.cs.indiana.edu:61200/cakewalks/one/

Click on the index.php link that takes you to:

http://silo.cs.indiana.edu:61200/cakewalks/one/index.php

You need to take a look at this before you make any changes. Once yousaw the page please delete the three .htaccess files indicated in core.php anduncomment the line below that comment. Here are the steps:

[tblgrant@silo config]$ pwd

/u/tblgrant/apache/htdocs/cakewalks/one/app/config

[tblgrant@silo config]$ pico -w core.php

[tblgrant@silo config]$ grep -in baseurl core.php

67: * And uncomment the App.baseUrl below:

69: Configure::write(’App.baseUrl’, env(’SCRIPT_NAME’));

[tblgrant@silo config]$ ls -ld ../.htaccess

-rw------- 1 tblgrant projects 141 Jul 26 2011 ../.htaccess

[tblgrant@silo config]$ ls -ld ../../.htaccess

-rw------- 1 tblgrant projects 139 Jul 26 2011 ../../.htaccess

[tblgrant@silo config]$ ls -ld ../webroot/.htaccess

-rw------- 1 tblgrant projects 185 Jul 26 2011 ../webroot/.htaccess

[tblgrant@silo config]$ rm ../.htaccess

[tblgrant@silo config]$ rm ../../.htaccess

[tblgrant@silo config]$ rm ../webroot/.htaccess

[tblgrant@silo config]$

Now access the index.php again. Two things worth pointing out: (a)the style sheets have kicked in and (b) the database configuration file is not

Page 82: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

80

present. The instructions are to copy database.php.default into a new filedatabase.php and write inside the coordinates of your database server. Here’show I did that:

[tblgrant@silo config]$ pwd

/u/tblgrant/apache/htdocs/cakewalks/one/app/config

[tblgrant@silo config]$ ls

acl.ini.php bootstrap.php core.php database.php.default routes.php schema

[tblgrant@silo config]$ cp database.php.default database.php

[tblgrant@silo config]$ pico -w database.php

[tblgrant@silo config]$ diff database.php database.php.default

79,82c79,82

< ’host’ => ’silo.cs.indiana.edu’,

< ’login’ => ’fabregas’,

< ’password’ => ’c3sc’,

< ’database’ => ’thursday’,

---

> ’host’ => ’localhost’,

> ’login’ => ’user’,

> ’password’ => ’password’,

> ’database’ => ’database_name’,

84d83

< ’port’ => ’61230’

[tblgrant@silo config]$

As soon as I did that I accessed index.php online again. The page now saysthat the database configuration file is present and that Cake is able to connectto the database.

Page 83: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 12

CakePHP Blog

Still following the online manual1 we will develop a simple Blog program. Thesteps that we are following are in chapter 112 of the mentioned manual. Let’sreview all the steps here.

We start with an introduction that enumerates the things we need3. Wehave or are about to acquire all of these. Next4 the tutorial instructs us toget and install Cake; we have already done that. Check that the info on thepage is consistent with what you think (or, even beter, remember) we have donethus far. The Blog application that we will develop must rely on a database,where all the posts will be stored. We create5 this database and because this issomething we have not done yet we include the steps below:

[tblgrant@silo config]$ pwd

/u/tblgrant/apache/htdocs/cakewalks/one/app/config

[tblgrant@silo config]$ cd

[tblgrant@silo ~]$ cd mysql

[tblgrant@silo mysql]$ ls -ld connect_as_*

-rwx------ 1 tblgrant projects 107 Jun 14 21:06 connect_as_fabregas

-rwx------ 1 tblgrant projects 76 Jun 14 16:23 connect_as_root

[tblgrant@silo mysql]$ ./connect_as_fabregas

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 14

Server version: 5.1.58-log Source distribution

[...]

mysql> use thursday

[...]

Database changed

1http://book.cakephp.org/1.3/view/876/The-Manual2http://book.cakephp.org/1.3/en/view/1528/Blog3http://book.cakephp.org/1.3/en/view/1528/Blog4http://book.cakephp.org/1.3/en/view/1529/Getting-Cake5http://book.cakephp.org/1.3/en/view/1530/Creating-the-Blog-Database

81

Page 84: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

82

mysql> show tables;

+--------------------+

| Tables_in_thursday |

+--------------------+

| enrollment |

| matches |

| participation |

| players |

| spectators |

| venues |

+--------------------+

6 rows in set (0.00 sec)

mysql> /* First, create our posts table: */

mysql>

mysql> CREATE TABLE posts (

-> id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

-> title VARCHAR(50),

-> body TEXT,

-> created DATETIME DEFAULT NULL,

-> modified DATETIME DEFAULT NULL

-> );

mysql>

mysql> /* Then insert some posts for testing: */

mysql>

mysql> INSERT INTO posts (title,body,created)

-> VALUES (’The title’, ’This is the post body.’, NOW());

Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO posts (title,body,created)

-> VALUES (’A title once again’, ’And the post body follows.’, NOW());

Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO posts (title,body,created)

-> VALUES (’Title strikes back’, ’This is really exciting! Not.’, NOW())

Query OK, 1 row affected (0.00 sec)

mysql> show tables;

+--------------------+

| Tables_in_thursday |

+--------------------+

| enrollment |

| matches |

| participation |

| players |

| posts |

| spectators |

| venues |

+--------------------+

7 rows in set (0.01 sec)

Page 85: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

83

mysql> describe posts;

+----------+------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+----------+------------------+------+-----+---------+----------------+

| id | int(10) unsigned | NO | PRI | NULL | auto_increment |

| title | varchar(50) | YES | | NULL | |

| body | text | YES | | NULL | |

| created | datetime | YES | | NULL | |

| modified | datetime | YES | | NULL | |

+----------+------------------+------+-----+---------+----------------+

5 rows in set (0.00 sec)

mysql> select * from posts;

+----+--------------------+-------------------------------+---------------------+----------+

| id | title | body | created | modified |

+----+--------------------+-------------------------------+---------------------+----------+

| 1 | The title | This is the post body. | 2012-06-16 20:53:51 | NULL |

| 2 | A title once again | And the post body follows. | 2012-06-16 20:53:51 | NULL |

| 3 | Title strikes back | This is really exciting! Not. | 2012-06-16 20:53:51 | NULL |

+----+--------------------+-------------------------------+---------------------+----------+

3 rows in set (0.00 sec)

mysql> exit

Bye

[tblgrant@silo mysql]$

Next the tutorial instructs us to set up the database configuration6 and we’ve done that already. The next7 section is an optional configuration, whichwe will skip for now. It is followed by a note8 presenting considerations havingto do with mod rewrite and, again, this is something we have accomplishedalready9. From here on the tutorial actually starts the CakePHP development.

The tutorial goes step by step. We’re going to take an orthogonal view tothat and tell you up front that our development will result in:

• one model10

• one controller11, and

• four views: index12, view13, add14 and edit15

6http://book.cakephp.org/1.3/en/view/1531/Cake-Database-Configuration7http://book.cakephp.org/1.3/en/view/1532/Optional-Configuration8http://book.cakephp.org/1.3/en/view/1533/A-Note-on-mod rewrite9However, it is a very important note and we will have to refer back it one more time.

10app/models/post.php11app/controllers/posts controller.php12app/views/posts/index.ctp13app/views/posts/view.ctp14app/views/posts/add.ctp15app/views/posts/edit.ctp

Page 86: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

84

You know where to put them, let me show you the final contents of thesefour files:

[tblgrant@silo mysql]$ cd

[tblgrant@silo ~]$ pwd

/u/tblgrant

[tblgrant@silo ~]$ cd apache/htdocs/cakewalks/one/

[tblgrant@silo one]$ pwd

/u/tblgrant/apache/htdocs/cakewalks/one

[tblgrant@silo one]$ pico -w app/models/post.php

[tblgrant@silo one]$ cat app/models/post.php

<?php class Post extends AppModel {

var $name = ’Post’;

var $validate = array( ’title’ => array( ’rule’ => ’notEmpty’ ),

’body’ => array( ’rule’ => ’notEmpty’ )

);

}

?>

[tblgrant@silo one]$ cat app/controllers/posts_controller.php

<?php class PostsController extends AppController {

var $name = ’Posts’;

var $components = array(’Session’);

function index() {

$this->set(’posts’, $this->Post->find(’all’));

}

function view($id) {

$this->Post->id = $id;

$this->set(’post’, $this->Post->read());

}

function add() {

if (!empty($this->data)) {

if ($this->Post->save($this->data)) {

$this->Session->setFlash(’Your post has been saved.’);

$this->redirect(array(’action’ => ’index’));

}

}

}

function edit($id = null) {

$this->Post->id = $id;

if (empty($this->data)) {

$this->data = $this->Post->read();

} else {

if ($this->Post->save($this->data)) {

$this->Session->setFlash(’Your post has been updated.’);

$this->redirect(array(’action’ => ’index’));

}

}

}

function delete($id) {

if ($this->Post->delete($id)) {

Page 87: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

85

$this->Session->setFlash(’The post with id: ’ . $id . ’ has been deleted.’);

$this->redirect(array(’action’ => ’index’));

}

}

}

?>

[tblgrant@silo one]$ mkdir app/views/posts

[tblgrant@silo one]$ pico -w app/views/posts/index.ctp

[tblgrant@silo one]$ cat app/views/posts/index.ctp

<!-- File: /app/views/posts/index.ctp (edit links added) -->

<h1>Blog posts</h1> <p>

<?php echo $this->Html->link("Add Post", array(’action’ => ’add’)); ?></p>

<table>

<tr>

<th>Id</th> <th>Title</th> <th>Action</th> <th>Created</th>

</tr>

<!-- Here’s where we loop through our $posts array, printing out post info -->

<?php foreach ($posts as $post): ?>

<tr>

<td><?php echo $post[’Post’][’id’]; ?></td>

<td>

<?php echo $this->Html->link( $post[’Post’][’title’],

array( ’action’ => ’view’,

$post[’Post’][’id’]

)

);?>

</td>

<td>

<?php echo $this->Html->link( ’Delete’,

array( ’action’ => ’delete’,

$post[’Post’][’id’]

),

null,

’Are you sure?’

);?>

<?php echo $this->Html->link( ’Edit’,

array( ’action’ => ’edit’,

$post[’Post’][’id’]

)

);?>

</td>

<td><?php echo $post[’Post’][’created’]; ?></td>

</tr>

<?php endforeach; ?>

Page 88: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

86

</table>

[tblgrant@silo one]$

The snapshot above shows the model, the controller and one of the views.Note that the folders for model(s), controller(s) already exist. We need to createthe post folder inside the views folder before we start creating the .ctp files. Asfar the extension (.ctp) goes it’s supposed to stand for “cake[php] template”.

Here’s the source code for the other three template files (views):

[tblgrant@silo one]$ pwd

/u/tblgrant/apache/htdocs/cakewalks/one

[tblgrant@silo one]$ pico -w app/views/posts/edit.ctp

[tblgrant@silo one]$ cat app/views/posts/edit.ctp

<!-- File: /app/views/posts/edit.ctp -->

<h1>Edit Post</h1>

<?php echo $this->Form->create(’Post’, array(’action’ => ’edit’));

echo $this->Form->input(’title’);

echo $this->Form->input(’body’, array(’rows’ => ’3’));

echo $this->Form->input(’id’, array(’type’ => ’hidden’));

echo $this->Form->end(’Save Post’);

?>

[tblgrant@silo one]$ pico -w app/views/posts/add.ctp

[tblgrant@silo one]$ cat app/views/posts/add.ctp

<!-- File: /app/views/posts/add.ctp -->

<h1>Add Post</h1>

<?php echo $this->Form->create(’Post’);

echo $this->Form->input(’title’);

echo $this->Form->input(’body’, array(’rows’ => ’3’));

echo $this->Form->end(’Save Post’);

?>

[tblgrant@silo one]$ pico -w app/views/posts/view.ctp

[tblgrant@silo one]$ cat app/views/posts/view.ctp

<!-- File: /app/views/posts/view.ctp -->

<h1><?php echo $post[’Post’][’title’]?></h1>

<p><small>Created: <?php echo $post[’Post’][’created’]?></small></p>

<p><?php echo $post[’Post’][’body’]?></p>

[tblgrant@silo one]$

Test your blog application with:

http://silo.cs.indiana.edu:61200/cakewalks/one/index.php/posts/index

Page 89: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 13

CakePHP1.3 Recipes

This1 is a book we’re making available under a special agreement.

1https://www.cs.indiana.edu/~dgerman/sum2012/micp.pdf

87

Page 90: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

88

Page 91: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 14

Django

89

Page 92: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

90

Page 93: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Part III

Lectures/Assignments/Labs

91

Page 94: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...
Page 95: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Chapter 15

Week of June 18-21

15.1 Lecture One

This lecture will give us an opportunity to get to know each other. I will showthe class website1 and we will go over the Calendar2, Schedule3, Instructors4

and Grades5 parts of this text. I will also indicate where6 the text can be found,along with additional class materials7. I will then ask, and collect your answersto, the following questions:

1. Name, username

2. Major, reasons for taking this class

3. Do you have a silo account?

4. Briefly describe your programming background.

5. Define to the best of your abilities: Unix, HTML, Python, Apache, HTTP.

6. Expectations for this class: what would you want to be able to do at theend to feel accomplished?

7. We endeavor to make this class the best experience you ever had at IU.Please mention any fears or concerns you may have with respect to thisclass. Any other comments and/or questions are welcome now and always.

Next we will go over the basic Unix commands needed for this class. We’llfollow the steps in Chapter 1 (“Unix”) and become familiar with the following

1http://www.cs.indiana.edu/classes/a2022http://www.cs.indiana.edu/classes/a202-dger/sum2012/whatsnew.html3http://www.cs.indiana.edu/classes/a202-dger/sum2012/classNotes.html4http://www.cs.indiana.edu/classes/a202-dger/sum2012/instructorsHours.pdf5http://www.cs.indiana.edu/classes/a202-dger/sum2012/whatsdue.pdf6https://www.cs.indiana.edu/~dgerman/sum2012/a202notes.pdf7http://www.libraries.iub.edu/scripts/countResources.php?resourceId=59555

93

Page 96: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

94

Unix commands and symbols: clear, mkdir, pwd, cd, ls, ls -l, ls -ld, touch,pico, pico -w, du -a, tar cvf, chmod, cat, ., .., javac, java, gzip andothers. If we have time we will install Apache.

Page 97: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

Contents

I The Basic Web 11

1 Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2 Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.1 Downloading Apache . . . . . . . . . . . . . . . . . . . . . . . . . 192.2 A tour of the Apache folders . . . . . . . . . . . . . . . . . . . . 232.3 Configuring Apache . . . . . . . . . . . . . . . . . . . . . . . . . 232.4 Starting and stopping Apache . . . . . . . . . . . . . . . . . . . . 242.5 Accessing your Apache server with a browser . . . . . . . . . . . 26

3 HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4 A Note on Open-Source . . . . . . . . . . . . . . . . . . . . . . . 29

5 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

6 CGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

7 HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

8 MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378.1 Downloading MySQL . . . . . . . . . . . . . . . . . . . . . . . . . 378.2 Uncompressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378.3 Unarchiving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378.4 Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 388.5 Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . 398.6 Starting MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 408.7 Checking MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 428.8 Stopping MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 428.9 Connecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

8.9.1 Connecting as root . . . . . . . . . . . . . . . . . . . . . . 448.9.2 Changing the root password . . . . . . . . . . . . . . . . . 468.9.3 Creating a user account . . . . . . . . . . . . . . . . . . . 478.9.4 Creating a database . . . . . . . . . . . . . . . . . . . . . 498.9.5 Creating a user . . . . . . . . . . . . . . . . . . . . . . . . 498.9.6 Granting access rights . . . . . . . . . . . . . . . . . . . . 49

95

Page 98: A202/A590 CGI/PHP Fall 2012 Class Notes - … · These are the notes for A290/A590 CGI/PHP first eight weeks Fall 2012. ... Chapter 1 Unix Each one of you ... Name them perl, ...

96

8.9.7 Connecting as a regular user . . . . . . . . . . . . . . . . 50

9 Database Design . . . . . . . . . . . . . . . . . . . . . . . . . . . 519.1 Entity-relationship model . . . . . . . . . . . . . . . . . . . . . . 519.2 A case study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

9.2.1 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529.2.2 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569.2.3 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

10 PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6710.1 Downloading PHP . . . . . . . . . . . . . . . . . . . . . . . . . . 6710.2 Uncompressing PHP . . . . . . . . . . . . . . . . . . . . . . . . . 6710.3 Unarchiving PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . 6710.4 Configuring PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . 6910.5 Building PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7010.6 Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7010.7 Final configuration of PHP . . . . . . . . . . . . . . . . . . . . . 7010.8 Verifying your PHP installation . . . . . . . . . . . . . . . . . . . 72

II The MVC Web 75

11 CakePHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7711.1 Getting ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7711.2 Downloading CakePHP . . . . . . . . . . . . . . . . . . . . . . . 7711.3 Uncompressing CakePHP . . . . . . . . . . . . . . . . . . . . . . 7811.4 Installing CakePHP . . . . . . . . . . . . . . . . . . . . . . . . . 7811.5 Configuring CakePHP . . . . . . . . . . . . . . . . . . . . . . . . 78

12 CakePHP Blog . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

13 CakePHP1.3 Recipes . . . . . . . . . . . . . . . . . . . . . . . . . 87

14 Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

III Lectures/Assignments/Labs 91

15 Week of June 18-21 . . . . . . . . . . . . . . . . . . . . . . . . . . 9315.1 Lecture One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93