KB / Web Programming / CGI, PHP, and Databases / PHP
From DreamHost
PHP is a scripting language - similar to ASP - which allows you to make dynamic web pages easily. Usually it is used in conjunction with a MySQL database to make for nice data-driven, interactive content. DreamHost supports PHP4 on all plans.
There are two ways to run PHP at DreamHost.. as a regular CGI (the default configuration, highly recommended), or as an Apache Module (not recommended).
Please note, PHP does not work in WebDAV-enabled directories.
Parent Article
Sub-Articles
Turning off register_globals for a specific domain/directory
Some scripts require that the register_globals setting be turned off in order to work. By default, register_globals is enabled on all DreamHost-hosted domains UNLESS you have php5 on. For php5 register_globals is off and you can't enable it.
In order to turn this setting off, create a file named ".htaccess" in the directory you want this setting to affect. If you want it to affect an entire domain name, create the ".htaccess" file in the root directory for that domain (usually, this directory is named after the domain). Within the file, place the following line:
php_flag register_globals off
Note that for this to work, you will need to uncheck the Run PHP as CGI setting option for your domain name by editing it in the web panel, here:
https://panel.dreamhost.com/index.cgi?tree=domain.web
This setting could take a few hours to take effect.
Do you support PEAR with your PHP?
Yes!
PEAR is included in the default distribution of PHP as of 4.3.0, which we have (at least.. actually a more recent version) installed on all servers!
Just run the commandpear liston your server and you'll get a list of what PEAR modules you have installed.. here's an example output:
Installed packages: =================== Package Version State Archive_Tar 1.1 stable Console_Getopt 1.2 stable DB 1.6.0 stable HTTP 1.2.2 stable Mail 1.1.2 stable Net_SMTP 1.2.3 stable Net_Socket 1.0.1 stable PEAR 1.3.1dev devel XML_Parser 1.0.1 stable XML_RPC 1.0.4 stableMost people mean the DB package when they speak of PEAR -- and it's there. Of course, there are many other packages not bundled with the distribution, but you can always use
pear install --installroot=<dir> <package>to install into a directory that you then add to include_path.
What's the difference between PHP-CGI and PHP as an Apache module?
There are two ways you can choose to have your PHP executed at DreamHost. The default for new customers is now PHP-CGI. This is by far the preferred method. The benefits of running PHP-CGI are:- It is more secure. The PHP runs as your user rather than dhapache. That means you can put your database passwords in a file readable only by you and your php scripts can still access it!
- It is more flexible. Because of security concerns when running PHP as an Apache module (which means it runs as our dhapache user), we have disabled a number of commands with the non-CGI PHP. This will cause installation problems with certain popular PHP scripts (such as Gallery) if you choose to run PHP not as a CGI!
- It's just as fast as running PHP as an Apache module, and we include more default libraries.
- Custom 404 pages won't work for .php files with PHP-CGI. Or will they? See n74's comment below!
- Variables in the URL which are not regular ?foo=bar variables won't work without using mod_rewrite (example.com/blah.php/username/info/variable).
- Custom php directives in .htaccess files (php_include_dir /home/user;/home/user/example_dir) won't work.
- The $_SERVER['SCRIPT_NAME'] variable will return the php.cgi binary rather than the name of your script
- Persistant database connections will not work. PHP's mysql_pconnect() function will just open a new connection because it can't find a persistant one.
How do I switch my PHP between running as a CGI and an Apache module?
Of course you've already read all about the differences in PHP-CGI and PHP as an Apache module, right? And you know that the default set up is as PHP-CGI, which is heavily preferred, right?
If you still want to change your PHP setup, here's how! Add the following to your .htaccess file:
AddHandler application/x-httpd-php .php
Note that this configuration is considered 'unsupported' by DreamHost and it may not always work in the future.
How Do I Install Gallery 1.4?
These instructions require shell access. Steps :- Untar the archive into your desired location.
- From the shell, inside the gallery dir run the command sh ./configure.sh
- Point your browser to http://[yoursite.com]/[gallerydir]/setup
- During setup use the following data.
- Use ImageMagick
- Path to ImageMagick is /usr/bin
- From any path delete the .xxxxx part. (ie /home/.lotus/user becomes /home/user )
- Set any custom paths ( ie, thumbnail/albdir ).
- create the directories referenced in above step
- Configure the settings on the next two pages.
- Be sure your domain is running PHP-CGI and not PHP as an Apache module.
- Via shell, from galler dir run command sh ./secure.sh
Do you have curl installed in your php?
We sure do! It should just work with your php scripts, including php-cgi.
And here's the CURL Information:
libcurl 7.9.5 (OpenSSL 0.9.6c) (ipv6 enabled)
Compiling your own custom PHP
If you need to use PHP modules we do not support, you can (if you're lion-mettled) compile PHP in your home directory and tell PHP scripts to use this binary instead of the default system PHP binary.
http://php.net has good instructions for compiling, and once that's completed, you can make it work like this.
Let's assume that you installed PHP into /home/username/php/. The binary is /home/username/php/bin/php. Your domain is hosted from /home/username/domain.com.
Copy that binary into some location accessible from your domain (cgi-bin/ is a good idea). So:
cp /home/username/php/bin/php /home/username/domain.com/cgi-bin/php
For the sake of maximum ease, rename the PHP binary to end in .cgi:
mv /home/username/domain.com/cgi-bin/php /home/username/domain.com/cgi-bin/php.cgi
Now create an .htaccess file to redirect all PHP requests to this PHP binary intead of going through the default system PHP binary. Put this inside:
AddHandler custom-php .php .pcgi Action custom-php /cgi-bin/php.cgi
Note: You can use whatever extension(s) in the AddHandler line. Separate multiple entries with spaces.
Make sure that the Action line points to a useful URL. In the example above, it points to http://domain.com/cgi-bin/php.cgi. If your PHP binary is in a different location, use a different URL. And keep in mind that it's a URL (sans domain name), NOT a path for the filesystem. Don't forget that first slash!
Good luck!
PHP Security
If you are having trouble with PHP commands like backticks (``), system(), exec(), passthru(), and others that spawn external commands, or are having trouble with errors like:
open_basedir Restrictions in effect, file is in wrong directory
Then you're running PHP as an Apache module and you've written a script that doesn't quite conform to our security standards.
We implement strict security for PHP scripts run by Apache, because otherwise, none of our users would be able to sufficiently secure sensitive information (like Database passwords)!
If PHP is running as part of Apache, it runs as Apache's user and group. This differs from CGI scripts, which, because they run as a separate process outside Apache, run as the owner's user and group.
To protect your PHP scripts, we've disallowed all PHP functions that would let one user possibly open another user's script (and see sensitive stuff like passwords). Our restrictions work in two parts:
1: OPEN_BASEDIR RESTRICTIONS
open_basedir restrictions prevent any of PHP's file opening commands from working on any files outside of /home/youruser. Files BELOW that directory WILL WORK:
/home/youruser/phpstuff/some/more/dirs/file.info IS OK! PHP CAN OPEN IT!
If you need to open files in another user's home directory (assuming both are under you account, of course!) contact TS and they will happily make an exception for you.
NOTE: There have been some strange bugs that appeared with respect to open_basedir. Sometimes files that ARE in your home directory trigger an open_basedir error when they really shouln't. Notify support when this happens.
2: DISABLED EXECUTION FUNCTIONS
We have disabled the backticks operator (``), system(), exec(), passthru(), and dl() because all of these functions could be used to run naughty external processes as Apache's user.
If you need to use these functions, we have provided a second PHP system, PHP-CGI, which runs all your PHP scripts as if they were CGI's, so they run as your user and group. There are no restrictions on these scripts (not even open_basedir restrictions).
Here's how to switch to running PHP-CGI.
If you're using sessions, you'll have to delete your cookie and get a new one. Once the script is running as YOUR user, the session files Apache created in /tmp won't be readable (since your user can't read /tmp files created by Apache's user). If you delete your cookie and start a new session and all is well!
Contact support if you have any questions!
Do you have netPBM installed?
We sure do!
This is set up on all of our servers. This is in the usual place as all of our binaries: /usr/bin, so your scripts shouldn't have any trouble executing this.
Can I run a phpbb forum (message board) on my site?
You sure can!
Phpbb is free, open source software for running a forum. Phpbb runs on MySQL, so you can use any DreamHost plan to run your very own forum on your website using this exciting, highly-rated software!
In fact, we now offer super-easy one-click installation of phpBB via the "Goodies > One-Click Installs" area of our web panel!
For more information, go to the phpbb home site, at http://www.phpbb.com
Do you support this PHP module or extension?
We support a fair number of PHP modules. Here's a list of some of them:
- calendar
- ctype
- curl
- GD (Version 2, TTF, GIF, PNG, JPG, PNG, WBMP, XMB)
- mysql
- overload
- pcre
- xml
- zlib
As mentioned in / Web Programming / CGI, PHP & Databases / PHP | this article, you can compile PHP in your user space (if you're on a plan that includes shell / CGI access) and run it as a CGI. While we don't provide support for this if you have difficulties getting it to work (ie you need to have some degree of expertise with compiling software on a *nix system and familiarity with PHP), we do have several customers who have done this successfully.
Do you support PHP? PHP3? PHP4? PHP5?
Yes, yes, yes and yes! Support for PHP4 is a standard feature on all DreamHost accounts.. by default we are running 4.4.2 on all servers.
You can also turn PHP5 (as a CGI, not an apache module) on for any domain of yours from our web panel's "Domains > Manage Domains" area.
What extension should my php files have?
Our servers are configured to recognize files with the extensions .phtml, .php, .php3, .php4, or .pcgi as PHP. If your PHP files aren't working, it may be that you haven't named them with a proper extension.
If you want a php page to be your default page (i.e. when somebody goes to yoursite.com you want it to show them a php file), you must name it either index.phtml or index.php (and make sure you don't have a file named index.html in the same directory).
Also, you can use whatever extension(s) you want for PHP files by placing a .htaccess file in your web root directory with a line similar to the following:
For PHP4:
AddHandler php-cgi .html .htm
For PHP5:
AddHandler php5-cgi .html .htm
Where '.html .htm' are all the extensions you wish to use.

