Thursday, May 17, 2012

how to fix Yum thread.error: can’t start new thread

If you get this error (thread.error: can’t start new thread) when you are trying to update your CentOS (I got this error when I tried to install PHP), you have a two step solution to overcome this issue. This is not very common issue, and its about the memory resource problem with the VPS which is caused by Yum’s fastestmirror plugin. Here is the solution;
  1. Stop the mysql server.
  2. Start Yum again after disabling Yum’s fastestmirror plugin like the code below;
yum --disableplugin=fastestmirror update
 
You should now update your CentOS.

Thursday, May 10, 2012

How to Install Apache 2.4.2 from Source on CentOS 6.2 with SSL


Make sure you have gcc and openssl-devel installed.
# yum install gcc
# yum install openssl-devel
You also need “Apache Portable Runtime Library” APR to install Apache from source.
You’ll already have “apr” and “apr-util” package installed. Install the apr-devel and apr-util-devel packages.
# yum install apr-devel
# yum install apr-util-devel
Note: In our case (because of the version compatibility issues), we’ll be downloading these and installing it manually later. But, let us go with the flow for now and see what happens when you try to do it this way.

Download Apache

Download Apache from httpd.apache.org. The current stable release is 2.4.2.
Once you get the direct URL to download the latest stable version of Apache, use wget as shown below to download it directly to you server.
cd /usr/src
wget http://mirror.nyi.net/apache//httpd/httpd-2.4.2.tar.gz
tar xvfz httpd-2.4.2.tar.gz

Install Apache with SSL/TLS

View all available Apache installation and configuration options as shown below.
cd httpd-2.4.2
./configure --help
To install an Apache module, you would typically say –enable-{module-name}. For example, to install SSL with Apache, it is –enable-ssl. To install ldap module, it is –enable-ldap.
To uninstall any default module that comes with Apache, you would typically say –disable-{module-name}. For example, to disable basic authentication in Apache, it is –disable-auth-basic
In this example, we will install Apache with all default modules, with addition of –enable-ssl (to install mod_ssl for SSL support), and –enable-so, which helps to load modules in Apache during run-time via the Dynamic Shared Object (DSO) mechanism, rather than requiring a recompilation.
./configure --enable-ssl --enable-so
make
make install
Note: By default the above installs Apache under /usr/local/apache2. If you like to change this location, use –prefix option in the ./configure.

Fixing APR Utility Error Messages

You might’ve not faced this problem while installing older version of Apache as we discussed a while back.
When you execute the “make”, you might get “rotatelogs.c:(.text+0x5ed): undefined reference to `apr_file_link’” error message if you are doing this on CentOS 6.2 as shown below.
# make
rotatelogs.c:298: warning: implicit declaration of function âapr_file_linkâ
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -std=gnu99 -pthread
-o rotatelogs  rotatelogs.lo /usr/lib64/libaprutil-1.la -ldb-4.7 -lexpat -ldb-4.7 /usr/lib64/libapr-1.la -lpthread
rotatelogs.o: In function `post_rotate':
rotatelogs.c:(.text+0x5ed): undefined reference to `apr_file_link'
collect2: ld returned 1 exit status
make[2]: *** [rotatelogs] Error 1
make[2]: Leaving directory `/usr/src/httpd-2.4.2/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/src/httpd-2.4.2/support'
make: *** [all-recursive] Error 1
This is because on CentOS 6, the latest APR version available through yum installation is 1.3.9 as shown below.
# rpm -qa apr*
apr-1.3.9-3.el6_1.2.x86_64
apr-util-1.3.9-3.el6_0.1.x86_64
However, Apache 2.4.2 needs the latest version of APR (which is currently 1.4.6).
So, go to APR download page and download both apr and apr-util.
cd /usr/src
wget http://mirror.atlanticmetro.net/apache//apr/apr-1.4.6.tar.gz
wget http://mirror.atlanticmetro.net/apache//apr/apr-util-1.4.1.tar.gz
tar xvfz apr-1.4.6.tar.gz
tar xvfz apr-util-1.4.1.tar.gz
Now, you should place this new version of apr and apr-util directories (without the version name in the directory) under “srclib” directory located under the httpd-2.4.2 directory that was created when you uncompressed the downloaded apache software.
In my example, I downloaded the httpd-2.4.2.tar.gz and uncompressed it under /usr/src. So, I need to place the latest apr and apr-util under this directory.
mv apr-1.4.6 /usr/src/httpd-2.4.2/srclib/apr
mv apr-util-1.4.1 /usr/src/httpd-2.4.2/srclib/apr-util
After this is done, we need to configure and make it again. If you execute the ./configure –help, you’ll see the following options that are related to APR
# cd /usr/src/httpd-2.4.2
# ./configure --help
  --with-included-apr     Use bundled copies of APR/APR-Util
  --with-apr=PATH         prefix for installed APR or the full path to apr-config
  --with-apr-util=PATH    prefix for installed APU or the full path to apu-config
If you decide to install the apr-1.4.6 and apr-util-1.4.1 on your system, you need to use “–with-apr” and “–with-apr-util” and provide the path where you installed these utility.
In this example, we didn’t do that. i.e We didn’t install the apr and apr-util that we downloaded. Instead we placed them under the httpd-2.4.2/srclib/apr-util. So, we should use “–with-included-apr” in the ./configure which will use these apr and apr-util only for the apache compilation and installation.
So, let us re-do the ./configure (using –with-included-apr), make and make install as shown below.
./configure --enable-ssl --enable-so --with-included-apr
make
make install
Now, make will not give “rotatelogs.c:(.text+0x5ed): undefined reference to `apr_file_link” error message anymore.

Enable SSL in httpd.conf

Apache configuration file httpd.conf is located under /usr/local/apache2/conf.
Uncomment the httpd-ssl.conf Include line and the LoadModule ssl_module line in the /usr/local/apache2/conf/httpd.conf file.
# vi /usr/local/apache2/conf/httpd.conf
LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
View the httpd-ssl.conf to review all the default SSL configurations. For most cases, you don’t need to modify anything in this file.
# vi /usr/local/apache2/conf/extra/httpd-ssl.conf
The SSL certificate and key are required before we start the Apache. The server.crt and server.key file mentioned in the httpd-ssl.conf needs to be created before we move forward.
# cd /usr/local/apache2/conf/extra
# egrep 'server.crt|server.key' httpd-ssl.conf
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"

Create server.crt and server.key file

First, Generate the server.key using openssl.
# cd /usr/src
# openssl genrsa -des3 -out server.key 1024
The above command will ask for the password. Make sure to remember this password. You need this while starting your Apache later.
Next, generate a certificate request file (server.csr) using the above server.key file.
# openssl req -new -key server.key -out server.csr
Finally, generate a self signed ssl certificate (server.crt) using the above server.key and server.csr file.
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
After you’ve done with the above steps, you’ll see the following three files under /usr/src
# ls server*
server.crt  server.csr  server.key
Copy the server.key and server.crt file to appropriate Apache configuration directory location.
cp server.key /usr/local/apache2/conf/
cp server.crt /usr/local/apache2/conf/

Start the Apache

If you are getting the below error message, make sure to uncomment the line shown below in httpd.conf
# /usr/local/apache2/bin/apachectl start
AH00526: Syntax error on line 51 of /usr/local/apache2/conf/extra/httpd-ssl.conf:
Invalid command 'SSLCipherSuite', perhaps misspelled or defined by a module not included in the server configuration

# vi /usr/local/apache2/conf/httpd.conf
LoadModule ssl_module modules/mod_ssl.so
If you are getting the below error message, make sure to uncomment the line shown below in httpd.conf
# /usr/local/apache2/bin/apachectl start
AH00526: Syntax error on line 76 of /usr/local/apache2/conf/extra/httpd-ssl.conf:
SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).

# vi /usr/local/apache2/conf/httpd.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
Finally, this will prompt you to enter the password for your private key before starting up the apache.
# /usr/local/apache2/bin/apachectl start
Apache/2.4.2 mod_ssl (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server www.example.com:443 (RSA)
Enter pass phrase:

OK: Pass Phrase Dialog successful.
Verify that the Apache httpd process is running in the background
# ps -ef | grep http
root   29529     1  0 13:08 ?     00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 29530 29529  0 13:08 ?     00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 29531 29529  0 13:08 ?     00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 29532 29529  0 13:08 ?     00:00:00 /usr/local/apache2/bin/httpd -k start
root   29616 18260  0 13:09 pts/0 00:00:00 grep http
To stop the apache use apachectl stop.
# /usr/local/apache2/bin/apachectl stop
Use httpd -l to view all the modules that are compiled inside the Apache httpd daemon.
# /usr/local/apache2/bin/httpd -l
Compiled in modules:
  core.c
  mod_so.c
  http_core.c
  event.c
By default Apache SSL runs on 443 port. Open a web browser and verify that you can access your Apache using https://{your-ip-address}

How Traceroute Works

There are scenarios in which one would like to know the route which a connection follows. By route here we mean the IP addresses of all the forwarding entities (like routers in between).
Although there is no guarantee that the route will remain same for all the packets of a connection but usually it is same. This route related information can be very handy while debugging network related issues.

traceroute utility prints out complete route to a particular destination. In this article we will discuss how traceroute works and will see some practical examples.

How Traceroute Works?

Before beginning with examples, lets understand the concept on which traceroute works.
Traceroute utility uses the TTL field in the IP header to achieve its operation. For users who are new to TTL field, this field describes how much hops a particular packet will take while traveling on network.
So, this effectively outlines the lifetime of the packet on network. This field is usually set to 32 or 64. Each time the packet is held on an intermediate router, it decreases the TTL value by 1. When a router finds the TTL value of 1 in a received packet then that packet is not forwarded but instead discarded.
After discarding the packet, router sends an ICMP error message of “Time exceeded” back to the source from where packet generated. The ICMP packet that is sent back contains the IP address of the router.
So now it can be easily understood that traceroute operates by sending packets with TTL value starting from 1 and then incrementing by one each time. Each time a router receives the packet, it checks the TTL field, if TTL field is 1 then it discards the packet and sends the ICMP error packet containing its IP address and this is what traceroute requires. So traceroute incrementally fetches the IP of all the routers  between the source and the destination.
You should also understand the IP header fields that we discussed a while back.

Traceroute Examples

1. How to run traceroute?

$ traceroute <server-name>
The server-name above is the destination name or IP address.  For example, traceroute is used to find the network path from my machine to google.com :
$ traceroute google.com
traceroute to google.com (74.125.236.132), 30 hops max, 60 byte packets
1  220.224.141.129 (220.224.141.129)  89.174 ms  89.094 ms  89.054 ms
2  115.255.239.65 (115.255.239.65)  109.037 ms  108.994 ms  108.963 ms
3  124.124.251.245 (124.124.251.245)  108.937 ms  121.322 ms  121.300 ms
4  * 115.255.239.45 (115.255.239.45)  113.754 ms  113.692 ms
5  72.14.212.118 (72.14.212.118)  123.585 ms  123.558 ms  123.527 ms
6  72.14.232.202 (72.14.232.202)  123.499 ms  123.475 ms  143.523 ms
7  216.239.48.179 (216.239.48.179)  143.503 ms  95.106 ms  95.026 ms
8  bom03s02-in-f4.1e100.net (74.125.236.132)  94.980 ms  104.989 ms  104.954 ms
Each lines gives the details of interaction with each router encountered. So we see that traceroute not only gives the IP addresses of the intermediate routers but also three round trip times for that particular router as for each router the traceroute commands fires three packets.

The ‘*’ field in output

There are times when one could encounter an ‘*’ in the output rather than a value. This depicts that the required field could not be fetched. The reason can be anything from reverse DNS lookup failure to packets not hitting the target router to packets getting lost on their way back. So we see that the reason could be many but for all these type of cases the traceroute utility provides an * in the output.

2. Disable IP address and host name mapping

Traceroute provides and option through which the mapping of IP addresses with host name (that traceroute tries) is disabled. The option for doing this is ‘-n’ . Following example illustrates it :
$ traceroute google.com -n
traceroute to google.com (173.194.36.7), 30 hops max, 60 byte packets
1  220.224.141.129  109.352 ms  109.280 ms  109.248 ms
2  115.255.239.65  131.633 ms  131.598 ms  131.573 ms
3  124.124.251.245  131.554 ms  131.529 ms  131.502 ms
4  115.255.239.45  131.478 ms  131.464 ms  199.741 ms
5  72.14.212.118  199.674 ms  199.637 ms  199.603 ms
6  209.85.241.52  199.578 ms  199.549 ms  209.838 ms
7  209.85.241.187  199.488 ms  177.264 ms  177.196 ms
8  173.194.36.7  177.159 ms  187.463 ms  187.434 ms
So we see that no host name is displayed in the output.

3. Configure Response Wait Time

The time for which traceroute utility waits after issuing a probe can also be configured. This can be done through ‘-w’ option that it provides. The -w option expects a value which the utility will take as the response time to wait for. In this example, the wait time is 0.1 seconds and the traceroute utility was unable to wait for any response and it printed all the *’s.
$ traceroute google.com -w 0.1
traceroute to google.com (74.125.236.101), 30 hops max, 60 byte packets
1  * * *
2  * * *
3  * * *
..
26  * * *
27  * * *
28  * * *
29  * * *
30  * * *
So we see that traceroute tried 30 attempts (the max hop attempts) and then gave up as no ICMP packet was received in 0.1 seconds.

4. Configure Number of Queries per Hop

As already explained earlier, the traceroute utility sends 3 packets per hop to provide 3 round trip times. This default value of 3 is configurable using the option ‘-q’. This option expects an integer which it sets as new value of number of probes per hop.
$ traceroute google.com -q 5
traceroute to google.com (173.194.36.46), 30 hops max, 60 byte packets
1  220.224.141.129 (220.224.141.129)  91.579 ms  91.497 ms  91.458 ms  91.422 ms  91.385 ms
2  115.255.239.65 (115.255.239.65)  91.356 ms  91.325 ms  98.868 ms  98.848 ms  98.829 ms
3  124.124.251.245 (124.124.251.245)  94.581 ms  107.083 ms  107.044 ms  107.017 ms  106.981 ms
4  115.255.239.45 (115.255.239.45)  106.948 ms  106.918 ms  144.432 ms  144.412 ms  144.392 ms
5  72.14.212.118 (72.14.212.118)  115.565 ms  115.485 ms  115.446 ms  115.408 ms  115.381 ms
6  72.14.232.202 (72.14.232.202)  115.351 ms  87.232 ms  117.157 ms  117.123 ms  117.049 ms
7  209.85.241.189 (209.85.241.189)  126.998 ms  126.973 ms  126.950 ms  126.929 ms  126.912 ms
8  bom04s02-in-f14.1e100.net (173.194.36.46)  126.889 ms  95.526 ms  95.450 ms  95.418 ms  105.392 ms
So we see that after configuring the number of probes to 5, the output started showing five round trip times per hop.

5. Configure the TTL value to start with

Traceroute utility is flexible enough to accept the TTL value that the user wants to start the utility with. By default its value is 1 which means it starts off with the first router in the path but using the ‘-f’ option (which expects the new value of TTL) a new value of the TTL field can be set. For example, I tried a normal traceroute operation and then tried a traceroute with a different TTL value.
$ traceroute google.com
traceroute to google.com (74.125.236.132), 30 hops max, 60 byte packets
1  220.224.141.129 (220.224.141.129)  89.181 ms  101.540 ms  101.503 ms
2  115.255.239.65 (115.255.239.65)  101.468 ms  101.431 ms  101.324 ms
3  124.124.251.245 (124.124.251.245)  121.373 ms  121.350 ms  158.694 ms
4  115.255.239.45 (115.255.239.45)  101.223 ms  141.135 ms  123.932 ms
5  72.14.212.118 (72.14.212.118)  123.867 ms  123.832 ms  123.802 ms
6  72.14.232.202 (72.14.232.202)  123.773 ms  123.742 ms  587.812 ms
7  216.239.48.179 (216.239.48.179)  587.723 ms  587.681 ms  587.642 ms
8  bom03s02-in-f4.1e100.net (74.125.236.132)  577.548 ms  577.524 ms  587.512 ms

$ traceroute google.com -f 8
traceroute to google.com (74.125.236.129), 30 hops max, 60 byte packets
8  bom03s02-in-f1.1e100.net (74.125.236.129)  96.961 ms  96.886 ms  96.849 ms
So we see that after using the -f option with value 8, only the last (8th) line from the previous output was shown.

Thursday, May 3, 2012

How to add cakePHP auto-complete to netbeans

NetBeans is a very powerful development environment for many languages including PHP. With the release of its latest version, NetBeans announced full support for symfony PHP framework. NetBeans can also support many other PHP frameworks and libraries although it is not officially supported by NetBeans. This support needs some effort to enable some tools specially auto-complete, or intellisense, which saves a lot of time for developers.
We will start to make NetBeans support CakePHP. Firstly we need to create a cake project using cake command line tools.
1cd DOCUMENT_ROOT
2cake bake PROJECT_NAME
Then we need to create a NetBeans project using the create files. Click file > New Project, then choose New Project. From the window choose PHP from categories and from projects choose PHP Application with Existing Sources. Click Next then choose the project path. and click finish.
NetBeans new project dialog
NetBeans New Project Dialog
After the project is created select the project in the Projects pane and right click Include Path. Click add folder and choose cake library path and add it. NetBeans will scan and add the library to its internal library.
NetBeans PHP include path dialog
NetBeans PHP include path dialog

Now you will have auto-complete working in your controllers, models, controllers, behaviors and helpers files. This will happen because NetBeans is smart enough to know that your inheriting AppController, AppModel,...etc which in turn inherits Controller and Model classes which is found in CakePHP library directory which NetBeans just scanned. But till now we don't have support for models and components included magically by CakePHP in controllers and models. To make NetBeans identifies those variables we will use PHPDoc trick. In your model or controller add a variable with the same name of your model or any other model you want, but make sure it is loaded as this will not include your model.
01class ProductsController extends AppController{
02    var $name = 'Products';
03 
04    /**
05     * @var Product
06     */
07    var $Product;
08 
09    /**
10     * @var EmailComponent
11     */
12    var $Email;
13}
You will notice that NetBeans enables auto-complete even in comments. Just hit Ctrl + Space when writing the name of any class. This will work with associated models in a model class.
Now you will notice that your CakePHP work is much more easier, but we still have auto-complete is not working in views. This problem can be solved using NetBeans feature which enables us to define a variable's type using comments. I call this feature var-doc or variable type notice. Just write a comment like this before any variable. You even can make this with $this variable to define the class you are working with as View class.
1/* @var $this View */
2/* @var $html HtmlHelper */
3/* @var $javascript JavascriptHelper */
4.....
This way you can have auto-complete support for your views and helpers included in views and you will notice that NetBeans also has support for your classes in comments.
At last the only thing that I couldn't have support for is behaviors methods which can be called from a model's instances if you now a way to do this please tell me. But we are waiting for more from NetBeans which has introduced too much for us, PHP Developers.

Tuesday, May 1, 2012

How to fix ubuntu high CPU temperature

There is a common problem with some laptop owners  with Ubuntu OS installed on their laptops to have high CPU heat issues. Especially after heavy load of applications running (especially 3D/Multimedia/Webcam applications), CPU temperature may rise through 70-80 celcius degrees. And that is a real dangerous and critical situation for hardware health.  Dynamic CPU function at Ubuntu or the lack of hardware driver support that manufacturers share for Linux is causing that issue according to talks at Ubuntu Community Forums.

There are lots of complains about that situation and most of em were reported to Ubuntu developers but unfortunately yet Ubuntu has not shared any fix against that. Fortunately there is a solution for that.


Well this does not guarantee solving your heat issues but at least this solution worked for a lot of users (check Ubuntu community forums).

First of all be sure about that your laptop fans and components are cleaned because most of the cpu heat issues are related with that. 

If you think that Ubuntu is the reason that causes the problem then you can try this solution. It worked for my Dell Inspiron N5010 laptop.

1) To monitor you cpu and other other components, install lm-sensors;
Open the terminal.

sudo apt-get install lm-sensors
After that ;
sensors-detect 
and type Y to all questions, so it is going to install some modules to your kernel.

By typing sensors you can monitor your components.

2) Instal six important modules to fix your heat issue;

Open the terminal.
sudo gedit /etc/modules
Then add those lines save the editor and close;
#added to fix heat issue
battery
ac
thermal
processor
acpi-cpufreq
cpufreq-userspace


3) Restart Ubuntu and check the temperature values again by sensors command.

4) Try updating your hardware drivers. (Especially graphics card drivers)

If this does not solve your problem, avoid using Ubuntu till a solution for that appears. Because it may cause unfixable damages to your hardware.

How to add wordpress Uploader to plugins

WordPress has a nice media uploader dialog that it uses on the editor pages. Now wouldn’t it be nice if you could use it to handle image uploads for part of a plugin or theme you’re writing? Maybe you want to add an easy way to change the logo in a theme? A simple “Upload Image” button would work quite well for that, wouldn’t it?
It’s fairly simple to implement, providing you already have a bit of experience with the WordPress API.
The first step is to prepare your HTML. Put it wherever the code for your admin page is. You want to have a text input for the image URL, and a button that will launch the uploader dialog.

<tr valign="top">

<th scope="row">Upload Image</th>

<td><label for="upload_image">

<input id="upload_image" type="text" size="36" name="upload_image" value="" />

<input id="upload_image_button" type="button" value="Upload Image" />

<br />Enter an URL or upload an image for the banner.

</label></td>

</tr>
Now that the easy part is out of the way, it’s time to start making it do something. You need to enqueue some scripts and styles. Here’s an example function to show how it’s done:


function my_admin_scripts() {


wp_enqueue_script('media-upload');


wp_enqueue_script('thickbox');


wp_register_script('my-upload', WP_PLUGIN_URL.'/my-script.js', array('jquery','media-upload','thickbox'));


wp_enqueue_script('my-upload');


}



function my_admin_styles() {


wp_enqueue_style('thickbox');


}


if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {


add_action('admin_print_scripts', 'my_admin_scripts');


add_action('admin_print_styles', 'my_admin_styles');

}
We need the media-upload and thickbox scripts for starters, as well as jQuery, which is already included. Then we have to register and enqueue our own JavaScript file, my-script.js, which will handle the media uploader functionality. We also need to load the thickbox stylesheet in the next function.
The if (…) block ensures that the scripts and styles will only be included if the user is on a specific admin page. If you look at your plugin’s (or theme’s) admin page, the URL should have a ?page=some_string at the end. Substitute my_plugin_page for that string.
Now for the part the actually invokes the uploader: the JavaScript. This will go in the my-script.js file we included earlier.


jQuery(document).ready(function() {




jQuery('#upload_image_button').click(function() {


 formfield = jQuery('#upload_image').attr('name');


 tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');


 return false;


});




window.send_to_editor = function(html) {


 imgurl = jQuery('img',html).attr('src');


 jQuery('#upload_image').val(imgurl);


 tb_remove();


}




});
The first click() event listener opens a ThickBox dialog when the “Upload Image” button is clicked, and loads the uploader page inside it. It also stores the name of the URL input field in a variable, for later use.
The second function overrides the send_to_editor() function in the media-upload script. This is probably the most important part. When the “Insert into Post” button is clicked in the uploader dialog, this function fires. It collects the URL of the image that was uploaded, dumps it into the awaiting form field, and closes the ThickBox dialog.
That’s it. Providing everything went according to planned, you should have a form field that will either accept an arbitrary image URL, or allow a user to upload one on the spot.