Protecting Downloads From Hotlinking – The Soft Way

December 20th, 2011

The Story

Once upon a time, on a Web site 23 hops away from my home PC, there was a free software download that required registration. An email with the download page URL was sent to the visitor after registration. The download page contained usage instructions and a direct, static URL of the download in the form http://host/download/file.

Someone had registered and published the latter URL in a public directory, so people started to download the file directly, without seeing even the download page, let alone the registration form.

The URL of the download was changed, but the story repeated itself in a couple of weeks.
Read the rest of this entry »

Backporting PHP 5.3.5 from Ubuntu 11.04

May 15th, 2011

This is just a quick notice that the instructions for backporting PHP 5.3.3 from Ubuntu 10.10 to 10.04 LTS also seem to be working for PHP 5.3.5, found in just released Ubuntu 11.04.

Backporting PHP 5.3.3 from Ubuntu 10.10 to 10.04 LTS

Comments Off

April 20th, 2011

Update 15-May-2011: The below instructions seem to be working for backporting PHP 5.3.5 from Natty as well. I have not tested it much myself, but at least it builds and can run a test copy of this blog on my local Lucid VM. Therefore I have added a dash of jQuery magic to this post so as to avoid content duplication.

Use one of the links below to jump straight to the meat:

Why Backport?

Of all the Linux flavors out there, Ubuntu is least unknown to me, so it came as a natural choice when I was selecting an operating system for the VPS that hosts this Web site. I still had to choose between the latest Ubuntu 10.10 (code named Maverick Meerkat) and Ubuntu 10.04 LTS (Lucid Lynx). LTS stands for Long-Term Support, and LTS releases of Ubuntu Server are supported for five years, as opposed to 18 months for “normal” releases. This means security updates for 10.10 will stop next April, whereas 10.04 will be serviced till April 2015. Natural choice again?

Yes and no. The problem is that I really wanted to use PHP-FPM (FastCGI Processs Manager). PHP-FPM is bundled with PHP since version 5.3.3, which is the version included in Ubuntu 10.10, whereas Ubuntu 10.04 LTS comes with PHP 5.3.2 and hence no PHP-FPM.

I have evaluated the following options for getting PHP-FPM to work on Lucid:

  • Retrieve the latest PHP sources right from php.net and build them on a reference Ubuntu 10.04 system. I built version 5.3.4 and it seemed to work, but the perspective of studying patches made by the Debian team has really put me off. I also wanted to install and update everything using APT tools.
  • Retrieve PHP 5.3.2 sources from the Ubuntu 10.04 repository, get PHP-FPM from SVN and try to build the regular packages. Again, I would have had to study how to add the PHP-FPM package to the build and so on.
  • Use unofficial PHP-FPM packages for Ubuntu 10.04. On launchpad.net, there are at least two PHP 5.3.3 backports from Ubuntu 10.10 ([backport 1], [backport 2]), plus a build of PHP 5.3.2 with PHP-FPM. But they are unofficial, so who knows what is in there, and are not updated regularly.
  • Backport PHP 5.3.3 from Ubuntu 10.10 myself. It turned out to be very simple to accomplish with the help of apt-src, so I have eventually stuck to this method. (Credit goes to someone known as lykwydchykyn on Ubuntu Forums.)

And here is

How You Too Can Backport PHP 5.3.3 from Maverick to Lucid

  1. Get yourself a clean Ubuntu 10.04 LTS Server system, e.g. by cloning a baseline VM. That will be your build system.
  2. Update the build system:

    sudo aptitude update
    sudo aptitude safe-upgrade 
    
  3. If you do not have MySQL installed on the build system, do yourself a small favor and install it now:

    sudo apt-get install mysql-server

    (you may leave the MySQL root user password blank when prompted.)

  4. Install apt-src:

    sudo apt-get install apt-src
    
  5. Create a new file, say, maverick.list in /etc/apt/sources.list.d:

    sudoedit /etc/apt/sources.list.d/maverick.list
    

    and paste the following lines into it, substituting your preferred Ubuntu 10.10 mirror URLs if needed:

    deb-src http://us.archive.ubuntu.com/ubuntu maverick main
    deb-src http://us.archive.ubuntu.com/ubuntu maverick-updates main
    deb-src http://security.ubuntu.com/ubuntu maverick-security main

    This will add the respective Ubuntu 10.10 source repository.

    Note: It does not matter how you call the file, but it must end with “.list“, otherwise APT tools will ignore it silently.

  6. Update APT cache:

    sudo apt-src update | tee apt-src-update.log

    Expected output (watch for “maverick” lines):

       .  .  .
    Get:1 http://us.archive.ubuntu.com maverick Release.gpg [198B]
    Get:2 http://us.archive.ubuntu.com maverick-updates Release.gpg [198B]
       .  .  .
    Get:9 http://security.ubuntu.com maverick-security/main Sources [39.8kB]
       .  .  .
  7. Create a working directory, e.g. in your own home directory, and change to it:
    mkdir ~/php-5.3.3-backport
    cd !$
    
  8. Fetch PHP 5.3.3 sources and all builld dependencies:

    apt-src install php5 | tee apt-src-install.log
    

    Notes:

    1. There is no need to use sudo since the sources will be placed in your own directory. However, the numerous build dependencies: tools, libraries, utilities, etc., must be installed system-wide, so apt-src may ask you to enter your sudo password.
    2. apt-src will complain that it cannot check signatures on the source packages. Apparently this happens because you do not have Ubuntu 10.10 public keys on your build system, so this warning can be safely ignored.
    3. apt-src will first say it needs to retrieve 14MB of archives, then calculate dependencies and say it needs a bit more - up to 189 more packages on a baseline Ubuntu installation. Unless you mirror Ubuntu repositories locally or have a very fast Internet connection, you may be tempted to leave for a coffee at this point. However, if you have not followed the above advice, apt-src will in particular install MySQL, prompting for a root user password three times, so you better stay in this case.
  9. Do the build:

    apt-src build php5 | tee apt-src-build.log

    At this point, it is really important to NOT use sudo, otherwise MySQL will fail to start during testing.

    Time for lunch!

    Expected output when you get back:

    Over 40000 lines of informational messages and warnings
    I: Successfully built in current-directory/php5-5.3.3

    And if you see this, there will indeed be 27 fresh .deb files in the working directory.

See my next post for the instructions on setting up a local repository on your Ubuntu/Debian server so as to install the desired PHP 5.3.3 packages using conventional APT tools.

If you think you could simply copy those packages to the production system and run

sudo dpkg -i deb-file-1 deb-file-2 ...

I urge you to not do that, as dpkg does not care about dependencies, which must be installed from an Ubuntu 10.04 repository.

Apache 2.2 cannot load php5apache2_2.dll – Problem Solved

November 8th, 2009

It was a pleasant surprise for me to learn that File Information functions are enabled by default as of PHP 5.3.0. No need to figure out PECL and PEAR, set up the environment to compile from source, or beg for a Windows binary in PHP developer forums, thought I. Upgrading from 5.2.6 would be as easy as making the respective lines in Apache’s httpd.conf point to the 5.3.0 installation…

I was wrong. Apache won’t start and it took me more than half an hour to figure out what’s going on.

First, PHP for Windows comes in several binary incarnations. The official site suggests that “VC6″ PHP binaries must be used with apache.org Apache binaries, whereas e.g. this SitePoint article states that “VC9″ PHP binaries must be used with Apache binaries provided by ApacheLounge. As far as I could recall, I had the apache.org binaries, so I tried the VC6 build and Apache crashed on startup with access violation. Assuming a memory failure on my part, I tried the VC9 build but Apache still won’t start, this time with the following event appearing in the Event Log:

Event Type:	Error
Event Source:	Apache Service
Event Category:	None
Event ID:	3299
 .  .  .
Description:
The Apache service named  reported the following error:
>>> httpd.exe: Syntax error on line 490 of O:/apache-2.2/conf/httpd.conf: 
Cannot load O:/php-5.3.0/php5apache2_2.dll into server: The specified 
procedure could not be found.

So I searched for “Apache 2.2 cannot load php5apache2_2.dll” and spent the next twenty or so minutes reading various forums and blog posts, to no avail.

When nothing helps, read the manual. I have re-read the “Apache 2.0.x on Microsoft Windows” section in the PHP install.txt file and noticed:

Note: You should read the manual installation steps first!

This made me think: “Hmm. Perhaps I had done something when installing PHP 5.2.6?” I scrolled to “Manual Installation Steps” and you know what, it turned out to be the case!

The CGI and CLI binaries, and the web server modules all require the
php5ts.dll file to be available to them. 
   .  .  .
To make php5ts.dll available you have three options: copy
the file to the Windows system directory, copy the file to the web
server's directory, or add your PHP directory, C:\php to the PATH.

I blindly trust Windows Update to mess with my Windows system directory, but never manually copy files there, so the first option was ruled out. PHP was not in the PATH either, so looked into Apache bin/ directory and voila, php5ts.dll was right there.

However, replacing it with php5ts.dll from PHP 5.3.0 did not help. Fortunately, at this point I recalled switching to the VC9 build in an attempt to solve the access violation problem. Switched back to VC6 and Apache finally starts!

Lessons learned:

  • Manual installation now means manual uninstallation/upgrade in the future. Take notes during the process.
  • When you have to resort to reading the manual – read, don’t scan
|