Install Setup Squid HTTP Web Proxy CentOS 6 Server

Install and Setup Squid as HTTP web proxy, squid caching and forwarding to speeding up a web server by caching repeated requests on CentOS 6 Server.

Install Squid from CentOS repositories, make sure sure CentOS 6 Server system is up-to-date

#yum update
#yum install squid

Check it Squid has been installed successfully

#ls -la /etc/squid
drwxr-xr-x  2 root  root   4096 Sep  4 00:15 .
drwxr-xr-x 53 root  root   4096 Sep  4 00:36 ..
-rw-r--r--  1 root  squid   419 Aug  4 07:56 cachemgr.conf
-rw-r--r--  1 root  root    419 Aug  4 07:56 cachemgr.conf.default
-rw-r--r--  1 root  root   1547 Aug  4 07:56 errorpage.css
-rw-r--r--  1 root  root   1547 Aug  4 07:56 errorpage.css.default
-rw-r--r--  1 root  root  11651 Aug  4 07:56 mime.conf
-rw-r--r--  1 root  root  11651 Aug  4 07:56 mime.conf.default
-rw-r--r--  1 root  root    421 Aug  4 07:56 msntauth.conf
-rw-r--r--  1 root  root    421 Aug  4 07:56 msntauth.conf.default
-rw-r-----  1 root  squid  3948 Sep  4 00:14 squid.conf
-rw-r--r--  1 root  root   2425 Aug  4 07:56 squid.conf.default

Configuring Squid as an HTTP web proxy.
Edit the Squid configuration file /etc/squid/squid.conf
This squid configuration set all IP address may connect or using this as HTTP web proxy

# vi /etc/squid/squid.conf

Add the following lines

http_access allow all

Run Squid http web proxy

#service squid start

Setup Squid for anonymous traffic and mask original local IP address.

Add the following lines to Squid configuration file.

#vi /etc/squid/squid.conf
forwarded_for off
request_header_access Allow allow all
request_header_access Authorization allow all
request_header_access WWW-Authenticate allow all
request_header_access Proxy-Authorization allow all
request_header_access Proxy-Authenticate allow all
request_header_access Cache-Control allow all
request_header_access Content-Encoding allow all
request_header_access Content-Length allow all
request_header_access Content-Type allow all
request_header_access Date allow all
request_header_access Expires allow all
request_header_access Host allow all
request_header_access If-Modified-Since allow all
request_header_access Last-Modified allow all
request_header_access Location allow all
request_header_access Pragma allow all
request_header_access Accept allow all
request_header_access Accept-Charset allow all
request_header_access Accept-Encoding allow all
request_header_access Accept-Language allow all
request_header_access Content-Language allow all
request_header_access Mime-Version allow all
request_header_access Retry-After allow all
request_header_access Title allow all
request_header_access Connection allow all
request_header_access Proxy-Connection allow all
request_header_access User-Agent allow all
request_header_access Cookie allow all
request_header_access All deny all

Save and exit then restart squid for the squid configuration applied changes

#service squid restart

Crontab job auto restart server on memory low CentOS 6 server

VPS Server with little memory is it very important to set up crontab job to automatically restart VPS Server on low memory based on condition. Check how much memory is left on the VPS Server, if 100M memory left then reboot the VPS Server.

Write bash script to checks how much memory is left and reboot the VPS.

#vim restartmemorylow

Below is script for CentOS, for Debian 7/8 find at bottom of this page

#!/bin/bash
mem=$(free -m | awk '/Mem:/{print $4}')
(( mem <= 100 )) && /sbin/shutdown -r now

We also can Swap memory, if less than 100M then reboot VPS Server

#vim restartswapmemorylow
#!/bin/bash
swapmem=$(free -m | awk '/Swap:/{print $4}')
(( swapmem <= 100 )) && /sbin/shutdown -r now

Make the script executable

#chmod +x restartmemorylow
#chmod +x restartswapmemorylow

Add script to the to crontab (make sure path/to/the/script correct)

crontab -u root -e

Check memory low every week  At 01:00 on Sunday (https://crontab.guru)

0 1 * * 0 /restartmemorylow
0 1 * * 0 /restartswapmemorylow

Error occurred and how to solved

Check error by check current mail with following command

tail -f /var/spool/mail/root

If crontab command not found, Install cron with following command

#crontab -u root -e
-bash: crontab: command not found
#yum install vixie-cron crontabs
#chkconfig crond on
#service crond start
#chkconfig --list | grep crond

Low memory script and install cron on Debian 7 / 8

#vim restartmemorylow
#!/bin/bash
mem=$(cat /proc/meminfo | egrep "^MemFree" |awk '{print $2}')
if (( mem <= 51200 )); then
echo "Memory lower than or 10%, so we kill and restart";
/sbin/shutdown -r now
else
echo "Memory is fine"
fi

Make the script executable

#chmod +x restartmemorylow
#apt-get install cron

For all cron jobs that should be executed under a user’s account, you should use crontab -e. For system jobs, you should add a file under /etc/cron.d, if that exists; under /etc/cron.{hourly|daily|weekly|monthly} (but those must not be named like a package name!), if that fits your purpose; or add a line to /etc/crontab. But be aware that /etc/crontab might be overwritten with a system update.

Insert

#crontab -u root -e
0 1 * * 0 root /restartmemorylow

Enable cron log on Debian

#vim /etc/rsyslog.conf

In the file, you will find the following line: #cron.* -/var/log/cron
Uncomment the line (remove the #) and save the rsyslog.conf file

cron.* -/var/log/cron
#service rsyslog restart

After restarting the rsyslog daemon, crontab log entries will appear in the /var/log/cron.log file.