Full-service Internet Marketing & Web Development
Recent Posts

Featured Posts
|
How to install PHP 5.2.11 with php-fpmMike Peters, November 10 |
This post will walk you through the process of installing PHP 5.2.11 to run under NGinx using PHP-FPM.
NGinx doesn't have built-in support for running PHP, it uses a third-party daemon to spawn instances of php-cgi and then communicates with those instances over sockets.
PHP-FPM is a replacement for spawn-cgi. Runs faster and is more reliable.
Step 1: Download PHP 5.2.11 and apply PHP-FPM patch
Step 2: Configure PHP
Step 3: Install
Step 4: Start PHP-FPM
View 1 Comment(s)
NGinx doesn't have built-in support for running PHP, it uses a third-party daemon to spawn instances of php-cgi and then communicates with those instances over sockets.
PHP-FPM is a replacement for spawn-cgi. Runs faster and is more reliable.
Step 1: Download PHP 5.2.11 and apply PHP-FPM patch
cd /home
mkdir temp
cd temp
wget http://www.softwareprojects.com/files/auto/php-5.2.11.tar.gz
wget http://www.softwareprojects.com/php-5.2.11-fpm-0.5.13.diff.gz
tar zxvf php-5.2.11.tar.gz
gzip -cd php-5.2.11-fpm-0.5.13.diff.gz | patch -d php-5.2.11 -p1
mkdir temp
cd temp
wget http://www.softwareprojects.com/files/auto/php-5.2.11.tar.gz
wget http://www.softwareprojects.com/php-5.2.11-fpm-0.5.13.diff.gz
tar zxvf php-5.2.11.tar.gz
gzip -cd php-5.2.11-fpm-0.5.13.diff.gz | patch -d php-5.2.11 -p1
Step 2: Configure PHP
cd php-5.2.11
'./configure' '--enable-fastcgi' '--enable-fpm' '--enable-calendar' '--enable-ftp' '--enable-mbstring' '--enable-mysql' '--with-curl' '--with-dom' '--with-mcrypt' '--with-gd' '--with-iconv' '--with-jpeg-dir=/usr/lib' '--with-mysql=/usr/local/mysql' '--with-openssl' '--with-soap' '--with-sockets' '--with-zlib' '--enable-zip'
make all
'./configure' '--enable-fastcgi' '--enable-fpm' '--enable-calendar' '--enable-ftp' '--enable-mbstring' '--enable-mysql' '--with-curl' '--with-dom' '--with-mcrypt' '--with-gd' '--with-iconv' '--with-jpeg-dir=/usr/lib' '--with-mysql=/usr/local/mysql' '--with-openssl' '--with-soap' '--with-sockets' '--with-zlib' '--enable-zip'
make all
Step 3: Install
make install
strip /usr/local/bin/php-cgi
strip /usr/local/bin/php-cgi
Step 4: Start PHP-FPM
/usr/local/sbin/php-fpm start
View 1 Comment(s)
|
How to install PHP ChartDirector under FreeBSDMichel Nadeau, November 6 |
PHP ChartDirector is a powerful PHP extension that allows to generate very complex charts. You can visit their Web site here.
This tutorial will show you how to install it under FreeBSD.
1. Download PHP ChartDirector
The download page is located here -
http://www.advsofteng.com/download.html
Make sure you pick the FreeBSD version for PHP, and make sure you choose the good version between 32 and 64-bit. At the time of writing this post, the latest 32-bit version available for FreeBSD was this one -
http://download2.advsofteng.com/char...freebsd.tar.gz
Now that you have the download link, the first thing to do is to find out where is your PHP extensions directory. You can find it by checking at phpinfo()'s output or in your php.ini file (which is usually located in /usr/local/lib/php.ini). Just search for "extension_dir" in php.ini - you will find it right away. In our case, our extensions directory was "/usr/local/lib/php/extensions/current". We can now download and extract ChartDirector...
When extracting ChartDirector, it creates a "ChartDirector" folder that contains these files/folders:
We are mainly interested by the "lib" directory. Let's copy the files that we need...
You can get rid of these 2 PHP files:
2. Determine the right version
To determine the right version of the DLL that you must use, refer to this page -
http://www.advsofteng.com/doc/cdphpdoc/phpinstall.htm
For example, if you have PHP 4.4.9, you will use this one -
PHP Version 4.2.1 and above: phpchartdir421.dll
Even if it's FreeBSD and not Windows, you are going to specify a DLL as the extension in your php.ini file. You can delete all the others DLL's that you are not using, but make sure you keep the "fonts" folder and the "libchardir.so" file - they are required. So your extensions directory might look like this:
3. Configure the extension in php.ini
Now that we have all we need, we're going to configure php.ini to add the PHP ChartDirector extension.
Open your php.ini and add a line like this one in the extensions section:
Make sure you use the right DLL for your PHP version. Save your php.ini file and restart your PHP engine and/or Web server (Apache, FastCGI, etc.) to reload your PHP configuration.
To test PHP ChartDirector, you can use the "phpdemo" directory that you can find in the PHP ChartDirector archive. Just copy the folder in the root of your Web server and point it with your browser (ex: http://yourWebAddress/phpdemo). This demo is also very convenient for charts examples.
This tutorial will show you how to install it under FreeBSD.
1. Download PHP ChartDirector
The download page is located here -
http://www.advsofteng.com/download.html
Make sure you pick the FreeBSD version for PHP, and make sure you choose the good version between 32 and 64-bit. At the time of writing this post, the latest 32-bit version available for FreeBSD was this one -
http://download2.advsofteng.com/char...freebsd.tar.gz
Now that you have the download link, the first thing to do is to find out where is your PHP extensions directory. You can find it by checking at phpinfo()'s output or in your php.ini file (which is usually located in /usr/local/lib/php.ini). Just search for "extension_dir" in php.ini - you will find it right away. In our case, our extensions directory was "/usr/local/lib/php/extensions/current". We can now download and extract ChartDirector...
cd /usr/local/lib/php/extensions/current
wget http://download2.advsofteng.com/chartdir_php_freebsd.tar.gz
tar xvfz chartdir_php_freebsd.tar.gz
wget http://download2.advsofteng.com/chartdir_php_freebsd.tar.gz
tar xvfz chartdir_php_freebsd.tar.gz
When extracting ChartDirector, it creates a "ChartDirector" folder that contains these files/folders:
doc/
lib/
phpdemo/
LICENSE.TXT
README.TXT
lib/
phpdemo/
LICENSE.TXT
README.TXT
We are mainly interested by the "lib" directory. Let's copy the files that we need...
cd /usr/local/lib/php/extensions/current
mv ChartDirector/lib/* .
mv ChartDirector/lib/* .
You can get rid of these 2 PHP files:
rm FinanceChart.php
rm phpchartdir.php
rm phpchartdir.php
2. Determine the right version
To determine the right version of the DLL that you must use, refer to this page -
http://www.advsofteng.com/doc/cdphpdoc/phpinstall.htm
For example, if you have PHP 4.4.9, you will use this one -
PHP Version 4.2.1 and above: phpchartdir421.dll
Even if it's FreeBSD and not Windows, you are going to specify a DLL as the extension in your php.ini file. You can delete all the others DLL's that you are not using, but make sure you keep the "fonts" folder and the "libchardir.so" file - they are required. So your extensions directory might look like this:
fonts/
libchartdir.so
phpchartdir421.dll
libchartdir.so
phpchartdir421.dll
3. Configure the extension in php.ini
Now that we have all we need, we're going to configure php.ini to add the PHP ChartDirector extension.
Open your php.ini and add a line like this one in the extensions section:
extension=phpchartdir421.dll
Make sure you use the right DLL for your PHP version. Save your php.ini file and restart your PHP engine and/or Web server (Apache, FastCGI, etc.) to reload your PHP configuration.
To test PHP ChartDirector, you can use the "phpdemo" directory that you can find in the PHP ChartDirector archive. Just copy the folder in the root of your Web server and point it with your browser (ex: http://yourWebAddress/phpdemo). This demo is also very convenient for charts examples.
|
How to count unique visitors in an nginx access.logMike Peters, November 4 |
This nifty one-liner will count the number of unique visitors in an NGinx web-server access.log file, for a given day:
Replace 04/Nov/2009 with the date you'd like to count the number of unique visitors for.
grep "\[04/Nov/2009" access.log | cut -d" " -f1 | sort | uniq | wc -l
Replace 04/Nov/2009 with the date you'd like to count the number of unique visitors for.
|
FreeBSD No Ports FixMike Peters, November 4 |
When installing a FreeBSD system, you're asked if you would like to install the Ports Collection. If you chose no, you can follow these instructions to obtain the ports collection:
Option 1 - CVSup
Option 2 - Portsnap
Option 1 - CVSup
csup -L 2 -h cvsup.FreeBSD.org /usr/share/examples/cvsup/ports-supfile
csup
csup
Option 2 - Portsnap
portsnap fetch
portsnap extract
portsnap extract
|
Do It Yourself Web Server and Database monitoringMike Peters, October 21 |
When you have production web servers and databases, it is absolutely critical to have a monitoring system in-place that will alert you whenever service goes down.
Getting Started with website monitoring
One of our favorite monitoring tools is Pingdom. It's a $9.95/month easy-to-use service that will ping 5 of your servers, once every 60 seconds, alerting you via email & SMS whenever any server goes down.
In addition to downtime notifications, the service also provides response-time trends, raw data and uptime-over-time reports:

If you don't have any monitoring in-place right one, you should definitely try out Pingdom or a similar service.
In about 5 minutes of setting it up, you'll have the peace of mind, knowing exactly when your merchant website, affiliate landing page or payment gateway goes down.
Beyond simple HTTP monitoring
While Pingdom and other similar tools are great in letting you know once your website is -already- down, a lot of times the outage can be prevented in the first place, by detecting a decrease available memory, cpu, disk space etc.
At the pinnacle of server monitoring are tools such as Nagios and Munin.
Munin will monitor every single resource on your server, including: memory, cpu utilization, available handles, open connections, average load, running processes and more.
Nagios sends alerts to groups of users based on predetermined rules.

Do It Yourself Server Monitoring
In this post, I'd like to focus on how to create a simple monitoring script, that will survey remote servers for a few critical metrics, displaying the results on the screen.
Unlike Nagios+Munin, such a setup doesn't require installing a monitoring component on each server. Your monitoring script can run remotely, surveying all servers once every 60 seconds on a cronjob, sending an email/sms whenever detecting abnormal behavior.
For the purpose of this example, our script will monitor the following metrics:
= MySQL Database server
* Number of running threads
* Number of threads connected
* Queries per second
* Open tables
* Sample query response
* Number of tables in database
= Any FreeBSD/Linux server
* CPU load over the last 5 minutes
* Available memory
* Open sockets
* Number of running processes
// Set these for easier access
$USERNAME = "root";
$SERVER = "www.mydomain.com";
$DBHOST = "www.mydatabase.com";
$DBUSER = "root";
$DBPASS = "dbpassword";
$DBNAME = "dbdatabasename";
// Get load average, available memory and number of running processes
$load_average = "N/A";
$available_memory = "N/A";
$running_processes = "N/A";
$CRLF = "\\r\"\n";
exec("ssh -l$USERNAME $SERVER \"top -b -n 1\"",&$buf);
for ($i=0,$str=""; $i<count($buf); $i++) $str .= $buf[$i].$CRLF;
if (($pos=strpos($str,"load averages:"))!==false)
{
$str = trim(substr($str, $pos+strlen("load averages:")));
if (($pos=strpos($str,$CRLF))!==false)
{
$load_average = substr($str,0,$pos);
if (($pos=strpos($str,"up"))!==false) $load_average = substr($load_average,0,$pos);
}
}
else
if (($pos=strpos($str,"load average:"))!==false)
{
$str = trim(substr($str, $pos+strlen("load average:")));
if (($pos=strpos($str,$CRLF))!==false)
{
$load_average = substr($str,0,$pos);
}
}
if (($pos=strpos($str,"processes:"))!==false)
{
$str = trim(substr($str, $pos+strlen("processes:")));
if (($pos=strpos($str,","))!==false)
{
$running_processes = substr($str,0,$pos);
}
}
else
if (($pos=strpos($str,"Tasks:"))!==false)
{
$str = trim(substr($str, $pos+strlen("Tasks:")));
if (($pos=strpos($str,","))!==false)
$str = trim(substr($str, $pos+1));
if (($pos=strpos($str,"run"))!==false)
{
$running_processes = substr($str,0,$pos);
}
}
if (($pos=strpos($str,"Mem:"))!==false)
{
$str = trim(substr($str, $pos+strlen("Mem:")));
if (($pos=strpos($str,$CRLF))!==false)
{
$available_memory = substr($str, 0, $pos);
}
}
// Get open sockets
$open_sockets = "N/A";
$buf = array();
exec("ssh -l$USERNAME $SERVER "netstat -n | wc -l"",&$buf);
$open_sockets = trim($buf[0]);
// Display server metrics
echo "== Server\r\n";
echo " Load average: $load_average\r\n";
echo " Open Sockets: $open_sockets\r\n";
echo " Running Processes: $running_processes\r\n";
echo " Available memory: $available_memory\r\n";
echo "\\r\"\n";
// Get running and connected threads
$db_threads_connected = "N/A";
$db_running_threads = "N/A";
mysql_connect($DBHOST,$DBUSER,$DBPASS);
$Result = @mysql_query("show status like '%thread%' ");
$cnt = @mysql_num_rows($Result);
while ($cnt)
{
$cnt--;
if (!($Row = @mysql_fetch_array($Result))) continue;
if (empty($Row['Value'])) continue;
if (Strcasecmp($Row['Variable_name'],'Threads_connected')==0)
$db_threads_connected = $Row['Value'];
else
if (Strcasecmp($Row['Variable_name'],'Threads_running')==0)
$db_running_threads = $Row['Value'];
}
// Get queries per second
$Result = @mysql_query("show status like 'questions' ");
$Row = @mysql_fetch_array($Result);
$questions = $Row['Value']+0;
$Result = @mysql_query("show status like 'uptime' ");
$Row = @mysql_fetch_array($Result);
$uptime = $Row['Value']+0;
$db_queries_per_second = number_format($questions/$uptime,0);
// Get open tables
$db_open_tables = "N/A";
$Result = @mysql_query("show status like '%tables%' ");
$cnt = @mysql_num_rows($Result);
while ($cnt)
{
$cnt--;
if (!($Row = @mysql_fetch_array($Result))) continue;
if (empty($Row['Value'])) continue;
if (Strcasecmp($Row['Variable_name'],'Open_tables')==0)
$db_open_tables = $Row['Value'];
}
// Get query response
$Result = @mysql_query("SELECT 1");
$Row = @mysql_fetch_row($Result);
$db_query_response = $Row[0]=='1' ? "Good" : "BAD";
// Get total number of tables
@mysql_query("use $DBNAME");
$Result = @mysql_query("show tables");
$db_tables_in_database = @mysql_num_rows($Result);
// Display database metrics
echo "== Database\r\n";
echo " Running Threads: $db_running_threads\r\n";
echo " Threads Connected: $db_threads_connected\r\n";
echo " Queries Per Second: $db_queries_per_second\r\n";
echo " Open Tables: $db_open_tables\r\n";
echo " Query Response: $db_query_response\r\n";
echo " Number of tables: $db_tables_in_database\r\n";
The output of the script will look something like this:
Tip: If you're looking to monitor dozens of servers, consider applying our ssh no password approach, to save the hassle of having to exchange private keys with each server.
Getting Started with website monitoring
One of our favorite monitoring tools is Pingdom. It's a $9.95/month easy-to-use service that will ping 5 of your servers, once every 60 seconds, alerting you via email & SMS whenever any server goes down.
In addition to downtime notifications, the service also provides response-time trends, raw data and uptime-over-time reports:

If you don't have any monitoring in-place right one, you should definitely try out Pingdom or a similar service.
In about 5 minutes of setting it up, you'll have the peace of mind, knowing exactly when your merchant website, affiliate landing page or payment gateway goes down.
Beyond simple HTTP monitoring
While Pingdom and other similar tools are great in letting you know once your website is -already- down, a lot of times the outage can be prevented in the first place, by detecting a decrease available memory, cpu, disk space etc.
At the pinnacle of server monitoring are tools such as Nagios and Munin.
Munin will monitor every single resource on your server, including: memory, cpu utilization, available handles, open connections, average load, running processes and more.
Nagios sends alerts to groups of users based on predetermined rules.

Do It Yourself Server Monitoring
In this post, I'd like to focus on how to create a simple monitoring script, that will survey remote servers for a few critical metrics, displaying the results on the screen.
Unlike Nagios+Munin, such a setup doesn't require installing a monitoring component on each server. Your monitoring script can run remotely, surveying all servers once every 60 seconds on a cronjob, sending an email/sms whenever detecting abnormal behavior.
For the purpose of this example, our script will monitor the following metrics:
= MySQL Database server
* Number of running threads
* Number of threads connected
* Queries per second
* Open tables
* Sample query response
* Number of tables in database
= Any FreeBSD/Linux server
* CPU load over the last 5 minutes
* Available memory
* Open sockets
* Number of running processes
// Set these for easier access
$USERNAME = "root";
$SERVER = "www.mydomain.com";
$DBHOST = "www.mydatabase.com";
$DBUSER = "root";
$DBPASS = "dbpassword";
$DBNAME = "dbdatabasename";
// Get load average, available memory and number of running processes
$load_average = "N/A";
$available_memory = "N/A";
$running_processes = "N/A";
$CRLF = "\\r\"\n";
exec("ssh -l$USERNAME $SERVER \"top -b -n 1\"",&$buf);
for ($i=0,$str=""; $i<count($buf); $i++) $str .= $buf[$i].$CRLF;
if (($pos=strpos($str,"load averages:"))!==false)
{
$str = trim(substr($str, $pos+strlen("load averages:")));
if (($pos=strpos($str,$CRLF))!==false)
{
$load_average = substr($str,0,$pos);
if (($pos=strpos($str,"up"))!==false) $load_average = substr($load_average,0,$pos);
}
}
else
if (($pos=strpos($str,"load average:"))!==false)
{
$str = trim(substr($str, $pos+strlen("load average:")));
if (($pos=strpos($str,$CRLF))!==false)
{
$load_average = substr($str,0,$pos);
}
}
if (($pos=strpos($str,"processes:"))!==false)
{
$str = trim(substr($str, $pos+strlen("processes:")));
if (($pos=strpos($str,","))!==false)
{
$running_processes = substr($str,0,$pos);
}
}
else
if (($pos=strpos($str,"Tasks:"))!==false)
{
$str = trim(substr($str, $pos+strlen("Tasks:")));
if (($pos=strpos($str,","))!==false)
$str = trim(substr($str, $pos+1));
if (($pos=strpos($str,"run"))!==false)
{
$running_processes = substr($str,0,$pos);
}
}
if (($pos=strpos($str,"Mem:"))!==false)
{
$str = trim(substr($str, $pos+strlen("Mem:")));
if (($pos=strpos($str,$CRLF))!==false)
{
$available_memory = substr($str, 0, $pos);
}
}
// Get open sockets
$open_sockets = "N/A";
$buf = array();
exec("ssh -l$USERNAME $SERVER "netstat -n | wc -l"",&$buf);
$open_sockets = trim($buf[0]);
// Display server metrics
echo "== Server\r\n";
echo " Load average: $load_average\r\n";
echo " Open Sockets: $open_sockets\r\n";
echo " Running Processes: $running_processes\r\n";
echo " Available memory: $available_memory\r\n";
echo "\\r\"\n";
// Get running and connected threads
$db_threads_connected = "N/A";
$db_running_threads = "N/A";
mysql_connect($DBHOST,$DBUSER,$DBPASS);
$Result = @mysql_query("show status like '%thread%' ");
$cnt = @mysql_num_rows($Result);
while ($cnt)
{
$cnt--;
if (!($Row = @mysql_fetch_array($Result))) continue;
if (empty($Row['Value'])) continue;
if (Strcasecmp($Row['Variable_name'],'Threads_connected')==0)
$db_threads_connected = $Row['Value'];
else
if (Strcasecmp($Row['Variable_name'],'Threads_running')==0)
$db_running_threads = $Row['Value'];
}
// Get queries per second
$Result = @mysql_query("show status like 'questions' ");
$Row = @mysql_fetch_array($Result);
$questions = $Row['Value']+0;
$Result = @mysql_query("show status like 'uptime' ");
$Row = @mysql_fetch_array($Result);
$uptime = $Row['Value']+0;
$db_queries_per_second = number_format($questions/$uptime,0);
// Get open tables
$db_open_tables = "N/A";
$Result = @mysql_query("show status like '%tables%' ");
$cnt = @mysql_num_rows($Result);
while ($cnt)
{
$cnt--;
if (!($Row = @mysql_fetch_array($Result))) continue;
if (empty($Row['Value'])) continue;
if (Strcasecmp($Row['Variable_name'],'Open_tables')==0)
$db_open_tables = $Row['Value'];
}
// Get query response
$Result = @mysql_query("SELECT 1");
$Row = @mysql_fetch_row($Result);
$db_query_response = $Row[0]=='1' ? "Good" : "BAD";
// Get total number of tables
@mysql_query("use $DBNAME");
$Result = @mysql_query("show tables");
$db_tables_in_database = @mysql_num_rows($Result);
// Display database metrics
echo "== Database\r\n";
echo " Running Threads: $db_running_threads\r\n";
echo " Threads Connected: $db_threads_connected\r\n";
echo " Queries Per Second: $db_queries_per_second\r\n";
echo " Open Tables: $db_open_tables\r\n";
echo " Query Response: $db_query_response\r\n";
echo " Number of tables: $db_tables_in_database\r\n";
The output of the script will look something like this:
== Server
Load average: 0.09, 0.23, 0.23
Open Sockets: 226
Running Processes: 1 running
Available memory: 2203M Active, 112M Buf, 4668K Free
== Database
Running Threads: 3
Threads Connected: 16
Queries Per Second: 320
Open Tables: 200
Query Response: Good
Number of tables: 1648
Load average: 0.09, 0.23, 0.23
Open Sockets: 226
Running Processes: 1 running
Available memory: 2203M Active, 112M Buf, 4668K Free
== Database
Running Threads: 3
Threads Connected: 16
Queries Per Second: 320
Open Tables: 200
Query Response: Good
Number of tables: 1648
Tip: If you're looking to monitor dozens of servers, consider applying our ssh no password approach, to save the hassle of having to exchange private keys with each server.
|
SSH no password without any private keys. It's magic!Michel Nadeau, October 8 |
We all know that one can configure SSH to login automatically by adding the client's public key to the server's ~/.ssh/authorized_keys file. But what if you need to run commands on 200 machines and don't want to login to every single machine to add the key? Search no more, this tutorial has the answer!
1. Installing sshpass
sshpass is a tool for non-interactivly performing password authentication with SSH's so called "interactive keyboard password authentication".
Here's a standard SSH connect command:
First you have to answer "yes" to accept the host key and then to enter the password.
With sshpass, you are able to specify the password on the command line and skip this step. Here's how to install it:
sshpass is now ready to be used!
2. Using sshpass
The sshpass' syntax is:
So instead of doing:
You can simply do:
You will be automatically logged in, without any password prompt:
3. Automatically accepting host keys
The last problem is this prompt:
When you're using sshpass to connect on a single machine that you use often, it's not a big deal because you will get the prompt once and never again after. But if you want to connect to 200 machines, you definitely don't want to type "yes" 200 times.
To fix this issue, simply add this line in /etc/ssh/ssh_config on the CLIENT machine:
With this setting enabled, SSH will automatically accept the host keys and will not prompt you about it.
4. Automating the process
This section will show you how to easily automate the process of running commands on any number of machines you want.
First you will need a file containing the hostname, username and password for each of the servers you want to run commands on, in a CSV format. For example:
ssh_magic.csv:
Then you will create this bash script:
ssh_magic.sh:
Simply place ssh_magic.csv and ssh_magic.sh together and run the script.
Conclusion
You now know how to run commands on any number of SSH machines, without any prompt! Make sure you check the other ways to pass the password to sshpass - they offer more secure ways than directly on the command line with the -p option.
1. Installing sshpass
sshpass is a tool for non-interactivly performing password authentication with SSH's so called "interactive keyboard password authentication".
Here's a standard SSH connect command:
debian_I:~# ssh -l root localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is b4:e9:e7:56:a2:b4:89:9b:d8:fd:7e:8e:f1:e4:1d:9f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password:
Last login: Thu Oct 8 09:35:41 2009 from localhost
Linux debian_I 2.6.18-6-686 #1 SMP i686
debian_I:~#
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is b4:e9:e7:56:a2:b4:89:9b:d8:fd:7e:8e:f1:e4:1d:9f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password:
Last login: Thu Oct 8 09:35:41 2009 from localhost
Linux debian_I 2.6.18-6-686 #1 SMP i686
debian_I:~#
First you have to answer "yes" to accept the host key and then to enter the password.
With sshpass, you are able to specify the password on the command line and skip this step. Here's how to install it:
mkdir -p /usr/local/src/
cd /usr/local/src/
wget http://downloads.sourceforge.net/project/sshpass/sshpass/1.04/sshpass-1.04.tar.gz?use_mirror=iweb
tar xvfz sshpass-1.04.tar.gz
cd sshpass-1.04
./configure
make
make install
cd /usr/local/src/
wget http://downloads.sourceforge.net/project/sshpass/sshpass/1.04/sshpass-1.04.tar.gz?use_mirror=iweb
tar xvfz sshpass-1.04.tar.gz
cd sshpass-1.04
./configure
make
make install
sshpass is now ready to be used!
2. Using sshpass
The sshpass' syntax is:
sshpass -p [password] [ssh command]
So instead of doing:
ssh -l root localhost
You can simply do:
sshpass -p myrootpass ssh -l root localhost
You will be automatically logged in, without any password prompt:
debian_I:~# sshpass -p myrootpass ssh -l root localhost
Last login: Thu Oct 8 09:52:04 2009 from localhost
Linux debian_I 2.6.18-6-686 #1 SMP i686
debian_I:~#
Last login: Thu Oct 8 09:52:04 2009 from localhost
Linux debian_I 2.6.18-6-686 #1 SMP i686
debian_I:~#
3. Automatically accepting host keys
The last problem is this prompt:
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is b4:e9:e7:56:a2:b4:89:9b:d8:fd:7e:8e:f1:e4:1d:9f.
Are you sure you want to continue connecting (yes/no)? yes
RSA key fingerprint is b4:e9:e7:56:a2:b4:89:9b:d8:fd:7e:8e:f1:e4:1d:9f.
Are you sure you want to continue connecting (yes/no)? yes
When you're using sshpass to connect on a single machine that you use often, it's not a big deal because you will get the prompt once and never again after. But if you want to connect to 200 machines, you definitely don't want to type "yes" 200 times.
To fix this issue, simply add this line in /etc/ssh/ssh_config on the CLIENT machine:
StrictHostKeyChecking=no
With this setting enabled, SSH will automatically accept the host keys and will not prompt you about it.
4. Automating the process
This section will show you how to easily automate the process of running commands on any number of machines you want.
First you will need a file containing the hostname, username and password for each of the servers you want to run commands on, in a CSV format. For example:
ssh_magic.csv:
someserver.com,root,123456
someotherserver.com,sshadmin,abcdef
onelastserver.com,root,123456
someotherserver.com,sshadmin,abcdef
onelastserver.com,root,123456
Then you will create this bash script:
ssh_magic.sh:
#!/bin/sh
# Loop ssh_magic.csv
for LINE in $(cat ssh_magic.csv)
do
# Split line
host=$(echo $LINE | cut -d "," -f1)
user=$(echo $LINE | cut -d "," -f2)
pass=$(echo $LINE | cut -d "," -f3)
# Display server info
echo ""
echo "HOSTNAME: $host"
echo ""
# Run commands
sshpass -p $pass ssh -l $user $host "uname -a"
sshpass -p $pass ssh -l $user $host "df -h"
echo ""
echo "===================================="
done
echo ""
echo "DONE"
echo ""
# Loop ssh_magic.csv
for LINE in $(cat ssh_magic.csv)
do
# Split line
host=$(echo $LINE | cut -d "," -f1)
user=$(echo $LINE | cut -d "," -f2)
pass=$(echo $LINE | cut -d "," -f3)
# Display server info
echo ""
echo "HOSTNAME: $host"
echo ""
# Run commands
sshpass -p $pass ssh -l $user $host "uname -a"
sshpass -p $pass ssh -l $user $host "df -h"
echo ""
echo "===================================="
done
echo ""
echo "DONE"
echo ""
Simply place ssh_magic.csv and ssh_magic.sh together and run the script.
Conclusion
You now know how to run commands on any number of SSH machines, without any prompt! Make sure you check the other ways to pass the password to sshpass - they offer more secure ways than directly on the command line with the -p option.
|
How to install basic Squid on FreeBSDMichel Nadeau, October 2 |
Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-requested web pages. Squid has extensive access controls and makes a great server accelerator. It runs on most available operating systems, including Windows and is licensed under the GNU GPL.
This tutorial describes how to install a very simple implementation of Squid on your network.
1. Getting started
The first thing you want to do is to download Squid. In this tutorial, we're going to work in the /usr/local/src directory.
Then you want to extract Squid and change to its directory:
We're now ready to compile and install Squid!
2. Compiling/installing Squid
Compiling and installing Squid is very easy:
If the configure command fails because you don't have Perl, you can simply install it like this:
Then re-run the configure command.
3. Configuring Squid
First of all, you need to add the "visible_hostname" setting in your Squid configuration file. The main configuration file is:
Open it with your favorite editor and find this block:
At the end of the block (before the next "TAG" block), insert a new line and put something like this:
Replace "freebsd" with any hostname you want Squid to use - it can be a local hostname or a fully qualified domain name. Save the file.
Now you need to adjust some permissions before Squid can be initialized...
Squid can now be initialized. Use this command:
Squid is now ready to run!
4. Starting/stopping Squid
Starting Squid:
Stopping Squid:
You can start Squid automatically at boot time by creating the /usr/local/etc/rc.d/squid.sh file with this content:
You also need to allow execution of this file:
5. Using Squid with your browser
To use Squid with your browser, you simply need to set it as a proxy in your browser's settings. Squid is listening on TCP port 3128. So if your FreeBSD machine's IP is 1.2.3.4, you will configure your proxy to be 1.2.3.4, port 3128.
Once configured, all the traffic over the chosen protocols (usually your browser lets you choose for which protocols you want to use a proxy) will go through your Squid server.
6. "Overriding" Web sites addresses
The first place where Squid is looking when it comes to resolving URL's to IP addresses is in the /etc/hosts file. If you want to "override" Web sites addresses, simply add them in the /etc/hosts file along with the IP address where you want to redirect traffic.
For example, let's say that you want your Squid's users to be sent to 1.2.3.4 when they request www.google.com instead of to the real google.com. To do so, you will add this line in your /etc/hosts file:
Replace "1.2.3.4" with the IP of the machine where you want to redirect traffic. This isn't really a "redirect" as the users will never see 1.2.3.4 in their browser. For them, it will look like just as if they were really on www.google.com, not on 1.2.3.4.
NOTE: you NEED to restart Squid when you make changes in /etc/hosts.
Conclusion
Squid is very simple to install and use. Though, it's also VERY powerful and flexible: it has literally thousands of options! This tutorial only covered the very basic use - refer to the Squid users guide if you want to customize squid.conf.
Resources
* Squid
* Configuration guide
* Configuration examples
* Users guide
This tutorial describes how to install a very simple implementation of Squid on your network.
1. Getting started
The first thing you want to do is to download Squid. In this tutorial, we're going to work in the /usr/local/src directory.
$ mkdir -p /usr/local/src
$ cd /usr/local/src
$ wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE19.tar.gz
$ cd /usr/local/src
$ wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE19.tar.gz
Then you want to extract Squid and change to its directory:
$ tar xvfz squid-3.0.STABLE19.tar.gz
$ cd squid-3.0.STABLE19
$ cd squid-3.0.STABLE19
We're now ready to compile and install Squid!
2. Compiling/installing Squid
Compiling and installing Squid is very easy:
$ ./configure --prefix=/usr/local/squid
$ make all
$ make install
$ make all
$ make install
If the configure command fails because you don't have Perl, you can simply install it like this:
$ pkg_add -r perl
Then re-run the configure command.
3. Configuring Squid
First of all, you need to add the "visible_hostname" setting in your Squid configuration file. The main configuration file is:
/usr/local/squid/etc/squid.conf
Open it with your favorite editor and find this block:
# TAG: visible_hostname
At the end of the block (before the next "TAG" block), insert a new line and put something like this:
visible_hostname freebsd
Replace "freebsd" with any hostname you want Squid to use - it can be a local hostname or a fully qualified domain name. Save the file.
Now you need to adjust some permissions before Squid can be initialized...
$ mkdir -p /usr/local/squid/var/logs/
$ chmod 777 /usr/local/squid/var/logs/
$ mkdir -p /usr/local/squid/var/cache/
$ chmod 777 /usr/local/squid/var/cache/
$ chmod 777 /usr/local/squid/var/logs/
$ mkdir -p /usr/local/squid/var/cache/
$ chmod 777 /usr/local/squid/var/cache/
Squid can now be initialized. Use this command:
/usr/local/squid/sbin/squid -z
Squid is now ready to run!
4. Starting/stopping Squid
Starting Squid:
/usr/local/squid/sbin/squid
Stopping Squid:
kill -9 `cat /usr/local/squid/var/logs/squid.pid`
You can start Squid automatically at boot time by creating the /usr/local/etc/rc.d/squid.sh file with this content:
#!/bin/sh
/usr/local/squid/sbin/squid
/usr/local/squid/sbin/squid
You also need to allow execution of this file:
$ chmod 755 /usr/local/etc/rc.d/squid.sh
5. Using Squid with your browser
To use Squid with your browser, you simply need to set it as a proxy in your browser's settings. Squid is listening on TCP port 3128. So if your FreeBSD machine's IP is 1.2.3.4, you will configure your proxy to be 1.2.3.4, port 3128.
Once configured, all the traffic over the chosen protocols (usually your browser lets you choose for which protocols you want to use a proxy) will go through your Squid server.
6. "Overriding" Web sites addresses
The first place where Squid is looking when it comes to resolving URL's to IP addresses is in the /etc/hosts file. If you want to "override" Web sites addresses, simply add them in the /etc/hosts file along with the IP address where you want to redirect traffic.
For example, let's say that you want your Squid's users to be sent to 1.2.3.4 when they request www.google.com instead of to the real google.com. To do so, you will add this line in your /etc/hosts file:
1.2.3.4 google.com www.google.com
Replace "1.2.3.4" with the IP of the machine where you want to redirect traffic. This isn't really a "redirect" as the users will never see 1.2.3.4 in their browser. For them, it will look like just as if they were really on www.google.com, not on 1.2.3.4.
NOTE: you NEED to restart Squid when you make changes in /etc/hosts.
Conclusion
Squid is very simple to install and use. Though, it's also VERY powerful and flexible: it has literally thousands of options! This tutorial only covered the very basic use - refer to the Squid users guide if you want to customize squid.conf.
Resources
* Squid
* Configuration guide
* Configuration examples
* Users guide
| « Previous Posts |
