Category: Server Configuration

Bending servers to work with applications, and bending applications to work with servers

PHP-MSSQL on Linux: Easier Than You’ve Heard

We’ve integrated in-house Microsoft SQL Server databases with public PHP web sites a number of times, now. Until today, this meant dealing with Windows VPSes for even modest sites, simply because we were unable to find a Windows shared hosting plan which permitted access to remote databases, and all the research I could find suggested connecting to MS SQL from PHP on Linux was an absolute mess.

Those days appear to be over.

yum install php-mssql

That, my friends, is all we had to do, thanks to the atomic rocket turtle repository and our earlier efforts to modernize our server.

301 Redirects on IIS 6 with ISAPI_Rewrite

We just launched a new site for one of our clients, and one small improvement we made was replacing all the old and ugly (and not especially SEO-friendly) URLs like /cand_jobs.shtml with shiny new ones like /jobs. The catch here is that Google’s existing index refers to all the old URLs, and we risk serving up 404′s and losing Google juice if we don’t implement 301 redirects from all the old URLs to the new ones.

On Apache, the normal way to handle this is to add lines like this to your .htaccess:

redirect 301 /old.htm http://www.example.com/new

Unfortunately, in this case we aren’t using Apache–we’re using IIS 6. I could set up redirects from within IIS itself, but as far as I can tell that would require creating dummy files for each of our old URLs, and telling IIS to redirect each to their new URL, and I’m not interested in having a bunch of dummy files lying around waiting for someone to accidentally delete them. Instead, the smart way to approach the problem is via ISAPI_Rewrite, an IIS module which mimics some parts of Apache’s configuration, which we happen to already have installed.

Long story short, add this to your httpd.conf:

RewriteCond ^old\.htm$ new [R=301,L]

Why is such a trivial solution worthy of a blog post? Because it took me more than an hour trying different combinations of options to find one that would work. Why? Because ISAPI_Rewrite 2′s documentation has better Google juice than ISAPI_Rewrite 3′s, and I wasted my hour scouring the wrong one and following the advice of others running that version.

Troubleshooting WordPress Automatic Updates on IIS 6

WordPress automatic updates are awesome. When they work, they’re as easy as a click of a button. When they fail, however, you’re left either reconciling yourself to an eternity of manual updates, or troubleshooting your host–a process that routinely takes much longer than applying the update manually would’ve.

In this case, my VPS was running IIS 6 and the updates weren’t working. First, the update screen would merely say it was downloading the new version, and it would never make any progress. Then, when I extended PHP’s max_execution_time, I encountered a FastCGI internal error saying the process exceeded the configured activity timeout. When I corrected that, WordPress reported permissions errors while trying to move the new files into place. My full solution was as follows:

Backup Everything

Any time I’m updating anything; download a full & current copy of the site, and export all tables from the database.

Fix the FastCGI Timeout

Open \WINDOWS\system32\inetsrv\fcgiext.ini, and, in the section labelled PHP, add ActivityTimeout = X, where X is the desired number of seconds after which to terminate an unresponsive script. Restart IIS.

Fix Permissions

Much like how apache is often configured to run as “nobody” in order to prevent malicious scripts from modifying files, IIS (by default) runs as a user named IUSR_[hostname], where [hostname] will be a string you set up when initially configuring your site. Unlike apache, however, on IIS, 777 permissions don’t necessarily mean any user is permitted to write to a given file or directory.

To fix the problem I had to open up my web root in IIS, and give my IUSR_[hostname] full permissions. I was then able to run the update, after which for security purposes I reduced that users permissions to the original level.

Install PHP5 on an Uncooperative VPS

A VPS is the standard upgrade when conventional shared hosting won’t meet your needs. The catch, and most hosts try not to make this clear, is that you’re then on the hook for all configuration, upgrades, and maintenance. And as an added bonus, many hosts offer default VPS installs configured as though they’re deliberately trying to make administration difficult.

For a recent project hosted on one of our VPSes, I was planning to use Kohana, which requires PHP5. Despite the fact that PHP5 was released in 2004, it wasn’t installed on the VPS in question. As the VPS was running Plesk, any changes to the PHP installation would have to be made carefully so as not to demolish our hosted sites, but research revealed the Atomic Rocket Turtle Repository, designed exactly for upgrading machines such as mine. Except that yum wasn’t even installed.

The procedure was as follows:

Install yum

Note: If you’re following along at home, instead of just copy pasting my terminal commands, you should probably make sure your rpm -Uvh calls are requesting contemporary versions

First, install a key from the CentOS mirrors so rpm will work:
rpm –import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-4

Then, try and fail to install yum in order to get a full list of missing dependencies:
rpm -Uvh http://mirror.centos.org/centos/4/os/i386/CentOS/RPMS/yum-2.4.3-3.el4.centos.noarch.rpm

Next, install each listed dependency with a call to rpm -Uvh

Finally, install yum by re-running the rpm command above

(based on instructions here)

Set up the Atomic Rocket Turtle repository

wget -q -O – http://www.atomicorp.com/installers/atomic.sh | sh

Upgrade PHP

yum upgrade php

Reinstall Ioncube Loader

(Note: As we weren’t using the Ioncube Loader, everything worked without this step. Except that we were getting notices in our logs saying it was missing.)

yum install php-ioncube-loader

Set up new (default) php.ini

mv /etc/php.ini /etc/php.ini.php4
mv /etc/php.ini.rpmnew /etc/php.ini

Restart Plesk

/etc/init.d/psa stopall
/etc/init.d/psa startall
/etc/init.d/psa restart

Miraculously, Plesk restarted successfully and all my applications were fully functional under the new install of PHP5.

I could’ve stopped here, but because the VPS had been running for a few years, and yum hadn’t been installed, I knew that basically everything (if not literally everything) on the server was out of date. Research and testing aside, the above took less than 10 minutes. The below (upgrading everything) took several hours of troubleshooting.

Upgrade everything server-wide

yum upgrade

(Note: This might take a while. I made sure to archive the output, because when anything goes wrong, the clues will show up here.)

Set up new MySQL configuration

(Note: At this point, Plesk wouldn’t restart because MySQL (just upgraded from 4 to 5) was totally broken.)

mv /etc/my.cnf /etc/my.cnf.old
mv /etc/my.cnf.rpmnew /etc/my.cnf

Install new Plesk package to support openssl upgrade

(Note: Now MySQL worked, but Plesk was still broken.)

If you update from an old vesion of openssl, you’re probably going to run into a bug, the quick solution to which you can find here.

Update MySQL databases

(Note: Plesk would restart, but I couldn’t log in.)

mysqlcheck –check-upgrade –all-databases –auto-repair -uadmin -pyouradminpassword