Apache and mod_rewrite on Debian

I was recently trying to run a web application I cloned from GitHub, and it wasn’t working.  While I’m pretty familiar with Tomcat, this was the firs time I’d tried to use Apache, and I almost didn’t know where to begin.  Here’s what I learned.

(The details of this post apply to a Debian 7 setup.)

  • Apache was already installed.  I went into Synaptic and did a search.  It’s already there!  And it’s configured to run on startup, by default.  You can verify that the process is running with: ps -Aef | grep apache.  You can also visit http://localhost in a browser and expect to see a default “It works!” page served back to you.
  • By default, Apache configuration files reside in /etc/apache2/
  • By default, Apache serves up pages from /var/www/
  • mod_php was NOT installed.  This was obvious because my page wasn’t getting its includes, and when I viewed the page source in the browser, the PHP code was still listed in the page, unexecuted.  I was able to install the mod_php “meta” package through Synaptic pretty easily.
  • mod_rewrite is not enabled by default.  I figured this out because the particular app I was trying to configure had links that were pointing to pages that weren’t present in the directory listing.  When I’d click those links they’d return 404 errors.  Those pages just didn’t exist, so I figured something must be modifying the request or intercepting the routing (a la Angular or a similar SPA framework).  A little snooping in that directory listing revealed that the .htaccess file had quite a bit of code, and I realized that these instructions were rewriting the requests.  I’d heard of mod_rewrite before, so that’s where I started digging.
  • mod_rewrite is available in the default installation.  You’ll see rewrite.load if you list the files in /etc/apache2/mods-available/.  It’s not enabled though, so you’ll see that there’s no reference to it in /etc/apache2/mods-enabled/.
  • Nearly every online references I read told me to enable mod_rewrite with the command sudo a2enmod rewrite.  My installation had no clue what a2enmod meant, and told me “command not found.”  I believe a2enmod is a script that’s specific to Debian installations, but I didn’t/don’t have it, so I had to find another way.
  • I believe all that a2enmod does is create a symbolic link in /etc/apache2/mods-enabled/ that points to a load file in the /etc/apache2/mods-available/ directory, so I just needed to do that manually:  ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
  • Then, I needed to replace all occurrences of “AllowOverride None” with “AllowOverride All” within the /etc/apache2/sites-available/default file.  I’m not certain what this particular step accomplishes, but I read this step on Xmodulo, and it’s required (I tested it with and without this step, and out of curiosity, I also verified that the word “all” is not case-sensitive in that change.)
  • Finally, my configuration was complete, and I just needed to restart Apache:  sudo /etc/init.d/apache2 restart.

After that series of steps, everything worked.  Your mileage may vary, of course, so feel free to drop a comment and let me know.  Also, I welcome an education if you wish to clarify anything my writeup was shaky on.

Leave a Reply

Your email address will not be published. Required fields are marked *

*