Serving MathJax locally on OSX

I’m often in Cuba with no or very slow internet.

I have a local copy of MathJax that I use for reading and writing IPython notebooks.

Now I want to read Sphinx pages that use MathJax markup.


The page I wanted to serve is expecting this URL:

My task is to set up a redirect so that my own machine serves this URL.

Luckily OSX comes shipped with a version of the apache web server.

I worked off these two pages, both by Neil Gee:


  • Link local copy of MathJax into ~/Sites/;
  • Enable apache serving from my ~/Sites user directories;
  • Enable apache virtualhost for domain;
  • Redirect IP traffic for to localhost;
  • Restart apache.


Linking local copy of MathJax

I have a local copy of MathJax, stored in ~/stable_trees/mathjax. This directory contains MathJax.js.

OSX apache expects to serve user pages from ~/Sites. So, the linking step is:

mkdir -p Sites/
ln -s ~/stable_trees/mathjax Sites/

Enable apache serving from ~/Sites directories

Replace my_user with your login username in the following:

sudo bash
cd /etc/apache2/users
cat > \${MY_USERNAME}.conf << EOF
<Directory "/Users/\${MY_USERNAME}/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all

Now start apache:

sudo apachectl start

You now should be able to read http://localhost/~my_user/ in your browser (where you need to replace my_user in the URL with your login username).

Enable apache virtualhost for mathjax directory

(As root) edit /etc/apache2/httpd.conf.

Uncomment the line Include /private/etc/apache2/extra/httpd-vhosts.conf and save.

Edit /etc/apache2/extra/httpd-vhosts.conf.

Add the following at the end:

<VirtualHost *:80>
    DocumentRoot "/Users/my_user/Sites/"
    ErrorLog "/private/var/log/apache2/"
    CustomLog "/private/var/log/apache2/" common
    <Directory "/Users/my_user/Sites/">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all

where you replace my_user with your login username.

Note the FollowSymLinks option – we need this because the directory we want to serve is in fact a symbolic link.

Redirect IP traffic for to localhost

(As root), edit /etc/hosts and add the following line:

Restart apache

sudo apachectl restart

You should now be able to disconnect your internet connection but still get a directory listing in your browser for You should also be able to get rendered MathJax pages offline and at blazing speed.