Monthly Archives: August 2015

Change loopback interface IP address

First of all – I know that is not a good idea, and the world will end if for some reason I want to change our beloved home address.
But for a second there, just think that there is situations when you need this.

In my case I wanted to test some different IP’s against database – and that is the easiest way to give myself any IP address I want, from any country I want, while running the script on the same machine.

Actually it is pretty straight forward solution – but everywhere on the internet when someone ask this a bunch of people are starting to suggest some other solutions – adding another interface, telling them they don’t need to do that etc.
Here is how to assign any IP address to the loopback interface in linux.

You will need root permissions, or just run the commands with sudo. Also this is tested on Ubuntu

For example we gonna use the IP address 46.10.11.185
First we need to change hosts file with:

sudo nano /etc/hosts

Just change your home(127.0.0.1) with 46.10.11.185 in the beginning of the file, so it is going to look something like this:

46.10.11.185 localhost
127.0.0.1 ivan-T430

Then just use ifconfig to change the lo interface IP address with:

sudo ifconfig lo 46.10.11.185

That’s all. Now your local web server will see you as the IP you want.

This is potentially dangerous, and might even make your system unstable.
Make sure you revert all changes and you put back the correct lo IP!

Find and debug slow PHP scripts.

This is very useful trick when you are having site/script that is loading slow, but you are not sure what is the issue. In my case that was leading to extremely slow TTFB(Time to first byte) – around 15 seconds.

To do this we will have to enable slowlog, in my case I am using php5-fpm on Ubuntu, so I need to look for this config file –

 /etc/php5/fpm/pool.d/www.conf 

And look for something like this:

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow

I want to log the slow request in

/var/log/php5/slow.log

So first of all I need to create that file, and the parent dir php5, if it doesn’t exists with

mkdir /var/log/php5
touch /var/log/php5/slow.log

And then add the fallowing values to our fpm config file –

slowlog = /var/log/php5/slow.log
request_slowlog_timeout = 8s
request_slowlog_timeout

Will determine how long before script will be logged. In my case anything that takes more than 8 seconds will be logged, of course you can use any other value.

Restart php5-fpm so the changes take effect

service php5-fpm restart

And then you can monitor the log in real time while browsing the site:

tail -f /var/log/php5/slow.log

For me the problem was malware injected in footer.php in one of the WordPress sites:

root@linuxvps:~# tail -f /var/log/php5/slow.log 
script_filename = /var/www/site.info/public_html/index.php
[0x00007f9bcb861fe0] curl_exec() /var/www/site.info/public_html/wp-content/themes/2014/footer.php(23) : assert code(1) : eval()'d code(1) : eval()'d code:14
[0x00007f9bcb861ed8] getContents() /var/www/site.info/public_html/wp-content/themes/2014/footer.php(23) : assert code(1) : eval()'d code(1) : eval()'d code:36
[0x00007f9bcb861df0] +++ dump failed