Wednesday, November 18, 2015
Reduce TIME_WAIT socket connections
Some time in your life you’ll run across an Apache server that always has tons of TIME_WAIT connections just seeming to hang out. While these don’t take up as many resources as an ESTABLISHED connection, why keep them around so long? This short article will show you how to identify how many you have, and how to tell your server to reduce them, reuse and recycle them (see, recycling IS a good thing).
First, SSH into your server and become root.
Next, let’s see how many TIME_WAITs you have hanging out:
1
|
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n
|
You should see something like:
1
2
3
4
5
6
7
8
9
10
|
1 established)
1 Foreign
3 FIN_WAIT2
5 LAST_ACK
6 CLOSING
9 SYN_RECV
10 ESTABLISHED
22 FIN_WAIT1
26 LISTEN
466 TIME_WAIT
|
So – let’s get that number smaller.
See what your current values are in these files by catting them to the screen:
1
2
3
|
cat /proc/sys/net/ipv4/tcp_fin_timeout
cat /proc/sys/net/ipv4/tcp_tw_recycle
cat /proc/sys/net/ipv4/tcp_tw_reuse
|
If you have default settings, you’ll probably see values of 60, 0 and 0. Let’s change those values to 30, 1, 1.
1
2
3
|
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
|
Now, let’s make the change persistent by adding them to the sysctl.conf file. First however, let’s make sure there aren’t any entries in there yet for these settings.. cat the file and grep for the changes we’re about to make:
1
2
3
|
cat /etc/sysctl.conf |grep "net.ipv4.tcp_fin_timeout"
cat /etc/sysctl.conf |grep "net.ipv4.tcp_tw_recycle"
cat /etc/sysctl.conf |grep "net.ipv4.tcp_tw_reuse"
|
Make notes of what your settings are if you had any results..
Now, edit the /etc/sysctl.conf with your favorite editor and add these lines to the end of it (or edit the values you have in yours if they exist already):
1
2
3
4
5
6
|
# Decrease TIME_WAIT seconds
net.ipv4.tcp_fin_timeout = 30
# Recycle and Reuse TIME_WAIT sockets faster
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
|
Now, let’s rerun that command from before and see where your TIME_WAITs are at:
1
|
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n
|
You may need to wait at least a minute or so (depending on what your old values were) to see a change here.
Fixperms script for cPanel servers running suPHP or FastCGI
#! /bin/bash
#
# Date: Jan 26th 2012
# Author: Colin R.
# Revisions: Jacob "Boom Shadow" Tirey (boomshadow.net)
# Revisions: Will Ashworth (williamashworth.com || ashworthconsulting.com)
# Fixperms script for ServInt
#
# https://github.com/PeachFlame/cPanel-fixperms</code>
#
# Fixperms script for cPanel servers running suPHP or FastCGI.
# Written for ServInt.net
# Copyright (C) 2012 Colin R.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. http://www.gnu.org/licenses/
# Set verbose to null
verbose=""
#Print the help text
helptext () {
tput bold
tput setaf 2
echo "Fix perms script help:"
echo "Sets file/directory permissions to match suPHP and FastCGI schemes"
echo "USAGE: fixperms [options] -a account_name"
echo "-------"
echo "Options:"
echo "-h or --help: print this screen and exit"
echo "-v: verbose output"
echo "-all: run on all cPanel accounts"
echo "--account or -a: specify a cPanel account"
tput sgr0
exit 0
}
# Main workhorse, fix perms per account passed to it
fixperms () {
#Get account from what is passed to the function
account=$1
#Check account against cPanel users file
if ! grep $account /var/cpanel/users/*
then
tput bold
tput setaf 1
echo "Invalid cPanel account"
tput sgr0
exit 0
fi
#Make sure account isn't blank
if [ -z $account ]
then
tput bold
tput setaf 1
echo "Need an account name!"
tput sgr0
helptext
#Else, start doing work
else
#Get the account's homedir
HOMEDIR=$(egrep "^${account}:" /etc/passwd | cut -d: -f6)
tput bold
tput setaf 4
echo "Fixing perms for $account:"
tput setaf 3
echo "------------------------"
tput setaf 4
echo "Fixing website files...."
tput sgr0
#Fix individual files in public_html
find $HOMEDIR/public_html -type d -exec chmod $verbose 755 {} \;
find $HOMEDIR/public_html -type f | xargs -d$'\n' -r chmod $verbose 644
find $HOMEDIR/public_html -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
chown $verbose -R $account:$account $HOMEDIR/public_html/*
find $HOMEDIR/* -name .htaccess -exec chown $verbose $account.$account {} \;
tput bold
tput setaf 4
echo "Fixing public_html...."
tput sgr0
#Fix perms of public_html itself
chown $verbose $account:nobody $HOMEDIR/public_html
chmod $verbose 750 $HOMEDIR/public_html
#Fix subdomains that lie outside of public_html
tput setaf 3
tput bold
echo "------------------------"
tput setaf 4
echo "Fixing any domains with a document root outside of public_html...."
for SUBDOMAIN in $(grep -i document /var/cpanel/userdata/$account/* | awk '{print $2}' | grep home | grep -v public_html)
do
tput bold
tput setaf 4
echo "Fixing sub/addon domain document root $SUBDOMAIN...."
tput sgr0
find $SUBDOMAIN -type d -exec chmod $verbose 755 {} \;
find $SUBDOMAIN -type f | xargs -d$'\n' -r chmod $verbose 644
find $SUBDOMAIN -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
chown $verbose -R $account:$account $SUBDOMAIN
find $SUBDOMAIN -name .htaccess -exec chown $verbose $account.$account {} \;
done
#Finished
tput bold
tput setaf 3
echo "Finished!"
echo "------------------------"
printf "\n\n"
tput sgr0
fi
return 0
}
#Parses all users through cPanel's users file
all () {
cd /var/cpanel/users
for user in *
do
fixperms $user
done
}
#Main function, switches options passed to it
case "$1" in
-h) helptext
;;
--help) helptext
;;
-v) verbose="-v"
case "$2" in
-all) all
;;
--account) fixperms "$3"
;;
-a) fixperms "$3"
;;
*) tput bold
tput setaf 1
echo "Invalid Option!"
helptext
;;
esac
;;
-all) all
;;
--account) fixperms "$2"
;;
-a) fixperms "$2"
;;
*)
tput bold
tput setaf 1
echo "Invalid Option!"
helptext
;;
esac
#
# Date: Jan 26th 2012
# Author: Colin R.
# Revisions: Jacob "Boom Shadow" Tirey (boomshadow.net)
# Revisions: Will Ashworth (williamashworth.com || ashworthconsulting.com)
# Fixperms script for ServInt
#
# https://github.com/PeachFlame/cPanel-fixperms</code>
#
# Fixperms script for cPanel servers running suPHP or FastCGI.
# Written for ServInt.net
# Copyright (C) 2012 Colin R.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. http://www.gnu.org/licenses/
# Set verbose to null
verbose=""
#Print the help text
helptext () {
tput bold
tput setaf 2
echo "Fix perms script help:"
echo "Sets file/directory permissions to match suPHP and FastCGI schemes"
echo "USAGE: fixperms [options] -a account_name"
echo "-------"
echo "Options:"
echo "-h or --help: print this screen and exit"
echo "-v: verbose output"
echo "-all: run on all cPanel accounts"
echo "--account or -a: specify a cPanel account"
tput sgr0
exit 0
}
# Main workhorse, fix perms per account passed to it
fixperms () {
#Get account from what is passed to the function
account=$1
#Check account against cPanel users file
if ! grep $account /var/cpanel/users/*
then
tput bold
tput setaf 1
echo "Invalid cPanel account"
tput sgr0
exit 0
fi
#Make sure account isn't blank
if [ -z $account ]
then
tput bold
tput setaf 1
echo "Need an account name!"
tput sgr0
helptext
#Else, start doing work
else
#Get the account's homedir
HOMEDIR=$(egrep "^${account}:" /etc/passwd | cut -d: -f6)
tput bold
tput setaf 4
echo "Fixing perms for $account:"
tput setaf 3
echo "------------------------"
tput setaf 4
echo "Fixing website files...."
tput sgr0
#Fix individual files in public_html
find $HOMEDIR/public_html -type d -exec chmod $verbose 755 {} \;
find $HOMEDIR/public_html -type f | xargs -d$'\n' -r chmod $verbose 644
find $HOMEDIR/public_html -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
chown $verbose -R $account:$account $HOMEDIR/public_html/*
find $HOMEDIR/* -name .htaccess -exec chown $verbose $account.$account {} \;
tput bold
tput setaf 4
echo "Fixing public_html...."
tput sgr0
#Fix perms of public_html itself
chown $verbose $account:nobody $HOMEDIR/public_html
chmod $verbose 750 $HOMEDIR/public_html
#Fix subdomains that lie outside of public_html
tput setaf 3
tput bold
echo "------------------------"
tput setaf 4
echo "Fixing any domains with a document root outside of public_html...."
for SUBDOMAIN in $(grep -i document /var/cpanel/userdata/$account/* | awk '{print $2}' | grep home | grep -v public_html)
do
tput bold
tput setaf 4
echo "Fixing sub/addon domain document root $SUBDOMAIN...."
tput sgr0
find $SUBDOMAIN -type d -exec chmod $verbose 755 {} \;
find $SUBDOMAIN -type f | xargs -d$'\n' -r chmod $verbose 644
find $SUBDOMAIN -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
chown $verbose -R $account:$account $SUBDOMAIN
find $SUBDOMAIN -name .htaccess -exec chown $verbose $account.$account {} \;
done
#Finished
tput bold
tput setaf 3
echo "Finished!"
echo "------------------------"
printf "\n\n"
tput sgr0
fi
return 0
}
#Parses all users through cPanel's users file
all () {
cd /var/cpanel/users
for user in *
do
fixperms $user
done
}
#Main function, switches options passed to it
case "$1" in
-h) helptext
;;
--help) helptext
;;
-v) verbose="-v"
case "$2" in
-all) all
;;
--account) fixperms "$3"
;;
-a) fixperms "$3"
;;
*) tput bold
tput setaf 1
echo "Invalid Option!"
helptext
;;
esac
;;
-all) all
;;
--account) fixperms "$2"
;;
-a) fixperms "$2"
;;
*)
tput bold
tput setaf 1
echo "Invalid Option!"
helptext
;;
esac
Source: <a href="https://github.com/PeachFlame/cPanel-fixperms" target="_blank">https://github.com/PeachFlame/cPanel-fixperms</a>
Fix EasyApache Error : localhost did not have any working
One of the reason for Cpanel EasyApache failed when the Cpanel update not going smoothly.
So the solution is to run the update in force mode then re build easy apache.
#/scripts/upcp --force
#/scripts/easyapache --build
Subscribe to:
Posts (Atom)