Tuesday, March 5, 2013

Configure Apache to listen multiple ports


The listen directive in the Apache configuration file can be used to make Apache to listen in a particular port or IP address or port combination.

Listen 80
Listen 8000

In this format the Apache server listens on the given ports on all interfaces (IP addresses) which are up in the server. If you mention the above two directives in the Apache configuration file then the Apache server will listen on both the ports 80 and 8000. Multiple Listen directives may be used to specify a number of addresses and ports to listen to.

Listen IP:80

You need to replace the IP address in this example with the IP address you want the Apache service to listen. In this format an IP address is given as well as a port. In this case the server will listen on the given port and interface( IP address ). If you want the Apache service to listen on two IP addresses in the server you can use multiple listen directive in the configuration file.

Listen IP1:80
Listen IP2:80

Please remember to restart the Apache service in the server once the changes are made to the configuration file.

Starting Apache HTTPD Failed Cannot Open or No such file mod_bwlimited, mod_log_bytes or mod_bandwidth


On restaring Apache, you may get the following error:

Nov 21 09:31:40 web httpd: Cannot load usr/local/apache/libexec/mod_log_bytes.so into server: /usr/local/apache/libexec/mod_log_bytes.so: cannot open shared object file: No such file or directory
Nov 21 09:31:40 web rc: Starting httpd: failed

Invalid command ‘BytesLog’, perhaps mis-spelled or defined by a module not included in the server configuration

This is most likely due to the cause that the mod_belimited.so, mod_log_bytes.so, or mod_bandwidth.so are deleted or corrupted. These 3 files are unique on cPanel based servers.

This error is received when the Apache HTTPD server has been messed up with some shared library files such as mod_auth_passthrough.so, mod_bwlimited.so and mod_log_bytes.so located in libexec directory, that have accidentally been deleted or corrupted.

The solution to the error is to recompile those shared library modules. You can easily compile these cPanel modules for Apache by using the following commands:

cd /usr/local/cpanel/apache

/usr/local/apache/bin/apxs -iac mod_log_bytes.c

/usr/local/apache/bin/apxs -iac mod_bwlimited.c

/usr/local/apache/bin/apxs -iac mod_bandwidth.c

After compilation, the .so files will automatically be copied to libexec directory for Apache HTTPD web server.

Maximum file limit reach while starting Apache



Maximum file limit reach error is occur while we are starting the Apache server at that time you can fix the issue by using root login details.

root@gunjan[~]#pico /etc/sysctl.conf
Add following line as

fs.file-max = 22992

Save and exit from the file.

You need to run following command from shell to active changes.

root@gunjan[~]#sysctl -p
It will increase the maximum number of open files for your Server.

Bash Script to Check Apache Memory Usage


We might have come across many situation where we need to find the memory usage by apache and which script or from which directory this process is being called. You can use the script given below to find that and log it whenever the load goes beyond a threshold value. Which also help us to refer later if we want to troubleshoot load issues on the server.

To setup the script follow the simple steps mentioned below.

1. Create a directory /var/log/process_memcheck/

2. Create a file inside /root. I gave the name process_memcheck.sh.

3. Grant executable permission to this script.

4. Open the file and paste the following contents in it.

=========

#!/bin/bash

VAL=10

#### I gave 10 as threshold value. You can use your own
LOAD=`cat /proc/loadavg | awk -F’ ‘ ‘{print int($1)}’`
HOST=`hostname`
IP=`hostname -i`
if [ "$LOAD" -ge "$VAL" ]
then
{
for i in $(netstat -plan | grep :80 | grep ESTABLISH | awk ‘{print $7}’ | cut -d/ -f1)
do
dir=$(lsof -p $i | grep cwd 2> /dev/null| awk ‘{print $9}’);
pid=$(lsof -p $i | grep cwd 2> /dev/null| awk ‘{print $2}’);
memuse=$(pmap -x $pid 2>/dev/null | grep total | sed ‘s/-//g’);
echo “Mem Use = $memuse and home dir = $dir”;
done
} > /var/log/process_memcheck/process_memcheck-$(date +%d-%m-%y:%H:%M).log
fi

==========

5. Save and quit.

6. Open the file /var/spool/cron/root and append the following line in it.

*/5 * * * * /root/process_memcheck.sh
7. Save and quit. Restart crond.

/etc/init.d/crond restart
Thats all. You will get a log at /var/log/process_memcheck/, which show the memory usage by each apache and the directory from which it is being called.

httpd is not working: Unable to open logs


root@server# /etc/init.d/httpd restart
Warning: DocumentRoot does not exist
httpd not running, trying to start
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs

Solution :–

root@server [~] netstat -lnp | grep ’0.0.0.0:80′

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10240/httpd

root@server[~] kill -9 10240

root@server[~] service httpd restart

thats all.

Apache restart failed. Unable to load pid from pid file and no httpd process found in process lis


If you are getting the message “Apache restart failed. Unable to load pid from pid file and no httpd process found in process list.” It may cause by the problem with mod_ssl.
Just check the log files using tail -f /usr/local/apache/logs/error_log

If you are getting ”File exists: Cannot create SSLMutex with file `/usr/local/apache/logs/ssl_mutex’Configuration Failed“Also check apache status :

[root@vps12105 ~]# service httpd status

Looking up localhostMaking HTTP connection to localhost

Alert!: Unable to connect to remote host.
lynx: Can’t access startfile http://localhost/whm-server-status

Resolution :

Rename the file /usr/local/apache/logs/ssl_mutex to /usr/local/apache/logs/ssl_mutex_bkand try restarting the apache.

Steps :

mv /usr/local/apache/logs/ssl_mutex /usr/local/apache/logs/ssl_mutex_bk

service httpd stop

service httpd startssl

service httpd status

Unable to restart named


Getting the following errors while trying to restart named in a cPanel server.

root@host [~]# service named restart
Stopping named: [FAILED]
Starting named: [FAILED]

root@host [~]# service named status
rndc: connection to remote host closed
This may indicate that the remote server is using an older version of
the command protocol, this host is not authorized to connect,
or the key is invalid.
So named restart failed. Next step is to find the exact reason and this can be find out in /var/log/messages. In my case I got the following error in /var/log/messages while restarting named.

May 5 01:44:51 host named[7460]: starting BIND 9.3.3rc2 -u named
May 5 01:44:51 host named[7460]: found 4 CPUs, using 4 worker threads
May 5 01:44:51 host named[7460]: loading configuration from '/etc/named.conf'
May 5 01:44:51 host named[7460]: no IPv6 interfaces found
May 5 01:44:51 host named[7460]: listening on IPv4 interface lo, 127.0.0.1#53
May 5 01:44:51 host named[7460]: listening on IPv4 interface venet0:0, 67.222.12.150#53
May 5 01:44:51 host named[7460]: listening on IPv4 interface venet0:1, 67.222.13.150#53
May 5 01:44:51 host named[7460]: /etc/rndc.key:1: configuring key 'rndc-key': bad base64 encoding
May 5 01:44:51 host named[7460]: loading configuration: bad base64 encoding
May 5 01:44:51 host named[7460]: exiting (due to fatal error)


This error is due to the mismatch of the secret key in /etc/rndc.conf and the include file /etc/rndc.key. Both the keys should be the same.

After copying the secret key from the file /etc/rndc.conf to /etc/rndc.key, named was restarted successfully.

root@host [~]# service named status
number of zones: 19
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/1000
tcp clients: 0/100
server is up and running