Web Developer / Blog

March
3rd, 2009

How to host the php.net manual on your laptop for offline use

Digg this article · Save to del.icio.us · Stumble it!

In a previous article I wrote about how to develop web applications locally on your laptop. In addition to being faster to develop, it lets you work without needing to be connected to the Internet. But what about the tools you use while developing? If you’re a PHP developer then the manual at php.net is an invaluable tool. It only make sense to have it available for when you’re not online.

The general concept
The best way to get the full functionality of the php.net website is to create a mirror of it. This will give you the manual as well as additional functionality. The additional functionality isn’t as useful and you can remove any unwanted parts as you wish. We’ll be following the official mirroring documentation from the php.net website. This is for people that want to provide a mirror, but it turns out that it’s just as useful if you want to run a local copy of the site.

Setting up the rsync
The rsync command is fairly straightforward but we’ll be doing some slight modifications to it. We’ll create a shell script so that it’s easy to update the files as needed.

user# echo '#!/bin/sh

rsync -avzC --timeout=600 --delete --delete-after \
      --include="manual/en/" --include="manual/en/**" --exclude="manual/**" --exclude="distributions/manual/**" \
      --exclude="distributions/**" \
      rsync.php.net::phpweb /path/to/document/root' > php-net-mirror.sh
user# chmod u+x php-net-mirror.sh
user# sudo su
root# echo 'php' >> /etc/hosts
root# exit
user# ./php-net-mirror.sh

You’ll notice that we added an exclusion list for distributions/**. This is so you won’t be sitting and waiting for 10+ distributions of PHP source to download to your laptop. You’ll also need to replace /path/to/document/root to any location that you have write permission to. We’ll later use this in our Apache vhost definition. I put it something inside of my home directory in a subdirectory named php.net. Don’t forget to set the permissions on the shell script so that you can execute it.

NOTE: If you’re using windows then you can use cwRsync available on sourceforge. Thanks to Ronnie and rui for the tips in the comments.

In order to access it from your browser you’ll need to decide what hostname to use. I chose php so that I can quickly browse to http://php/mysql_connect to view the documentation for that function. You can choose whatever you want, just substitute that when echo’ing into the hosts file.

The final step is to run the script. You’ll see the full list of files as they download. Updates should be much faster since you’ll only be pulling down files which have been modified.

Setting up Apache
Create a new Apache virtual host using the following definition.

<VirtualHost *>
     <Directory "/path/to/document/root">
          # Do not display directory listings if index is not present,
          # and do not try to match filenames if extension is omitted
          Options -Indexes -MultiViews
     </Directory>

     ServerName php
     ServerAdmin yourname@example.com

     # Webroot of PHP mirror site
     DocumentRoot /path/to/document/root

     # Log server activity
     ErrorLog logs/error_log
     TransferLog logs/access_log

     # Set directory index
     DirectoryIndex index.php index.html

     # Handle errors with local error handler script
     ErrorDocument 401 /error.php
     ErrorDocument 403 /error.php
     ErrorDocument 404 /error.php

     # Add types not specified by Apache by default
     AddType application/octet-stream .chm .bz2 .tgz .msi
     AddType application/x-pilot .prc .pdb 

     # Set mirror's preferred language here
     SetEnv MIRROR_LANGUAGE "en"

     # Turn spelling support off (which would break URL shortcuts)
     <IfModule mod_speling.c>
       CheckSpelling Off
     </IfModule>
</VirtualHost>

You’ll need to update the path to your document root on lines 2 and 12. This should match the path you specified in the shell script above. Line 8 also needs to be updated with the hostname you specified earlier to access the site with.

Fire up the site
That’s all! Execute the command you use to restart Apache. If you’re using OSX with MacPorts then you can execute the following commands.

user# sudo /opt/local/apache2/bin/apachectl restart
user# open -a Safari "http://php/mysql_connect"

Viola!

An alternative for Mac users
Aziz noted in the comments that there’s a downloadable program PHPfi for Mac OSX users that looks very slick. You can download it from http://www.artissoftware.com/phpfi/index.html.

29 Responses to “How to host the php.net manual on your laptop for offline use”

  1. Duodraco Says:

    Hi Jaisen… This tip is very useful… I`m syncing now php docs…
    Can I publish your article in portuguese at my website?

  2. jaisen Says:

    @duodraco, of course. If you don’t mind linking back, that’d be great – but not required :).

  3. Dan Previte Says:

    Or you can grab the CHM from the php.net site if you’re running Windows.

    http://us3.php.net/docs-echm.php

  4. Duodraco Says:

    Dan, last time that I grabbed CHM it was so old… I don’t know if it remains … but rsync method takes always updated information.

    Jaisen: can you talk about .htaccess file for http://php/mysql_connect like searches? I did it, but i`m without my notebook today, so without file.

    Sorry by mistakes in my english…

  5. Sher Says:

    Jaisen,

    Amazingly well-written tutorial. Much appreciated.

    You can often tell the grasp someone has of their subject matter when they are able to write so effectively.

    And congratulations on your new addition to the family!!!

  6. Philip Olson Says:

    Also it’s not too difficult to build the manual then view said build in a similar way. See also:

    http://wiki.php.net/doc/phd/view

    And the build system (affectionally known as phd) also allows building plain old html, chm, and pdf too.

  7. rui Says:

    Thank you very much for the tutorial. After some hair pulling (very new to Apache, vhosts and overall programming beginner), I finally have a working mirror of the php.net documentation on my laptop.

    Question, would you happen to have instructions for mirroring the Apache httpd documentation, too? I don’t really want to download the whole 28GB for the Apache website specially now that my ISP has implemented bandwidth caps, not to mention my laptop has barely any free space left. ^_^;

  8. jaisen Says:

    @rui, you can follow a similar sequence of steps by following the docs for setting up an apache mirror here: http://www.apache.org/info/how-to-mirror.html

  9. rui Says:

    Thanks. I read that page but was hoping to avoid the large download:

    “At least 40 GB of available disk space. The current distribution directory is around 28 GB, but we wish to leave room for considerable expansion.”

  10. jaisen Says:

    @rui, that’s a bit big :). It’s entirely possible that you could exclude different languages and whatnot. However, I still expect it to be a big download.

  11. rui Says:

    Yep, it’s really big. I think the problem with the instructions is it only gives you the rsync command-line to mirror the entire website. It doesn’t mention how you can download specific projects. For example, I’m only interested in getting a working mirror for the HTTP Server (httpd.apache.org), Perl (perl.apache.org) and XML (xml.apache.org) projects.

    Guess I’ll try downloading on my desktop and trim it down before copying to the laptop. Just have to be really careful I don’t go over the 100GB download limit set by Charter. :(

  12. Ronnie Says:

    There is an Rsync for Windows called cwRsync at SourceForge. How well does that work? I would imagine the syntax for the shell script is the same, or at least easily adaptable.

  13. James Says:

    That’s a nice article – the limit with the CHMs they provide is that they go out of date quickly and you have to remember to update them. Having php.net rsync seems a pretty cool idea heh!

  14. rui Says:

    @Ronnie:
    That’s what I used for when following this tutorial. It worked pretty well and I now have a working mirror of the PHP manual on my laptop. Searchable, too. :)

  15. jaisen Says:

    @rui, @Ronnie,

    Thanks for the tip. I updated the post with a link to cwRsync. I have to say, after using it for a week I’m impressed at how well it works. Better than I suspected. Kudos to the PHP team!

  16. Ronnie Says:

    Well I am going to have to follow in Rui’s footsteps and fire that puppy up. I am using Html-kit with keyword help that opens up the online Php manual, but sometimes the lag is unbearable (I want the answer now).

    And jaisen, my name is not Lonnie. ;-)

  17. jaisen Says:

    @Ronnie, doh! Fixed. The L is nowhere close to the R on the keyboard.

  18. Mahmud Ahsan Says:

    awesome!

  19. Tareq Says:

    How to do it in windows?

  20. Tareq Says:

    oh, i got it. didn’t marked the line for windows :D

  21. Malaysia Laptop Says:

    Are you using feedburner as your feed? I can’t seems to get it working on my feed reader.

  22. uniflare Says:

    For the love of god someone please explain how to do this on windows..

    Tried everythiung i can think of and now cwrsync is going through every registry entry on my pc.. wth is going on :(.

  23. uniflare Says:

    Right. Figured, i hope..

    My rsyncd.conf:
    use chroot = false
    strict modes = false
    hosts allow = *
    log file = rsyncd.log

    # Module definitions
    # Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
    #
    [phpweb]
    path = /cygdrive/c/phpman
    comment = Rsync PHP Manual
    read only = false

    My Batch File:
    rsync -avzC –timeout=600 –delete –delete-after –include=”manual/en/” –include=”manual/en/**” –exclude=”manual/**” –exclude=”distributions/manual/**” –exclude=”distributions/**” rsync.php.net::phpweb /cygdrive/c/phpman/’

    (All of my batch file contents are on a single line).

    Hopefully this will save the hour it took me to figure it out :P.

  24. raq Says:

    can somebody give me a tip how to make it work under windows os ?
    i have cwRsync
    but what else ?
    should i launch rsync.exe via cmd with parameters like given above ? cause im always getting errors :/
    should i modify some files also ? *.cmd file ?

  25. jaisen Says:

    Unfortunately, I can’t really help much on Windows. The best bet will probably be to use cygwin. Here’s an article I found via Google.

    http://www.trueblade.com/knowledge/using-rsync-and-cygwin-to-sync-files-from-a-linux-server-to-a-windows-notebook-pc

  26. raq Says:

    damn, but it is going aroung quite much :/ as i see above, people solved this on Win OS, but i have no idea how :)
    im always getting some problems, cause im dont even know if im doing it correct …
    nevertheless, im forced to see link you’ve provided, thanks

  27. Shiv Deepak Says:

    nice article but i lack rsync access on my college network :(

    @Dan Previte
    Thanks for the chm link.. it is very latest :)

  28. Aziz Light Says:

    I’m commenting a little bit late, but better late than never, right?

    If you’re on a Mac, there is a little app called PHPfi, or PHP Function Index and it’s free for “Academic & Hobbyist Users”.

    It’s basically a GUI frontend for the multiple xhtml files version of the php manual that you can find in the downloads section of php.net.

    The cool thing is that it also indexes user comments and shows them in a drawer, AND it has a Textmate command that lets you search the current word in PHPfi from within the editor.

    Here’s the link: http://www.artissoftware.com/phpfi/index.html

  29. jaisen Says:

    @Aziz, This looks like a very cool program. I added it to the end of the blog post.

Leave a Reply


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a Web developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.