pavement

PHP

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
m (corrected a typo in an url)
m (Reverted edits by 190.27.10.162 (Talk); changed back to last version by Jimbo)
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
PHP is the recursive acronym for ''PHP Hypertext Preprocessor''. It is an interpreted script language commonly used for dynamic pages generation on webservers. Then it is generaly installed side by side with the [[Apache]] web server, and [[MySQL]] or [[PostgreSQL]] as database management system.
+
PHP is a recursive acronym for ''PHP Hypertext Preprocessor''. It is an interpreted script language commonly used for dynamic pages generation on webservers. It is generally installed side by side with the [[Apache]] web server, and [[MySQL]] or [[PostgreSQL]] as database management system.
  
 
==Installing PHP==
 
==Installing PHP==
  
PHP is available trougth the port-tree ...
+
PHP is available from the port-tree ...
 
  # cd /usr/ports/lang/php5
 
  # cd /usr/ports/lang/php5
 
... and the packages ...
 
... and the packages ...
Line 10: Line 10:
 
==Using PHP==
 
==Using PHP==
 
There are two ways to use PHP :
 
There are two ways to use PHP :
* With apache, configured to make PHP to proceed .php files asked by the user.
+
* With apache: configure apache to use php to process .php files requested by the user.
* As any any interpreter
+
* As a command interpreter: php can process scripts, or instructions typed at the commandline.
  
 
===PHP for dynamic websites===
 
===PHP for dynamic websites===
  
All the steps of Apache configuration are explained after PHP installation. You have to add these two lines to httpd.conf:
+
All the steps of Apache configuration are explained after PHP installation. You have to add these two lines in httpd.conf:
 
  AddType application/x-httpd-php .php
 
  AddType application/x-httpd-php .php
 
  AddType application/x-httpd-php-source .phps
 
  AddType application/x-httpd-php-source .phps
Line 23: Line 23:
  
 
You have to restart Apache to have your changes effective. For example, if you use Apache 2:
 
You have to restart Apache to have your changes effective. For example, if you use Apache 2:
  /usr/local/etc/rc.d/apache2 restart
+
  /usr/local/etc/rc.d/apache2.sh restart
  
Now, you can try your installation by creating a file test.php:
+
Now, you can try your installation by creating a file test.php in the /usr/local/www/data directory:
 
  <?
 
  <?
 
     phpinfo();
 
     phpinfo();
 
  ?>
 
  ?>
  
Pointing that file with your web browser should show you your php settings.
+
Request test.php using your web browser, and if all is well it will show you your PHP configuration.
  
===Shell scripts in PHP===
+
===Scripts in PHP===
  
You can write shell scripts in PHP, whitch is very useful for ''quick and dirty'' hacks... A PHP shell script looks like this :
+
If you have compiled PHP with the commandline interface, you can write scripts in PHP that can be used from the [[shell]]. This is very useful for ''quick and dirty'' hacks; especially if you want your output to be HTML. Put the PHP code in a file like this :
 +
<pre>
 
  #!/usr/local/bin/php
 
  #!/usr/local/bin/php
  <?  
+
  # phpscript
  // Your PHP code here ...
+
&lt;html&gt; &lt;title&gt;Defined Constants&lt;/title&gt; &lt;body&gt;
  echo 'Hello World !\n';
+
 
  ?>
+
&lt;h1&gt;&lt;? echo "Hello from PHP " .phpversion() ?&gt;&lt;/h1&gt;
 +
&lt;pre&gt;&lt;? print_r(get_defined_constants()) ?&gt;&lt;/pre&gt;
 +
 
 +
&lt;/body&gt;&lt;/html&gt;
 +
</pre>
 +
 
 +
Make the file executable:
 +
'''%''' chmod +x phpscript
 +
 
 +
Then, you can run it from the command line.
 +
 
 +
'''%''' ./phpscript | lynx -stdin
 +
In this case, we've piped to <code>lynx</code>, so that we can browse the nicely formatted result.
 +
<pre style="background-color:black; color:white">
 +
                                              Defined Constants (p1 of 154)
 +
 
 +
                            Hello from PHP 5.1.4
 +
 
 +
Array
 +
(
 +
    [E_ERROR] => 1
 +
    [E_WARNING] => 2
 +
    [E_PARSE] => 4
 +
    [E_NOTICE] => 8
 +
-- press space for more, use arrow keys to move, '?' for help, 'q' to quit.
 +
</pre>
 +
 
 +
Or, we could have executed the command directly from the command line:
 +
'''%''' php -r 'echo "Hello from PHP ". phpversion() . "\n"; print_r(get_defined_constants());'
 +
  Hello from PHP 5.1.4
 +
Array
 +
(
 +
...
  
 
==PHP Website==
 
==PHP Website==
 
[http://www.php.net http://www.php.net]
 
[http://www.php.net http://www.php.net]
 +
 +
 +
[[Category: Ports and Packages]]
 +
[[Category: Configuring FreeBSD]]

Latest revision as of 22:52, 22 October 2009

PHP is a recursive acronym for PHP Hypertext Preprocessor. It is an interpreted script language commonly used for dynamic pages generation on webservers. It is generally installed side by side with the Apache web server, and MySQL or PostgreSQL as database management system.

Contents

[edit] Installing PHP

PHP is available from the port-tree ...

# cd /usr/ports/lang/php5

... and the packages ...

# pkg_add -r php5

[edit] Using PHP

There are two ways to use PHP :

  • With apache: configure apache to use php to process .php files requested by the user.
  • As a command interpreter: php can process scripts, or instructions typed at the commandline.

[edit] PHP for dynamic websites

All the steps of Apache configuration are explained after PHP installation. You have to add these two lines in httpd.conf:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

You will probably also want to add index.php as a possible directory index:

DirectoryIndex index.html index.php

You have to restart Apache to have your changes effective. For example, if you use Apache 2:

/usr/local/etc/rc.d/apache2.sh restart

Now, you can try your installation by creating a file test.php in the /usr/local/www/data directory:

<?
   phpinfo();
?>

Request test.php using your web browser, and if all is well it will show you your PHP configuration.

[edit] Scripts in PHP

If you have compiled PHP with the commandline interface, you can write scripts in PHP that can be used from the shell. This is very useful for quick and dirty hacks; especially if you want your output to be HTML. Put the PHP code in a file like this :

 #!/usr/local/bin/php
 # phpscript
 <html> <title>Defined Constants</title> <body>

 <h1><? echo "Hello from PHP " .phpversion() ?></h1>
 <pre><? print_r(get_defined_constants()) ?></pre>

 </body></html>

Make the file executable:

% chmod +x phpscript

Then, you can run it from the command line.

% ./phpscript | lynx -stdin

In this case, we've piped to lynx, so that we can browse the nicely formatted result.

                                              Defined Constants (p1 of 154)

                             Hello from PHP 5.1.4

 Array
 (
    [E_ERROR] => 1
    [E_WARNING] => 2
    [E_PARSE] => 4
    [E_NOTICE] => 8
 -- press space for more, use arrow keys to move, '?' for help, 'q' to quit.

Or, we could have executed the command directly from the command line:

% php -r 'echo "Hello from PHP ". phpversion() . "\n"; print_r(get_defined_constants());'
Hello from PHP 5.1.4 
Array
(
...

[edit] PHP Website

http://www.php.net

Personal tools