Customizing PHP5 On DreamHost
I recently signed up for a DreamHost account. I’d been self-hosting for years, and I set up Antiquiet on a friend’s server. However, I was about to need a lot more technical support than I felt comfortable asking of a friend who was doing me a favor, as I planned to launch this site, as well as Johnny’s site and Britney’s.
I used to use MediaTemple back in the day. I loved them, but they’re a bit pricey, and I had a bad experience where they shut my site down (and eventually lost it) while I struggled (and ultimately failed) to pay a large hosting bill.
So far, DreamHost has been amazing. I can park an unlimited number of domains, with plenty of bandwidth to go around. I can do 95% of everything I need to do, from setting up domains and subdomains, to creating and managing SVN repositories, all with the admin panel. Web hosting seems to have come a long way since 2004.
When I do need to open a support ticket, I always get a prompt response from a real person who speaks proper English and seems genuinely happy to help. When setting up a ticket, there’s a little pulldown menu for the nature of the situation, and I find it refreshingly applicable; choices are:
- Just a casual question, comment, idea, suggestion…
- I need some help but it’s not super time-sensitive.
- I can’t get things done until I hear back from you, please reply ASAP.
- Things are broken and I’d like them not to be!
- OMG! EXTREME CRITICAL EMERGENCY!! EVERYTHING’S BROKEN! People are DYING!
It also asks for your level of expertise in the area you’re seeking support with, which cuts down on the “did you try power cycling?” responses, or indecipherable jargon when you’re a nub and you need something in simple terms.
Anyway, the only complaint I had with the server setup was the PHP upload limit. I installed wordTube (which I plan to overhaul) on Johnny’s blog, but DreamHost’s global limit of 7MB on shared hosting would make it difficult for Johnny to add videos on a whim, without having to teach him enough about FTP access to be dangerous.
But while DreamHost support refuses to entertain requests to raise the limit, they pointed me to their extensive Wiki, which explained that since they already run PHP as a CGI process, there’s really no reason you couldn’t compile your own PHP installation in your own area of the shared server- configured however you’d like- and have your sites use that instead of DreamHost’s.
But I ran into problems, as on September 2nd, DreamHost upgraded their shared hosting servers to Debian Etch, just barely breaking all of the provided install scripts. Of course if it was a dedicated Debian box, you could just use apt to install PHP5, but a shared box means you have to do it the old fashioned way.
After a day or two of hacking around, I managed to piece together a single shell script that would install PHP 5.2.5 with all the standard modules, on DreamHost’s new Debian Etch system.
So if you want to set up your own PHP installation on DreamHost, to have full control over your php.ini file, here’s how:
1. Run the install script
Download this PHP5 install script. You need to edit line 14, and replace ‘iamskwerl.com’ with your domain where you want to install your own PHP5. If you want to have all your domains use the same installation, that’s super easy to do later, but we just need to pick one now to start with. It will actually install PHP to your home folder, but the script needs to know where to put the first php.ini file. So edit line 14 and hit save.
Now copy the script into your home directory (don’t put it in a subfolder). You can do that part with FTP, but you’ll need to do the rest from the command line via SSH. Once you’re in the shell, make sure you have execute permissions on the install script, and run it by typing:
./sh php5-singlescript.sh
It will take an hour or so, and will require you to hit y & enter a couple of times near the end. It won’t timeout on you if you don’t respond right away, so no need to sit there and stare at it, but don’t go on vacation and expect to come home to a completed PHP installation.
2. Make sure you’ve got the right php.cgi
When I installed PHP5 with the script above, my PHP scripts didn’t work at first. Apparently the script put the wrong php.cgi into my site’s cgi-bin directory. To fix this problem before you find it the hard way, type this in the shell:Â
cp ~/php5/bin/php-cgi ~/iamskwerl.com/cgi-bin/php.cgi
and hit enter. Be sure to replace ‘iamskwerl.com’ with your domain.
3. Activate via .htaccess
At this point, you have a(n assumably) working PHP installation ready and waiting for some action, but your site is still using DreamHost’s because it hasn’t been told otherwise yet.
The beauty of this solution is that you can use .htaccess files to have only certain directories use your custom PHP installation, depending on where you put them. Just remember that .htaccess files affect all files and folders in the same directory, as well as all subfolders- but they do not affect anything outside of the directory they’re in.
So if you want the whole site to use your custom PHP installation, just create an .htaccess file in your domain’s webroot and call it a day.
But for Johnny’s site, I decided that I only wanted the WordPress admin to use the custom installation with the raised upload limit, while letting the front end of the site continue to use DreamHost’s more conservative installation- to be on the safe side. So I added the .htaccess file to the wp-admin directory.
Here’s what the .htaccess file should look like. If an .htaccess file already exists, just add this to the top and keep the rest as-is:
Options +ExecCGI AddHandler php-cgi .php Action php-cgi /cgi-bin/php.cgi <FilesMatch "^php5?\.(ini|cgi)$"> Order Deny,Allow Deny from All Allow from env=REDIRECT_STATUS </FilesMatch>
4. Test your custom PHP installation
Create a phpinfo file in the site you’ve installed your custom PHP for. Just create a new file called phpinfo.php, with the following contained therein:
<?php phpinfo(); ?>
Access phpinfo.php with a web browser, and you should get a pretty blue and grey table full of cool looking variables and version information. If you get an internal server error (500), there’s probably a problem with your .htaccess file. Clear out what you added to go back to using DreamHost’s standard PHP installation, make sure you did step 2 right, and then make sure that the php.cgi script in your site’s cgi-bin directory has execute permissions before trying step 3 again, carefully.
If your phpinfo.php file tells you your php.ini path is /home/you/php5/etc/php5/yoursite.tld, you’ve successfully set up a custom PHP installation!
5. Do something with it!
You can now edit your php.ini file, which can be found in the path reported by phpinfo (and mentioned above). At the very least, you’ll probably want to up your maximum file upload size by looking for the following lines:
post_max_size = 2M
and
upload_max_filesize = 2M
and replacing 2M with 32M or something more reasonable. You can just save your php.ini file for the change to take effect; since PHP is running as CGI, no need to restart Apache.
6. Let other domains use the custom installation (optional)
So as you can see from my script & examples, I set up my custom PHP installation for iamskwerl.com, but I wanted Johnny’s site to use it too.
It’s super easy to share the installation across different domains. You need to create an alias of the first site’s cgi-bin directory in the webroots of whichever sites you want to share it. For each additional site, run the following command from the shell:
ln -s /home/USERNAME/DOMAIN1.TLD/cgi-bin /home/USERNAME/DOMAIN2.TLD/cgi-bin
Replace USERNAME with your username, DOMAIN1.TLD with the domain name currently using the custom PHP installation, and DOMAIN2.TLD with a domain that you’d like to share it.
Then all you need to do is add .htaccess files for the new site, as described in step 3 above.
October 21st, 2008 at 8:04 pm
Hi there,
thanks for the script
but i noticed that two lines dont get to work as they should;
its seems that lines 36 and 37 which are
LIBXML2=”libxml2-2.6.27″
LIBXSLT=”libxslt-1.1.18″
are giving “files not found” error and so the script stops there.
So i went on the xmlsoft.org ftp site to check out by myself and it seems like libxml2-2.6.27
and
libxslt-1.1.18
are not on the server anymore.
I guess they simply updated the files to newer version.
so, if i aint wrong, the lines 36 and 37 could respectively rather be:
LIBXML2=”libxml2-2.7.2″
LIBXSLT=”libxslt-1.1.24″
which are the latest versions of the files we’re looking for.
But it is still just a guess, i havent even tried it!
i had the original libxml2-2.6.27 and libxslt-1.1.18 files copied on my own server somewhere so i used those instead.
Anyway, thanks for the good work!
October 23rd, 2008 at 10:49 am
ah, thanks!
December 16th, 2008 at 8:24 pm
Thanks for pointing that out Jean Michel, I ran into that error, hopefully now I should be able to get the rest of my installation working.
Thanks Skwerl for this tutorial and script! It’s a life saver after so many times I’ve been attempting (and obviously failing) to customize the PHP on my DreamHost site.
Also, maybe you should update the script to the changes jeanmichel pointed out.
Thanks,
Becca
December 16th, 2008 at 10:06 pm
Oh and I just wanted to let you know that instead of /home/USERNAME/ you can simply use $HOME, it’s much simpler
December 19th, 2008 at 6:44 am
isnt there really an easier way to get around the 7M upload limit? damn this really sucks
December 19th, 2008 at 11:01 pm
this came in really handy, to get the Zend Framework working on Dreamhost (just needed to add a stupid PHP path)
thanks!
btw, latest:
LIBXML2=”libxml2-2.7.2″
LIBXSLT=”libxslt-1.1.24″