Friday, January 29, 2016

nginx - 404 on images (only some images)

By default, nginx automatically decodes the url you send to it.

So , if you are looking for an image which has it's real name as the url encoded version of it, you will get error 404 - because nginx will try to grab the decoded version of the filename!

I haven't found a good workaround for this ATM. Checking.

UPDATE:

On my local server, every image is getting loaded properly.

On my remote server, only some of the images are getting loaded properly (others are 404).

Here's the final cause:


The images which returning error 404 where those for which urlencode(name)!=name!
During the transfer to my remote server, my picture names got url encoded somehow and that caused all the trouble!

Nice linux trick for finding the opened log files by a process

Looking for the location of your error.log (nginx) ?


Check this post by slash4:

You can use lsof (list of open files) in most cases to find open logfiles without knowing the configuration:
Example:
find PID of httpd (same concept applies for nginx and other programs):
# ps axu |grep httpd
...
root     17970  0.0  0.3 495964 64388 ?        Ssl  Oct29   3:45 /usr/sbin/httpd
...
Then search for open logfiles using lsof for the PID:
# lsof -p 17970 |grep log
httpd   17970 root    2w   REG             253,15     2278      6723 /var/log/httpd/error_log
httpd   17970 root   12w   REG             253,15        0      1387 /var/log/httpd/access_log
you can read a little more here

nginx - empty logs on ubuntu

nginx won't either reload nor restart unless the complete ngix configuration is 100% correct!

It will simply ignore the nginx -s reload command (and possibly throw an error line).


For example, when I was trying to reload the config file, I've encountered this line:

nginx: [emerg] getgrnam("web") failed in /etc/nginx/nginx.conf:7



It is a very important message, because when I fixed it (by changing user web with user web www-data in my nginx.conf , line 7), nginx reloaded without any errors!


UPDATE:

It's not enough that nginx -s reload goes ok.

You also should make sure that nginx -t is successful.

Also, full settings reload is achieved by nginx -s reload && nginx -s reopen (reopens log files).

In case your log files are not being generated, it's probable that nginx -t fails for some reason. Since, it's just an  interpreter, any further action in setup will be discarded.

Monday, January 25, 2016

Writing MySQL statements - be careful with spaces between function name and parenthessis when calling a function

In MySQL , this produces syntax error:

.... GROUP_CONCAT (field)....

while this is correct:
.... GROUP_CONCAT(field)....
:)

Vagrant / Trellis [Windows Host] - Replace NFS with rsync

If you are using Vagrant under Windows Host, you had the opportunity of witnessing the super slow performance of VirtualBox's default sync folder.

For example, if you are running a nginx/WordPress installation inside your guest VM, you are likely to face various timeouts, errors 504,502 and etc. Your page loading speed could be 10+ seconds.

This can be cured by switching to NFS. You can find details how to switch to NFS in my previous posts. It's just a matter of specifiying :type:"nfs" in your Vagrantfile and reloading the Vagrant vm - vagrant reload.

Unfortunately, if you are looking to run 2+ Vagrant boxes using NFS in Windows 8, you will probably get the "Access denied" error for the second box ( on vagrant up command).

After quite a few tips I've tried, I still haven't succeeded in running 2 boxes with NFS shares in Windows. If you switch one of the VMs to SMB or VirtualBoxes sync folders, you will get it working, but perfomance for that machine will be very poor.

I found another approach which can give your Vagrant Windows-hosted box a great performance boost:
Use the rsync for sync folder sychronization!

-You will probably have to download it as a part of CYGWIN package.

-Also, you will probably have to change the mounting point of environment variable cygdrive in your /etc/fstab located inside the CYGWIN installation directory.

-If you receive "errror in rsync protocol (12)", check this:
https://github.com/mitchellh/vagrant/issues/6702
You will probably have to delete / comment the lines 77-79 in $VAGRANT_HOME\embedded\gems\gems\vagrant-1.8.0\plugins\synced_folders\rsync\helper.rb.

-There are some rumors that you should use Windows console or PoweShell instead of Git Bash for rsync to work (not confirmed, probably due to the version of ssh having precedence in each console)

From my experience, rsync is even faster than NFS, as it relies on the local VMs filesystem. However, make sure you understand that (in the default edition) it's a 1-way protocol (from host -> VM). Also, make sure you use vagrant rsync-auto command to enable live transfers of edited files from host to the Vagrant VM.

This way, you can have 2 Vagrant boxes running blazingly fast at the same time. Hope it helps someone :)


Wednesday, January 20, 2016

Restart needed for git bash to reload new entries added to Window's PATH variable

If you modify your PATH, by adding or removing a Windows Environment Variable, you'll probably have to restart your git bash console before it recognizes the change.

Pointing Magnifier - A cool new software

Here's a useful software application for all super users!

Too much clicking and focus on targeting ever smaller command buttons can easily lead to fatigue.

Pointer Magnifier is here to rescue!

Here's the full link for this open source program made by talented fellows of University of Washington:

http://depts.washington.edu/aimgroup/proj/ptgmag/

Monday, January 18, 2016

Wordpress Plugin Development

Symptoms:
WP produces the following warning message upon plugin activation:
"The plugin generated ### characters of unexpected output during activation"

You can debug the actual characters by the help of this plugin:
https://wordpress.org/plugins/debug-plugin-activation-errors/

If the characters prove to be whitespaces, make sure you have no whitespaces after the final ?> tag.

You can find all ?> tags by issuing:

grep "?>" -R .

Sunday, January 17, 2016

PHP - Slow loading can be caused by file permission problems

Slow, especially very slow page loading times, can be caused by problems with file permssions.

For example, if your script is trying to use sessions, and the session.save_path is not accessible for current user, you are likely to experience ~10 second page loading time, followed by an error.

Google Chrome returning ERR_ICANN_NAME_COLLISION when trying to access Vagrant box

If you are getting ERR_ICANN_NAME_COLLISION error in your Chrome browser while trying to reach you website hosted in a Vagrant VM, here's what you can check:

-Check that your hosts file contains the VM hostname entry
-Check that Vagrant box is up and running
-Run : vagrant reload and try again to open your website in Chrome

Saturday, January 16, 2016

A few notes on running Trellis

If your hosts file is not filled with your hostnames, just try to run:
vagrant reload


VM boxes are user - dependant. Be sure to check the VM box list as the right user (the creator of the box).


A strong machine is required to use Trellis / Vagrant with relatively good speeds.

Another thing to check when considering Vagrant perfomance is : Firewall / Anti Virus live monitor on host system. Try to disable it for 15mins or so to see if it provides any speed improvements.


Super important tip - it can speed up your virtual machine by 10x:

Use NFS, especially if using Windows as host machine
More details here:
https://laracasts.com/discuss/channels/general-discussion/for-those-who-find-homesteadvagrantvirtualbox-slow-on-windows

Basically, it comes down to installing vagrant plugin winnfsd by issuing this:

vagrant plugin install vagrant-winnfsd

After that, just modify your Vagrant file, insert something like this:
    # EDIT:
    # Reorder folders for winnfsd plugin compatilibty
    # see https://github.com/GM-Alex/vagrant-winnfsd/issues/12#issuecomment-78195957
    #? settings["folders"].sort! { |a,b| a["map"].length <=> b["map"].length }

   # Register All Of The Configured Shared Folders
    settings["folders"].each do |folder|
      config.vm.synced_folder folder["map"], folder["to"], 
      id: folder["map"],
      :nfs => true,
      :mount_options => ['nolock,vers=3,udp,noatime']
    end
Another tip, the easiest way I found to control Vagrant is using git bash console in windows, run as Administrator.
-Replace php-fpm with HHVM if possible. It seems that php-fpm is not properly configured in basic trusty box. It provides significant speed up.

 -Increase php memory limit. For example, setting 196MB instead of 96MB makes the intermittent slow downs in page loading much less frequent! UPDATE 04/2017: It's very likely that ideal settings for VirtualBox NFS shared folder under Windows are: config.vm.synced_folder ".", "/vagrant", type: "nfs" For example, the setting above proved to be 10x faster than this: config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['rw', 'vers=3', 'tcp', 'fsc', 'actimeo=1'] If your Vagrant is still slow with NFS, make sure you try that setting.

Trellis/Vagrant problem - ansible-galaxy fails with : "fatal error - [FOLDER] not a git repository"

This is a tip for you if your vagrant up command, or  ansible-galaxy install command fails with something like this:

fatal error: [FOLDER]/.git  not a git repository


In my case , I have tracked this error to this line in roles/nginx/tasks/main.yml file of my Vagrant (Trellis) folder:

- name: Grab h5bp/server-configs-nginx
  git:
    repo: "https://github.com/h5bp/server-configs-nginx.git"
    dest: "{{ nginx_path }}/h5bp-server-configs"

    force: yes

Replaced it with this:


- name: Clean h5bp/server-configs-nginx
  command: rm -rf "{{ nginx_path }}/h5bp-server-configs"


- name: Grab h5bp/server-configs-nginx
  command: git clone "https://github.com/h5bp/server-configs-nginx.git" {{ nginx_path }}/h5bp-server-configs
 

Thursday, January 14, 2016

CURL , HTTP status code 404 - due to whitespaces

Always make sure your CURL requests are using a properly URL encoded address.

Otherwise, any whitespace will break your URL, and you will get ERROR 404 back.

This problem can be confirmed if you look in the body of 404 message. It will state that the file was not found, and you will see that the filename in the body is not the same as one you intended to receieve.

Check the similar issue with nginx 404 - due to automatic decoding of image urls.

Wednesday, January 13, 2016

Impact of hardware components on MySQL / WordPress performance

Recently, I had problems with a slow running WordPress site on my old Linux machine.
TTFB time was around 10 seconds on average!

On the other computer, the same page has about 1 second TTFB.

So I decided to put MySQL on fire , and test it in 3 different environments.

I used this command to perform tests on 3 systems (all 3 Ubuntu):

Command 1 (preapre mysql test db):

sysbench --test=oltp --oltp-table-size=100 --mysql-user=[user] --mysql-password=[pass] --mysql-db=test1 prepare

Command 2 (run the test)

sysbench --test=oltp --oltp-table-size=100 --mysql-user=[user] --mysql-password=[pass] --mysql-db=test1  --oltp-test-mode=complex --num-threads=16 run


Here are  the results:

Machine #1 (Virtual Machine on a Lenovo i7 with 8GB SSD cache and 4GB RAM):

Avg. request:
17 ms

Total test time:
11.7 seconds



Machine #2 (Host Machine on a Core2Duo 2GB 2GB RAM ):

Avg. request:
99 ms

Total test time:
63 seconds


Machine #3 (Vagrant Virtual Machine on a Core2Duo 2GB 2GB RAM ):

Avg. request:
292 ms

Total test time:
182 seconds



It's clearly indicated that hardware is a very important factor in WordPress / MySQL performance.


Edit:
Upon transferring Vagrant to i7 machine, results improved, but not that much as expected. In case of the WP, theme seems to be the bottle neck for performance.

What provided a significant improvement on my i7  was replacing Vagran'ts php-fpm with HHVM.
It could be that nginx / php-fpm combination is poorly configured in the default vagrant box?

Also, adding more CPUs and memory to Vagrant box helps.

UPDATE:
For Vagrant performance, and associated nginx performance, the disk access speed is crucial. In case of non-NFS Shared Folders , site load speed is reduced 10x times!! NFS makes it fly!

Fix for: Slow debugging ASP.NET MVC 4+ application in web browser

This tip is regarding ASP.NET MVC 4 development using Visual Studio 2013. If you have your browser freeze or slow down, intermittently, here's what you need to check:

Try to disable your Windows Defender or Antivirus Program for 10 minutes.

If that's the cause of your slowdown, your web application will fly again. :)

Tuesday, January 12, 2016

WordPress WSOD , no errors in log files - STRATEGY

Here's a situation I had today:

-nginx + php-fpm server

-Server returns status: 200

-No error in server logs (logical consequence of nginx returning code 200)

Symptoms:
-WSOD (blank page) while loading my WordPress homepage

-I can access admin

-All other , non-wp pages work normally.

After entering the wp-admin, I have found that my main theme is missing from the themes list. Child theme (which is the active one) was there.

However, WP was unable to find the main theme, and it just returned blank error page. No errors at all.

Allegedly, this can be also generated if it cannot find entire wp-content folder.

Hope this saves a bit of your time ! :)

Some tips on debugging PHP WSODs

Blank screen with no server errors in the log files is what we call PHP White Screen Of Death (WSOD).

It's not always easy to find the root cause, but here are a few tips, and a great forum thread for more information:

- Check your code @ statements
- Try to do a simple exit('test'); and move it down the code lines
- Check if  your WSOD is maybe caused by infinite loop
- Check for die() and exit() statements in the code

Helpful thread:
http://stackoverflow.com/questions/1944105/php-produces-a-completely-white-page-no-errors-logs-or-headers

Monday, January 11, 2016

Ubuntu - error running ansible - ImportError: No module named runner

Ansible is a popular tool managing complex deployment environments in Linux society. It makes your life as developer much easier. However, from time to time, especially if you install lots of additional software on your Ubuntu machine, you might encounter this problem while running ansible:

 ImportError: No module named runner

This error can be verified by issuing the following command in your Ubuntu terminal:

ansible --version

Traceback (most recent call last):
  File "/usr/bin/ansible", line 36, in <module>
    from ansible.runner import Runner
ImportError: No module named runner

If you see such an error , you can try to locate the ansible python package in /usr/local/lib/ and remove it.
For example:

rm -rf /usr/local/lib/ansible-2.0.0-py2.6.egg

Here's the original thread (very nicely written by Dan Swartz):

Monday, January 4, 2016

Default root password for Ubuntu Vagrant Box

If you are wondering what's the default root password for root user in Ubuntu 12 Vagrant box, here it is:

vagrant

Sunday, January 3, 2016

Ubuntu - phpStorm won't start anymore

Today, I had a problem with PHP Storm IDE not starting on my Linux machine. After clicking the application icon, nothing would happen. After reading some forums, I stumbled upon this procedure:

Run Terminal

Find all "java" or "phpstorm" processes and terminate them

Re-run PHP Storm

It worked in my case. Hope it can help you as well!

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutte...