Buy this as as a 155-page colour PDF for only $3

Chapter 5 - PHP

The next stage, once the web server is up and running, is to install PHP. If we were building from source code, this is where it starts to get a little bit more fiddly, which is why packaging systems like rpm and apt really start to save you a lot of time and effort. All we need to do is to "yum install php", via sudo if we are not already root.

Install PHP

[john@lamp ~]$ sudo yum install php
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centoss5.centos.org
* extras: centosg4.centos.org
* updates: centosh2.centos.org
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.3.3-22.el6 will be installed
--> Processing Dependency: php-common(x86-64) = 5.3.3-22.el6 for package: php-5.3.3-22.el6.x86_64
--> Processing Dependency: php-cli(x86-64) = 5.3.3-22.el6 for package: php-5.3.3-22.el6.x86_64
--> Running transaction check
---> Package php-cli.x86_64 0:5.3.3-22.el6 will be installed
--> Processing Dependency: libedit.so.0()(64bit) for package: php-cli-5.3.3-22.el6.x86_64
---> Package php-common.x86_64 0:5.3.3-22.el6 will be installed
--> Running transaction check
---> Package libedit.x86_64 0:2.11-4.20080712cvs.1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
php x86_64 5.3.3-22.el6 base 1.1 M
Installing for dependencies:
libedit x86_64 2.11-4.20080712cvs.1.el6 base 74 k
php-cli x86_64 5.3.3-22.el6 base 2.2 M
php-common x86_64 5.3.3-22.el6 base 524 k

Transaction Summary
================================================================================
Install 4 Package(s)

Total download size: 3.9 M
Installed size: 13 M

Again, yum asks for confirmation before continuing

Is this ok [y/N]: y
Downloading Packages:
(1/4): libedit-2.11-4.20080712cvs.1.el6.x86_64.rpm | 74 kB 00:04
(2/4): php-5.3.3-22.el6.x86_64.rpm | 1.1 MB 00:16
(3/4): php-cli-5.3.3-22.el6.x86_64.rpm | 2.2 MB 00:15
(4/4): php-common-5.3.3-22.el6.x86_64.rpm | 524 kB 00:05
--------------------------------------------------------------------------------
Total 92 kB/s | 3.9 MB 00:43
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : php-common-5.3.3-22.el6.x86_64 1/4
Installing : libedit-2.11-4.20080712cvs.1.el6.x86_64 2/4
Installing : php-cli-5.3.3-22.el6.x86_64 3/4
Installing : php-5.3.3-22.el6.x86_64 4/4
Verifying : php-cli-5.3.3-22.el6.x86_64 1/4
Verifying : php-common-5.3.3-22.el6.x86_64 2/4
Verifying : libedit-2.11-4.20080712cvs.1.el6.x86_64 3/4
Verifying : php-5.3.3-22.el6.x86_64 4/4

Installed:
php.x86_64 0:5.3.3-22.el6

Dependency Installed:
libedit.x86_64 0:2.11-4.20080712cvs.1.el6 php-cli.x86_64 0:5.3.3-22.el6
php-common.x86_64 0:5.3.3-22.el6

Complete!
[john@lamp ~]$

Test PHP

Once PHP is installed, all that is needed is to restart Apache, and install a PHP-enabled test page.

[john@lamp ~]$ sudo service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[john@lamp ~]$

As root, go to /var/www/html, rename index.html as index.php so that Apache will know to pass the file to the PHP module for processing, rather than just passing the contents as-is to the client's web browser. Then edit the file, and add some PHP code to it.

[john@lamp ~]$ sudo su -
[root@lamp ~]# cd /var/www/html/
[root@lamp html]# ls
index.html
[root@lamp html]# mv index.html index.php
[root@lamp html]# vi index.php 

The <?php tag tells the PHP parser to start treating the following section as PHP code. The phpinfo() function is a PHP feature which displays a bunch of PHP configuration details.
The <?php tag is closed with the ?> closing bracket. After that, the text is treated as plain HTML, is not parsed by the PHP parser, and is passed straight on to the web browser as HTML text.

<html>
<body>
Hello, World! This is my very own web server!
 <?php
phpinfo();
?>
<body>
<html>

Reload the web page, and you should see the PHP-powered web page below:

Now that the web server is installed, it is time to get the MySQL database installed and configured.

How to Build a LAMP Server (Linux, Apache, MySQL, PHP)
Share on Twitter Share on Facebook Share on LinkedIn Share on Identi.ca Share on StumbleUpon