Installing and Configuring the PHP Engine

If you want to write some PHP programs, you need a PHP engine to turn them from punctuation-studded text files into actual interactive web pages. The easiest way to get up and running with PHP is to sign up with a cheap or free web-hosting provider that offers PHP—but you can run the PHP engine on your own computer, too.

1. Using PHP with a Web-Hosting Provider

If you already have an account with a web-hosting provider, you probably have access to a PHP-enabled server. These days, it is the odd web-hosting provider that doesn’t have PHP support. Usually, hosting providers configure their servers so that files whose names end in .php are treated as PHP programs. To see whether your hosted website supports PHP, first save the file in Example A-1 on your server as phptest.php.

Example A-1. PHP test program

<?php print “PHP enabled”; ?>

Load the file in your browser by visiting the URL for your site (e.g., http://www.exam- ple.com/phptest.php). If you see just the message PHP enabled, then your website host supports PHP. If you see the entire contents of the page (<?php print “PHP enabled”; ?>), then your hosting provider probably doesn’t support PHP. Check with them, however, to make sure that they haven’t turned on PHP for a different file extension or made some other nonstandard configuration choice.

2. Installing the PHP Engine

Installing the PHP engine on your own computer is a good idea if you don’t have an account with a hosting provider, or you just want to experiment with PHP without exposing your programs to the entire Internet. If you’re not using a hosting provider and want to install the PHP engine on your own computer, follow the instructions in this section. After you’ve installed the engine, you’ll be able to run your own PHP programs.

Installing the PHP engine is a matter of downloading some files and putting them in the right places on your computer. You may also need to configure your web server so that it knows about PHP. This section contains instructions on how to do this for computers running Linux and OS X, and also includes some references for how to install PHP on Windows. If you get stuck, check out the php.net installation FAQ.

2.1. Installing on OS X

OS X comes with PHP 5.5 installed. However, to install a newer version of PHP and be able to easily manage add-ons and extensions, you’ll want to install your own PHP engine using the Homebrew package manager. Homebrew helps you install OS X programs and the libraries those programs depend on.

First, install Homebrew if you don’t already have it installed. Visit http://brew.sh/ for all the nitty-gritty details, or just type the following into a Terminal prompt (on a sin­gle line):

ruby -e “$(curt -fsSL

https://raw.githubusercontent.com/Homebrew/install/master/install)”

If that’s too much to type, visit the Homebrew site, from which you can copy the command to the clipboard and paste it into Terminal.

Once Homebrew is installed, you need to tell it where to find the latest and greatest PHP. Run these commands to do so:

brew tap homebrew/dupes

brew tap homebrew/versions

brew tap homebrew/homebrew-php

Then, to install PHP 7, run brew install php70. That’s it!

At the end of installation, Homebrew prints out a bunch of stuff about configuring your setup. Pay attention to those instructions, since you need to follow them to tell your Mac’s copy of the Apache web server where to find PHP.

Homebrew includes a number of extensions (including intl and mbstring, used in Chapter 20), but also offers other PHP extensions for installation. Run brew search php70- to see a list of extension packages. Installing one of those extensions and its dependent libraries is as easy as running brew install with the extension package name. For example, brew install php70-gmp installs the GMP (GNU Multiple Pre­cision) extension for doing arbitrary-precision math with huge numbers.

Justin Hileman has more details on installing PHP with Homebrew.

2.2. Installing on Linux

Most Linux distributions come with PHP already installed or with binary PHP packages that you can install. For example, if you’re using Fedora Linux, use yum to install the php package. If you’re using Ubuntu, use apt-get to install the package. The most up-to-date PHP 5 package is php5, and at the time of writing, an official php7 package is not available yet. A well-supported PHP 7 package for Ubuntu is available from an alternate source. First, run sudo add-apt-repository ppa:ondrej/php and sudo apt-get update, and then you can install the php7.0 package with apt-get.

If those packages are out of date, you can build PHP yourself. Download the Current Stable .tar.gz package. From a shell prompt, uncompress and unpack the archive:

gunzip php-7.0.5.tar.gz

tar xvf php-7.0.5.tar

This creates a directory, php-7.0.5, that contains the PHP engine source code. Read the file INSTALL at the top level of the source code directory for detailed installation instructions. There is also an overview of PHP installation on Linux and Unix on php.net, as well as instructions for installing PHP with Apache 2.0.

2.3. Installing on Windows

Installing PHP on Windows is a little different than on OS X or Linux. The assump­tions that the PHP engine can make about the things it needs when it’s being installed are different, as well as the tools that might be available for it to compile itself.

Fortunately, there are several good all-in-one packages that combine PHP, Apache, and MySQL for Windows. These include WampServer, the Bitnami WAMP Stack, and Apache Friends XAMPP.

Microsoft maintains a website devoted to running PHP with IIS. Additionally, the official PHP For Windows website has different versions of PHP for Windows avail­able for download.

3. Modifying PHP Configuration Directives

Earlier chapters in the book mention various PHP configuration directives. These are settings that affect the behavior of the PHP engine, such as how errors are reported, where the PHP engine looks for included files and extensions, and much more.

Read this section when you encounter a configuration directive you want to alter or are curious about how you can tweak the PHP engine’s settings (whether you are using PHP on your own computer or with a hosting provider). For example, chang­ing the output_buffering directive (as discussed in “Why setcookie() and ses­sion_start() Want to Be at the Top of the Page” on page 226) makes your life much easier if you are working with cookies and sessions.

The values of configuration directives can be changed in a few places: in the PHP engine’s php.ini configuration file, in Apache’s httpd.conf or .htaccess configuration files, and in your PHP programs. Not all configuration directives can be changed in all places. If you can edit your php.ini or httpd.conf file, it’s easiest to set PHP configu­ration directives there. But if you can’t change those files because of server permis­sions, then you can still change some settings in your PHP programs.

If your web server talks to the PHP engine using CGI or FastCGI, you can also set configuration directives in .user.ini files. In PHP 5.3.0 and later, the PHP engine looks for a file called .user.ini in the same directory as the PHP program it’s running. If the PHP program is inside the web server’s document root, the PHP engine also looks in the program’s parent directory, and that directory’s parent, and so on, up to the docu­ment root. The syntax for .user.ini files is the same as for the main php.ini file.

The php.ini file holds systemwide configuration for the PHP engine. When the web server process starts up, the PHP engine reads the php.ini file and adjusts its configu­ration accordingly. To find the location of your system’s php.ini file, examine the out­put from the phpinfo() function. This function prints a report of the PHP engine’s configuration. The tiny program in Example A-2 produces a page that looks like the one in Figure A-1.

Example A-2. Getting configuration details with phpinfo()

<?php phpinfo(); ?>

In Figure A-1, the sixth line (Configuration File (php.ini) Path) shows that the php.ini file is located at /php7/etc/php.ini. Your php.ini file may be in a different place.

Figure A-1. Output of phpinfo()

In the php.ini file, lines that begin with a semicolon (;) are comments. Lines that set values for configuration directives look like those shown in Example A-3.

Example A-3. Sample lines in php.ini

; How to specify directories on Unix: forward slash for a separator

; and a colon between the directory names

include_path = “.:/usr/local/lib/php/includes”

; How to specify directories on Windows: backslash for a separator

; and a semicolon between the directory names

; Windows: “\path1;\path2”

include_path = “.;c:\php\inctudes”

; Report all errors except notices

error_reporting = E_ALL & ~E_NOTICE

; Record errors in the error log

log_errors = On

; An uploaded file can’t be more than 2 megabytes

uptoad_max_fitesize = 2M

; Sessions expire after 1440 seconds

session.gc_maxtifetime = 1440

The error_reporting configuration directive is set by combining built-in constants with logical operators. For example, the line error_reporting = E_ALL & ~E_NOTICE sets error_reporting to E_ALL but not E_NOTICE. The operators you can use are & (“and”), | (“either … or”), and ~ (“not”). So, to the PHP engine, E_ALL & ~E_NOTICE means E_ALL and not E_NOTICE. You may find it easier to read “and not” as “but not,” as in E_ALL but not E_NOTICE. The setting E_ALL | E_NOTICE means either E_ALL or E_NOTICE.

When setting a configuration directive whose value is a number (such as uptoad_max_fitesize), you can use M (for megabyte) or K (for kilobyte) at the end of the number to multiply by 1,048,576 or 1,024. Setting uptoad_max_fitesize=2M is the same as setting uptoad_max_fitesize=2097152. There are 1,048,576 bytes in a megabyte, and 2,097,152 = 2 * 1,048,576.

To change a configuration directive in Apache’s httpd.conf or .htaccess file, you must use a slightly different syntax, shown in Example A-4.

Example A-4. Sample PHP configuration lines in httpd.conf

; How to specify directories on Unix: forward stash for a separator

; and a coton between the directory names

php_vatue inctude_path “.:/usr/tocat/tib/php/inctudes”

 

; How to specify directories on Windows: backstash for a separator

; and a semicoton between the directory names

; Windows: “\path1;\path2”

php_vatue inctude_path “.;c:\php\inctudes”

 

; Report att errors but notices

php_vatue error_reporting “E_ALL & ~E_NOTICE”

 

; Record errors in the error log

php_flag log_errors On 

 

; An uploaded file can’t be more than 2 megabytes

php_value upload_max_filesize 2M

 

; Sessions expire after 1440 seconds

php_value session.gc_maxlifetime 1440

The php_flag and php_value words in Example A-4 tell Apache that the rest of the line is a PHP configuration directive. After php_flag, put the name of the configura­tion directive and then On or Off. After php_value, put the name of the directive and then its value. If the value has spaces in it (such as E_ALL & ~E_NOTICE), you must put it in quotes. There is no equals sign between the name of the configuration direc­tive and the value.

To change a configuration directive from within a PHP program, use the ini_set() function. Example A-5 sets error_reporting from within a PHP program.

Example A-5. Changing a configuration directive with ini_set()

ini_set(‘error_reporting’,E_ALL & ~E_NOTICE);

The first argument to ini_set() is the name of the configuration directive to set. The second argument is the value to which you want to set the configuration directive. For error_reporting, that value is the same logical expression as youd put in php.ini. For configuration directives whose values are strings or integers, pass the string or integer to ini_set(). For configuration directives whose values are On or Off, pass 1 (for On) or 0 (for Off) to ini_set().

To find the value of a configuration directive from within a program, use ini_get( ). Pass it the name of the configuration directive, and it returns the value. This is useful for adding a directory to the include_path, as shown in Example A-6.

Example A-6. Changing include_path with ini_get() and ini_set()

// These lines add /home/ireneo/php to the end of the include_path

$include_path = ini_get(‘include_path’);

ini_set(‘include_path’,$include_path . ‘:/home/ireneo/php’);

As mentioned earlier, not all configuration directives can be set in all places. There are some configuration directives that cannot be set from within your PHP programs. These are directives that the PHP engine must know about before it starts reading your program, such as output_buffering. The output_buffering directive makes a change to the engine’s behavior that must be active before the engine gets a look at your program, so you can’t set output_buffering with ini_set(). In addition, some configuration directives are prohibited from being set in Apache .htaccess files and some from being set in the Apache httpd.conf file. All configuration directives can be set in the php.ini file.

The PHP Manual has a big list of all the configuration directives and the contexts in which they can be changed. Some useful configuration directives to know about are listed in Table A-1.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *