Moving on

I started to write my random notes on a dedicated site at TinyWP.com . I also run TinyWP.in to write about WordPress performance. So, I am unlikely to maintain this domain going forward. But, I will keep this as long as I can, as it helps myself to refer some of my old notes from time to time.

I might come back here and write something that doesn’t fit any of the other domains! Until, then, bye for now!

Find in PHP files

Often, I search for a function, most likely a wordpress function, within the PHP files. It is usually done within wordpress themes that contains a few other files too, particularly minified CSS file that results in seeing the entire minified CSS file in the output of the search result. This is pretty annoying to say the least.

Now, I started using an alias that finds only the PHP files and then pass the result to a grep command to find only what I really need. Here’s the alias…

alias findinphp='find * -type f -name '*.php' -print | xargs grep -inr'

I could have used the built-in -exec option too. However, it doesn’t play well when we search thousands of files. Moreover, using xargs is safe and faster.

PSI – How to Optimize Images

I have been using ewww for years. While running a Google PageSpeed Insights test for a client’s site, I noticed several images aren’t up to the mark. EWWW said it’s compressed. So, downloaded the optimized files from Google PSI and compared with the original images.

ImageMagick has a nice command line utility called identify that helps to find the “Q”uality of the image compression. To find it out, use the following command…

identify -format '%Q' imagename.jpg

Make sure your server has imagemagick. lol.

It looks like Google uses lossy compression with 85% of the original image quality. So, a loss of 15%.

Later, I noticed that it is a common query in recent times in the official PSI forums too. I am glad I am not alone. 🙂

macOS Sierra Errors and Fixes

Updating to a new OS has always been a pain for any OS. Mac is no different. A couple of days ago, updated the mac OS from OS X El Capitan (10.11) to macOS Sierra (10.12).

screen-shot-2016-10-26-at-6-25-45-pm

Mostly the update seemed okay. However, I did receive a few hiccups after the recent update. Here’s the list of errors that I received and how I solved it. Hope it helps someone else who come across similar error/s.

php config files

Sierra simply takes a backup of the previous /etc/php.ini into /etc/php.ini-previous and doesn’t install the new php.ini by itself. So, make sure you copy the new php.ini.

The same applies to /etc/php-fpm.conf file too.

locale settings – zsh

I use zsh as my primary shell. Not sure what went wrong with the default locale settings. I get somewhat similar error messages related to locale whenever I connect to a CentOS or Debian based servers via SSH.

To fix it, I appended the following line into ~/.zshrc

export LANG="en_US.UTF-8"
export LC_ALL=$LANG

Just for the records, here’s the error message on CentOS 5…

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = “UTF-8”,
LANG = “en_US.UTF-8”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = “UTF-8”,
LANG = “en_US.UTF-8”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = “UTF-8”,
LANG = “en_US.UTF-8”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).

Even though, the error message contains the word “bash”, it isn’t related to bash. 😛

ZSH – du – tab completion

Actually, an old issue has been fixed in macOS Sierra. Earlier, there was a bug that doesn’t allow tab completion for the command “du”. Now it works! YES!!! Thanks for whoever fixed it!

I will keep this post updated, if I come across any more errors and possible fixes!

PHP user.ini file

If you don’t know already, I am obsessed with optimising everything around me, including PHP. Today, I am going to share a tip to optimise PHP, particularly PHP-FPM. It assumes that you do not use “.user.ini” file or you never heard about it. 😉

As per the official docs for .user.ini file, by default, PHP scans each user directory for the file named “.user.ini” for every 5 minutes. So, it is much better than what Apache does to .htaccess files. Still, if you don’t use it, just disable it in the php.ini file. Just set “user_ini.filename” as empty in php.ini of your PHP-FPM process. To make it easier, the default php.ini file already contains the line commented out. So, uncommenting is enough to disable “.user.ini” functionality and make your server a bit more snappier!

Stay tuned for further optimisation tips!

Display Active Conf in php-fpm pool

Php-fpm pool configuration contains 400+ lines (for example in Ubuntu 16.04). In reality, we may use only about 20 lines or less than 5% of the file. The original configuration file contains lines that explains what each config does. It is good practice to have inline documentation.

In certain cases, we may want to know what lines are actually active. To find that please use the following command…

grep -v '^;\|^$' /path/to/fpm/pool.d/www.conf

What it does:

The ‘-v’ modifier excludes the pattern that immediately follows it.

The quoted string has two parts.

  1. ‘^;’ excludes any lines that do not start with a semi-colon.
  2. ‘^$’ excludes the empty lines

‘|’ modifier combines the above two parts. Finally, ‘\’ modifier is the escape character.

systemctl automatically restart service

References:

  1. Ensure systemd services restart on failure by Jon Archer
  2. php-fpm systemd unit with auto-restart by Juan Luis Boya García
  3. systemd Project Web Page

systemd is a system and service manager that runs as PID 1 and starts the rest of the system and services. The main command used to introspect and control systemd is systemctl.

Basically do the following to let a service restart automatically…

  • Edit /lib/systemd/system/name.service file
  • Insert “Restart=on-failure” under the section “Service”. Save the file
  • Execute the command… systemctl daemon-reload
  • Execute the command… systemctl restart name_of_the_service
  • (optional) try killing the process now

See https://wiki.archlinux.org/index.php/Systemd#Examples for more examples.

Save Screenshots Space on Mac

If you use mac OS and if you think you might run out of disk space at some point in the future (everybody does this), then here is a small (advanced) tip to help to optimise the space, especially if you take a lot of screenshots.

I was going through the random video on youtube and stumbled upon this gem…

You may know the commands used in it at http://snazzylabs.com/tutorial/five-advanced-tricks-for-mac-users/ . I am just going to explain the first tip. It basically uses the following command…

defaults write com.apple.screencapture type jpg

I didn’t want to simply type it down. So, I tried the following…

defaults read com.apple.screencapture

It didn’t show any results. It basically means the default value isn’t setup specifically. So, I executed the original command, tested if it really saved any space and type the following command to see, if I can go back to the default option.

defaults delete com.apple.screencapture

It did work. So, go ahead and do it yourself. You can thank me later!

nginx: [emerg] unexpected end of file, expecting “;” or “}” in filename:linenumber

I created a new vhost for a domain to serve static files. Here’s the config…

 

server {
    server_name static.example.com;

    root "/path/to/static.example.com/public;

    index index.html;

    access_log /var/log/nginx/static.example.com-access.log;
    error_log /var/log/nginx/static.example.com-error.log;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

It can’t get simpler than this. However, when I checked the configuration with `nginx -t`, it threw the following error…

nginx: [emerg] unexpected end of file, expecting ";" or "}" in /path/to/static.example.com/public:15

It had exactly 15 lines and the error happened exactly at the end of the file. I was too confused about this. Finally, I decided to check each character of this config. Turned out that I missed the matching quote around the `root` path.

Time to stop using quotes. Nginx can work without quotes too. However, quotes are necessary, if there is a space in the path.

It is easy to catch these kind of errors, if we turned on the syntax highlighting in my editor. While I did turn it on, the configuration was located in a specific path that the editor failed to apply the syntax highlighting on this particular file. Time to fine-tune it!

SSL – Links

SSL  can be tricky at times. Here are some of the useful links that may help install and configure SSL correctly on various applications and servers.

Mixed Content Warning:

Troubleshooting Mixed Content Warnings with HTTPS

SSL CA:

LetsEncrypt.org

StartSSL

WP Plugins:

Really Simple SSL