Apache: Address already in use: make_sock: could not bind to address 0.0.0.0:80

Every now and then my Apache webserver becomes un-responsive, and attempting to restart it with apachectl graceful gives me the following error:

Address already in use: make_sock: could not bind to address 0.0.0.0:80

This error means that there is already a process running that is using port 80, so Apache is unable to start up and use it. To solve this problem, we need to figure out what process is currently using the port in question and kill it so that Apache can start. Use lsof to find out what is using our port:

shell> lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 23506 apache 3u IPv6 1206927465 TCP *:http (LISTEN)

You can see from the output of lsof that an httpd process already exists under PID 23506. This is simple enough to get rid of:

shell> kill -9 23506
shell> lsof -i :80
shell> apachectl start

I run lsof again after issuing the kill command to make sure that whatever was using port 80 is gone.

PHP & ionCube Loader: ‘The Loader must appear as the first entry in the php.ini file in Unknown on line 0’

I received the following in my Apache error_log when attempting to load the ionCube loader in my php.ini file:

PHP Fatal error: [ionCube Loader] The Loader must appear as the first entry in the php.ini file in Unknown on line 0

This is because ionCube must be loaded before any of the Zend extensions are. So if you have the Zend extension/optimizer loaded, your php.ini should look like this to get ionCube to work:

[Zend]
zend_extension=/usr/local/ioncube/ioncube_loader_lin_5.1.so
zen_extension_ts=/usr/local/ioncube/ioncube_loader_lin_5.1_ts.so
zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.0
zend_extension_manager.optimizer_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.0
zend_optimizer.version=3.3.0a
zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so
zend_extension_ts=/usr/local/Zend/lib/ZendExtensionManager_TS.so

—————-
Now playing: Tommy McCook & The Aggrovators – A Loving Melody
via FoxyTunes

mod_rewrite & PHP: How to match urlencoded plus sign (+ = %2B)

I ran into trouble when trying to pass a urlencode()‘ed plus sign into a web address being processed by mod_rewrite.

$url = 'http://www.server.com/browsealpha/name+has+plus+in+it/';
$url = urlencode($url); // http://www.server.com/browsealpha/name%2Bhas%2Bplus%2Bin%2Bit/

This $url variable gets echo()‘d as a link in a page, so once it’s clicked and loaded in the browser, I then needed mod_rewrite to translate that to the actual URL, which is:

http://www.server.com/browsealpha.php?name=name%2Bhas%2Bplus%2Bin%2Bit

Here is the RewriteRule I was using to match:

# match any name containing any combination of letters, numbers, and the % sign (to match urlencoded URLs)
RewriteRule ^browsealpha/([%\w]*)/?$ /browsealpha.php?name=$1 [QSA,L]

This rule should match http://www.server.com/browsealpha/name%2Bhas%2Bplus%2Bin%2Bit/ but for some reason it wouldn’t work. After hours of frustration, I found a few threads mentioning the need to urlencode() the string twice, like so:

urlencode(urlencode($variable));

IT WORKS!!! Apparently this is because mod_rewrite automatically decodes the urlencoded URL, so if you pass in %2B, PHP sees it as %2B0. If you double encode, mod_rewrite decodes the first one, and PHP receives the second one (which is now %2B, which is what we want).

—————-
Now playing: Autechre – 444
via FoxyTunes

Apache VirtualHost on Ubuntu

Here’s a quickie tutorial on how to add Virtual Hosts to Apache on Ubuntu. This tutorial assumes that you have a basic understanding of Apache configuration and that your Apache is installed and able to serve websites.

  1. cd /etc/apache2/sites-available
  2. sudo vim yourdomain.com.conf and enter your VirtualHost directive. Below I’ve put the most basic example, see Apache docs for details and additional features:
    <VirtualHost *>
      ServerName yourdomain.com
      DocumentRoot /home/youruser/public_html
    </VirtualHost>

    Save & exit.
  3. sudo vim /etc/hosts and add your new domain to the 127.0.0.1 localhost line so it looks like this:
    127.0.0.1 localhost yourdomain.com
    Save & exit.
  4. Enable your new virtualhost:
    sudo a2ensite yourdomain.com.conf
  5. Reload the Apache configuration:
    sudo /etc/init.d/apache2 reload

That’s it! Repeat for each of your VirtualHosts.

—————-
Now playing: Alèmayèhu Eshèté – Tchero Adari Nègn
via FoxyTunes

Serving up a Windows Media Video (WMV) with Apache.

When trying to serve up a Windows Media Video file from my Apache webserver, I ran into the problem of Firefox being unable to handle the file properly. It would simply load it as a plain text file, resulting in a bunch of jibberish representing the machine code of the video (as if you had opened the video with Notepad). It turns out that with a stock install, Apache does not know how to serve this type of file, so it serves it as the default, which is plain text. It’s an easy fix, you simply have to tell Apache how to serve these types of files. Open up /etc/mime.types and add the following line:
video/x-ms-wmv wmv
You will also need to add the following to your httpd.conf file:
AddType video/x-ms-wmv .wmv
Save and exit, restart Apache, and your browser should now prompt you for how you want to handle the file (Open/Save).