Category Archives: Apache / .htaccess

How to install Apache HTTP Server service script

Recently, after some software and hardware changes, I was forced to rebuild a CentOS 6.5-based virtual machine.  Because the CentOS repository does not include Apache HTTP Server 2.4, and I’m not a big fan of third-party repositories, I decided to compile httpd from source.  When compiling the HTTP server from source, the system administrator is not provided with any service management script; thus, I began my search.

I found a simple service script in an archived version of an old Red Hat user guide, which made things a little easier.

# Startup script for the Apache Web Server
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server.  It is used to serve 
#	       HTML files and CGI.
# processname: httpd
# pidfile: /var/run/
# config: /etc/httpd/conf/httpd.conf

# Source function library.
. /etc/rc.d/init.d/functions

# See how we were called.
case "$1" in
echo -n "Starting httpd: "
daemon httpd -DSSL
touch /var/lock/subsys/httpd
echo -n "Shutting down httpd: "
killproc httpd
rm -f /var/lock/subsys/httpd
rm -f /var/run/
status httpd
$0 stop
$0 start
echo -n "Reloading httpd: "
killproc httpd -HUP
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1

exit 0

To create the service script file, follow these directions:

# Create httpd script
touch /etc/init.d/httpd

# Upload, or copy and paste the script contents
nano /etc/init.d/httpd

# Set desired permissions
chmod 0755 /etc/init.d/httpd

# Start httpd on boot, if desired
chkconfig --add httpd
chkconfig httpd on

Upon careful review, you’ll notice the service script assumes that the httpd binary is globally accessible.  Under my minimal CentOS installation, httpd was installed in /usr/local/apache2. While there are more than a few methods for accomplishing this goal, we’ll use a symbolic link.

# Create symbolic link to apachectl
ln -s /usr/local/apache2/bin/apachectl /usr/sbin/httpd

After that, I executed the script which caused a “bad interpreter” error.

[root@localhost]# /etc/init.d/httpd
-bash: ./httpd: /bin/sh^M: bad interpreter: No such file or directory

This error occurs when a file contains CRLF-style line endings (Windows) instead of the LF-style line endings required by Unix-based systems. Luckily, someone invented a utility that fixes this problem quickly and painlessly.

# Install dos2unix
yum install dos2unix

# Fix "bad interpreter" error
dos2unix /etc/init.d/httpd

After that, I could start, stop, and restart the httpd server without any problems. On the other hand, when I tried to retrieve the status directly from the httpd binary (apachectl), I encountered another problem. Luckily, this problem was also easily solvable.

httpd status
# Output: /usr/sbin/httpd: line 95: lynx: command not found

# Install Lynx browser
yum install lynx

And… that’s it!

Preparing an SSL certificate for use with AWS Elastic Load Balancer

Today I was tasked with re-keying our SSL certificate in preparation for the implementation of Amazon Web Service’s Elastic Load Balancer service.  In order to properly implement the load balancer into our existing architecture, it must be able to handle SSL connections.  We current employ GoDaddy’s Premium SSL Certificate, which gives you the familiar green bar in the address bar of your browser.  After copying and pasting the private key, certificate, and certificate chain data into the provided inputs, Amazon returned the following message: “Invalid private key.”

When I generate SSL certificate signing requests, I generally use the following command:

openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -out yourdomain.csr

Unfortunately, the resultant format is not compatible with AWS’s Elastic Load Balancer.  In order to counter the “Invalid private key” error, I issued the following commands:

openssl genrsa -des3 -out yourdomain.key 2048
openssl req -new -key yourdomain.key -out yourdomain.csr

After that, copy and paste the CSR contents into the CSR input provided by GoDaddy.  After doing this, I attempted to restart Apache HTTP Server, and noticed the following errors in /etc/httpd/logs/error_log.

[Mon Oct 07 17:55:24.779930 2013] [ssl:emerg] [pid 23786] AH02204: Init: Pass phrase incorrect for key of yourdomain:443
[Mon Oct 07 17:55:24.779981 2013] [ssl:emerg] [pid 23786] SSL Library Error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

[Mon Oct 07 17:55:24.780076 2013] [ssl:emerg] [pid 23786] SSL Library Error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
[Mon Oct 07 17:55:24.780093 2013] [ssl:emerg] [pid 23786] SSL Library Error: error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error (Type=PKCS8_PRIV_KEY_INFO)

Unfortunately, the new private key required a passphrase and Apache didn’t like that. While there are a number of approaches to resolve the issue between Apache and private key passphrases, I chose to remove it, both for the sake of simplicity, and because the Elastic Load Balancer will not accept a private key containing a passphrase. To remove the passphrase, use the following command:

openssl rsa -in yourdomain.key -out yourdomain.key.nopass

At this point, the HTTP server can be restarted, and the SSL certificate’s private key will work with Amazon Web Service’s Elastic Load Balancer.

Redirect from base URL using Apache HTTP Server

What I was trying to accomplish was quite simple—redirection from the base URL (e.g. to a specified path within the same domain.  Even with a lot of experience, one can easily fall prey to assumptions about proper Redirect usage.  Instead of working as expected, the code below caused the repeated concatenation of the destination URL term (“tools” in this example).

# The WRONG way to redirect from the base URL
Redirect / /tools
Redirect /

Traditionally, the Redirect directive is used as a simple method of redirection from one page to another. In the following example, the HTTP server will redirect the end user from old-index.html to new-index.html.

# Redirect from old-index.html to new-index.html
Redirect /old-index.html /new-index.html

If there is a way to use a Redirect directive to accomplish redirection from the base URL, I have not figured it out. To solve this problem, I used the rewrite engine, which works flawlessly.

RewriteEngine On
RewriteRule ^/$ [R=302,L]

Optimize PHP, Apache & MySQL for performance

Two MySQL tuner scripts

MySQLTuner (Perl) and MySQL Performance Tuning Primer (Shell) are two scripts that can help diagnose MySQL database performance issues, and provide recommendations on how to solve them.  While both scripts output similar information, the diagnosis can be slightly different with each script, so I like to use both.  Below are directions on how to use them, as well as a few screenshots.

# Download MySQLTuner and MySQL Performance Tuning Primer to ~/scripts.
# To avoid having to invoke Perl/SH each time, make both files executable.
chmod +x ~/scripts/
chmod +x ~/scripts/

# After that, give them a go!

Continue reading Optimize PHP, Apache & MySQL for performance

PHP 5.2.11 released, fixing 75+ bugs

Yet another PHP 5.2.xx release took place a few days ago, which brought us over 75 bug fixes, some security-related.  If you are running any PHP 5.2.xx series release, make sure you apply this update.  Full details on the PHP 5.2.11 release can be viewed at  In addition, WHM/cPanel implemented this version of PHP in EasyApache, which updates smoothly.  Now, if they would only get to implementing 5.3 already… =) Continue reading PHP 5.2.11 released, fixing 75+ bugs

Setting up virtual hosts with Apache on Windows XP

In this tutorial, we will discuss how to setup virtual hosts for your development environment (or production, if you wish) on Windows XP.  This will allow you to use URLs such as http://mywebsite/ instead of http://localhost/mywebsite/, which is much more realistic. Continue reading Setting up virtual hosts with Apache on Windows XP

Setting up tracd for multiple projects in Windows XP

I have a few ongoing projects that I wanted to use Trac for, but I only had tracd setup for one project. Yesterday, I spent a very long time working with my local development environment trying to get mod_python work with Apache 2.2.13, which led me to ultimately abandon the module and use the lightweight, standalone server for Trac, tracd. Continue reading Setting up tracd for multiple projects in Windows XP

WordPress Security: Primer & Advanced Techniques

WordPress is the most widely used open source blogging software in the world.  This didn’t happen overnight, and the developers over there have certainly earned their keep.  With any web application, especially a heavily used one, there are security risks and precautions we must take.  Without a security model and virtual barriers in place to protect us, it can be an all-out free-for-all for hackers. Continue reading WordPress Security: Primer & Advanced Techniques

How Zend Server CE breaks WordPress Permalinks and how to fix it

Recently, I switched to Zend Server CE from a manually installed AMP package (Apache/MySQL/PHP) to make use of Zend’s lightweight environment and easy install package. In doing this, I found that it broke the permalinks associated with WordPress.  Despite mod_rewrite being enabled, and the “Pretty Permalinks” code being in the .htaccess file, it still didn’t work.

404 Error

To fix this problem do the following:

  1. Open up httpd.conf.  (On Windows, the default location of this file is C:Program FilesZendApache2conf.)
  2. Find the line with <Directory />.
  3. Make sure +FollowSymLinks is present on the Options line.  (i.e. Options +FollowSymLinks)
  4. Change AllowOverride None to AllowOverride All.
  5. Restart Apache.

AllowOverride controls how .htaccess files are used.  When AllowOverride is set to None, users cannot override the configurations set in httpd.conf for particular directories.  While the .htaccess in this client’s WordPress directory was correct, Apache wasn’t allowing it to override the default settings as stated in httpd.conf.  It is definitely worth it to learn more about how to configure Apache so that you can have a secure and well-performing environment, especially if a lot of developers work on the same production server.

If you have similar experience, we would love to hear what you have to say.  Perhaps it will save someone else a minute or even a few hours.

25+ Must-Have Cheat Sheets for Web Developers

Who wants to go to Google or break out the library of programming books every single time you get stuck on something? Below is a compilation of extremely useful cheat sheets, which can be printed, laminated and placed nearby for easy reference while doing your web development and design. Everyone has moments where a function cannot be remembered, and that is where these cheat sheets can be lifesavers. Bookmark this page for an easy portal to all of the cheat sheets, or an individual page if you find it helpful. Please don’t hesitate to post any cheat sheets we neglected to include, for this is about sharing knowledge.

1. PHP

PHP Cheat Sheet Continue reading 25+ Must-Have Cheat Sheets for Web Developers