Documentation

Find documentation for our Joomla extensions on this page.

PHP-CLI and automatic conversion with JoomMedia

There is another method for running PHP scripts directly from your operating system shell or command line called php-cli (Command line interface).

If you have never heard about php-cli before, this is nothing to be ashamed of, after all web sites run within a webserver, so you can take for granted it supports PHP. Indeed it is even possible that you don't even have access to the command line of the server that is hosting your site. So, how come php-cli is important for JoomMedia users?

There are two important reasons why it is preferable to run certain processes via php-cli:

  • Unlike php running in a webserver, php-cli is usually configured to support long running processes. This makes it great for converting large media files.
  • It is suitable for scheduling periodic execution using CRON.

I hope it's clearer now why this is relevant in JoomMedia. Video and audio conversion processes are typically lengthy processes, so is synchronizing local media with a remote CDN. Using php-cli to execute these tasks improves their chances of completing successfully. They are also CPU intensive tasks, so for busy sites it makes sense to use php-cli to schedule them during low activity periods so not to negatively impact the server performance when it's more visited.

Getting down to business

In the rest of the article I'll tinker with php-cli from the Linux shell command. It is highly recommended that you go along and try the examples in your system yourself. Don't worry if your results differ from my example. If you can't access to the command line interface of your server (as in most shared server hostings), that doesn't mean that you can't use php-cli, so try to follow and understand the examples.

Finding php-cli

Perhaps the most disturbing fact about php-cli is that not only its location may vary in different servers, its name also does! The php-cli binary is usually called just "php", but can be also called php5, php-cli, or even php5-cli. A little trick I use to find the right name when unsure is to take advantage of shell autocompletion, by typing php and hitting the tab key, that shows me a list of known executables beginning with php. In my case:

# php
php         php-cgi     php-config  php-fpm     phpize

Among these alternatives, php is most probably the right one. Let's find its exact path by using the which shell command:

# which php
/usr/bin/php

The path /usr/local/bin/php is also a very common location, specially in servers with cpanel or where PHP was compiled from its source code.

If none of the names suggested by the autocomplete rings the bell you may also try the find command:

# find /usr -name "php*" -type executable

It will retrieve a list of executable binaries whose name begins by "php".

Beware that sometimes there may exist a binary called php that is not php-cli! How to know for sure? Try calling it with the "-v" flag:

# php -v
PHP 5.3.16 (cli) (built: Aug 17 2012 06:09:38)

In clearly shows that this is indeed the cli version of PHP we are looking for.

Installation

If you found php-cli following the tips above you can skip this section. Chances are is that if php is already running in your webserver php-cli is installed as well, but sometimes it needs to be installed separately. These packages will install in the most popular Linux distributions:

php-cli : Red Hat/CentOS/Fedora
php5-cli : Debian/Ubuntu

Setting JoomMedia

Once you are sure php-cli is installed in the server it's time so set its path in the JoomMedia configuration.

JoomMedia server settings

Now let's go to Configuration, select the Processing tab and click the Test Automatic Processing button. If you are lucky you will see the pop-up window below notifying you that php-cli is correctly setup and ready for duty.

test JoomMedia automatic processing

If not (or if you want to learn a few tricks more about php-cli), don't despair and read on!

Troubleshooting php-cli

Unfortunately some default PHP security settings may prevent the automatic conversion working out of the box, so it is important to know how to check and change the php-cli configuration. Something that confuses many webmasters is the fact that php-cli may, and indeed in most systems has, a different php.ini configuration file than the php handler running your website, that is the one you can see in the Joomla System Information, under the PHP Information tab. To check most relevant php-cli settings let's go back again to the Linux shell and run the following commands.

safe_mode

safe_mode was a creative way from the PHP developers to drive webmasters insane. Most of the time the settings were so safe that is was difficult to actually run complex scripts, so in the end this option was deprecated in PHP 5.3. That doesn't mean that still can't bite you! To check the value of safe_mode in php-cli run:

# php -i | grep safe_mode
safe_mode => Off => Off

safe_mode Off is good news. If On, you'll need to edit the php.ini as explained below to disable it.

exec()

Some system administrators may disable some unnecessary php functions that can be used by bad guys to do bad things in your servers. This is actually a good practice, except when you need that function to perform some work! For example, the exec() function used by JoomMedia to invoke operating system utilities like ffmpeg and qt-faststart. To check whether there are disabled functions run:

# php -i | grep disable_functions
disable_functions => no value => no value

If you see exec in the list below it means that it's execution is forbidden, so you need to remove that restriction in the php.ini.

max_execution_time

As we said above php-cli is mainly used for processes that take a long time, so we really don't want that our processes are prematurely interrupted:

# php -i | grep max_execution_time
max_execution_time => 0 => 0

0 means "unlimited" execution time, which is more than enough. A low value like 30 (seconds) will make the conversion script to be aborted before the actual conversion ends, so you'll surely want to push the value up.

php.ini

To find out what is the php-cli execution file run the following from the shell:

# php -i | grep php.ini
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

As you can see mine is /etc/php.ini, yours may differ. Notice that it is a system-wide configuration file, so it can only be edited by the administrator (root) user.

No shell access?

If you don't have ssh access to the server to try the commands above there are still a few tricks you can use. A favorite of mine is use the cron option in your hosting control panel to run the following script:

/path/to/php -i

Schedule it to a near time and it will mail the php-cli configuration to you. You just need to locate the important elements like safe_mode and disable_functions.

Conclusion

I hope this article has been informative and helpful for configuring JoomMedia automatic conversion. If you still experience problems after trying the suggestions here we'll be glad to help you in our support forum. Just make sure that provide the following info to get a faster solution (if unsure ask your provider or system administrator about them):

  • Is it a shared or VPS/dedicated server?
  • What's the full path of php-cli in your server
  • The output of the following commands:
    • which php
    • php -v
    • php -i | grep safe_mode
    • php -i | grep disable_function
Cron Job Starts

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.

Ok