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

Squirrelmail and Very Large (Big-Ass) Inboxes

We have an account with a VERY large inbox (over 80,000 emails), and when logging into Squirrelmail the main pane would not load. Instead we’d be prompted by the browser to download a right_main.php file. Accounts with smaller inboxes would load fine, so I figured it had something to do with system resources. This is an easy fix. Open up your php.ini file, and increase the memory_limit value to something larger than the default 8M. Restart Apache and try opening your big-ass inbox again. If it still doesn’t work, increase memory_limit again. Repeat until your inbox will load, but be careful if you have little RAM. I was able to load my 80,000+ inbox by setting memory_limit = 32M.

FCKeditor Error: this.DOMDocument has no properties

In my prior post I ran into a problem with FCKeditor on Apache having to do with serving JavaScript files as the proper MIME type. I ran into a similar problem with XML files when trying to load multiple instances of FCK textareas:
Error: this.DOMDocument has no properties
Source File: http://www.mysite.com/FCKeditor/editor/js/fckeditorcode_gecko.js
Line: 43

This is easily fixed in the same way, by telling Apache how to properly serve XML files. Put this in your httpd.conf or an .htaccess file:
AddType text/xml .xml

FCKeditor in Firefox – Illegal Character JavaScript error

For some reason, FCKeditor would not load in Firefox 2.0 even though it loaded in IE just fine. Turns out Apache was serving up the javascript files as the wrong FileType, so Firefox would choke on some jibberish characters:
Error: illegal character
Source File: http://www.myserver.com/FCKeditor/editor/js/fckeditorcode_gecko.js
Line: 1, Column: 1
Source Code: /*

The fix is easy enough, simply add an .htaccess file to your dir containing the following:
AddType application/x-javascript .js
Now FCKeditor should work fine in Firefox.