- 1 Overview
- 2 An example of PHP
- 3 Getting information about PHP
- 4 PHP on DreamHost
- 5 Advanced topics
- 6 See also
PHP, or PHP: Hypertext Preprocessor, is a general-purpose server-side scripting language. It can be embedded within, or used to manipulate, HTML pages.
An example of PHP
The following text that is saved with a file extension of .php, outputs "Hello, world!" when opened with a web browser:
<?php echo "Hello, world!"; ?>
Getting information about PHP
The language has a special function called phpinfo() that can retrieve information about a particular installation of itself, and the environment in which it has been installed.
This includes information about:
- PHP compilation options and extensions
- the PHP version
- server information and environment (if compiled as a module)
- the PHP environment
- OS version information, paths, master and local values of configuration options
- HTTP headers, and
- the PHP License.
Save the file with the following contents using a .php extension. Then, upload the file to a web server and view it in your web browser:
<?php phpinfo(); ?>
If you need help uploading the file, take a look at the FTP wiki.
PHP on DreamHost
General requirements for using PHP at DreamHost
DreamHost currently allows the following PHP configurations:
- User-installed PHP running as a CGI application
- PHP 5 running as a CGI or FastCGI application
- PHP 5 running as an Apache module (on DreamHost PS only)
DreamHost does not support mod_php on shared servers. For more information about mod_php, you can read more about on the DreamHost blog.
Changing the PHP version on your site
To change the PHP version on any of your domains/subdomains within the panel:
- Navigate to the (Panel > 'Domains' > 'Manage Domains') page.
- To the right of your domain, click the Edit button under the ‘Web Hosting’ column.
- The ‘Manage Domains’ page appears:
- In the 'Web Options' section, select which PHP version from the dropdown menu which you’d like your site to run.
- Scroll down and click the blue Change settings button to save your changes.
Forcing PHP for other file types
To process files with PHP whose names don't end with ".php", you can use a directive in .htaccess.
For example, create a file named myfile.test with the following code:
<?php echo ‘testing an extension using PHP’; ?>
The extension .test doesn’t exist. But, you can force it to run as PHP by adding this code to your .htaccess file:
AddHandler fcgid-script .test FCGIWrapper "/dh/cgi-system/php54.cgi" .test
If you load the file in a browser, it will now load as a normal PHP file.
In the above example, files with the extension .test are using PHP 5.4 with FastCGI. If you'd like files with the .test extension to load under a different version of PHP, change the second line. For example, this example forces .test extensions to use PHP 5.6:
AddHandler fcgid-script .test FCGIWrapper "/dh/cgi-system/php56.cgi" .test
If you do not want to change the version of PHP but want to PHP to use CGI (instead of FastCGI), use this line in your .htaccess file:
AddHandler php5-cgi .test
If you want to use the default version of PHP (currently 5.3) and CGI, use the following:
AddHandler php-cgi .test
|Note:||If you use either the ‘php5-cgi’ or ‘php-cgi’ directive in a FastCGI environment you'll force all PHP code in the specified extensions to be run using CGI regardless of what option you choose in the panel.|
Using PHP from the Command line (PHP-CLI)
DreamHost offers command line interface versions of PHP that are useful for developing shell applications. There are quite a few differences between the CLI (Command Line Interface), SAPI (Server Application Programming Interface), and other SAPIs (such as PHP-CGI). It's worth mentioning that CLI and CGI are different SAPIs even though they share many of the same behaviors. To learn more about these differences, you can begin by reviewing the PHP Manual's chapter, Using PHP from the command line.
The paths for the default DreamHost PHP-CLI installed binaries are as follows:
- PHP 5.3 – /usr/local/php53/bin/php (PHP 5.3.x where available)
- PHP 5.4 – /usr/local/php54/bin/php (PHP 5.4.x where available)
- PHP 5.5 – /usr/local/php55/bin/php (PHP 5.5.x)
- PHP 5.6 – /usr/local/php56/bin/php (PHP 5.6.x)
For convenience, DreamHost maintains shortcuts for all PHP versions in the ‘/usr/local/bin/’ directory:
lrwxrwxrwx 1 root root 16 Feb 7 23:35 /usr/local/bin/php-5.3 -> ../php53/bin/php lrwxrwxrwx 1 root root 16 Feb 8 00:56 /usr/local/bin/php-5.4 -> ../php54/bin/php lrwxrwxrwx 1 root root 16 Feb 8 02:12 /usr/local/bin/php-5.5 -> ../php55/bin/php lrwxrwxrwx 1 root root 16 Feb 8 03:18 /usr/local/bin/php-5.6 -> ../php56/bin/php
‘/usr/local/bin’ is included in the shell PATH for users by default, so you can use the shortcuts instead of the full path to the binaries.
You can confirm the exact version numbers in use by executing the binary from within the shell using the "-v" option. For example, you can check the version of the PHP5.6 CLI binary as follows:
usernamer@server:~$ php-5.6 -v PHP 5.6.5 (cli) (built: Feb 8 2015 11:16:19) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
The default PHP CLI interface on Dreamhost is PHP5.4.
[horsetail]$ php -v PHP 5.4.37 (cli) (built: Feb 8 2015 08:55:06) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
Changing the default PHP CLI in shell
In some cases, it may not be practical to have to specify the path to a particular version of PHP for each CLI command. Instead, you may decide to make PHP5.6 the default binary within your user's shell environment.
To change the default version of PHP under your Shell user:
- Log into your user via SSH.
- Make sure you’re in the user’s directory.
- Run the following command to open your .bash_profile file.
- Add the following line to force PHP 5.6.
- Update that file by running the following command:
If you now run php -v you’ll see the new version being used. You can also use an alias instead.
This works the same as export PATH when added to your .bash_profile.
If you want these directives to be applied every time you log in, add either of the lines to your shell user’s profile. The current supported defaults are:
|Note:||If you’re using tcsh, you must modify the command before they can be added to the .tcshrc profile:
alias php /usr/local/php56/bin/php set path=(/usr/local/php56/bin $path)
You can also modify this accordingly using the path to any of the PHP-CLI binaries.
Changing the output buffer cache
If you are using PHP FastCGI, the PHP functions flush(), ob_flush(), and ob_implicit_flush() will not function as expected. By default, output is buffered at a higher level than PHP (specifically, by the Apache module mod_deflate which is similar in form/function to mod_gzip).
If you need unbuffered output, you must either use CGI (instead of FastCGI) or contact support to request that mod_deflate is disabled for your site.
- PHP Tutorials – Beginner PHP tutorials.
- PHP Documentation – All the information you could ever need.
- MySQL and PHP
- PHP Form Processing
- PHP Code Snippets
- PHP resources
- allow_url_fopen – Accessing files with external URLs.
- PHP Magic Quotes
- PHP FastCGI
- PHP.ini – Instructions for customizing PHP settings (e.g., upload_max_filesize).