Understanding the Difference Between php-cli and php on Ubuntu

Understanding the Difference Between php-cli and php on Ubuntu
Photo by Ben Griffiths / Unsplash

When setting up PHP on an Ubuntu system, you may come across different packages like php-cli and php. These can be confusing at first, especially if you're new to the world of PHP and Linux. In this article, we’ll explore the differences between these two packages, what they are used for, and when to choose one over the other. We'll also discuss a few other points you should consider when installing PHP on Ubuntu.

What is php-cli?

The package php-cli refers to the PHP Command Line Interface. This version of PHP is designed to be used directly in the command line or terminal. It allows you to run PHP scripts without the need for a web server. For example, if you want to execute a PHP script that doesn’t require a browser or web server interaction, you would use the php-cli package.

Common uses of php-cli include:

  • Running cron jobs or scheduled tasks that require PHP.
  • Executing standalone scripts that don’t interact with the web.
  • Development and testing scripts directly in the terminal.

To install php-cli, you can use:

sudo apt install php-cli

This command installs only the necessary components for running PHP in the terminal, making it a lightweight choice if you don't need web server integration.

What is php?

The php package is a more comprehensive PHP runtime that includes the PHP CLI, but also installs additional components like PHP-FPM (FastCGI Process Manager) for web server integration. This is the default package for most PHP installations that will be used in conjunction with a web server, such as Apache or Nginx.

By installing php, you get:

  • The PHP CLI, for running PHP scripts in the terminal.
  • PHP-FPM, which is necessary for serving PHP content on a web server. PHP-FPM handles incoming requests from a web server like Nginx or Apache and processes the PHP code accordingly.
  • Other modules and dependencies for full PHP functionality, including support for various libraries and extensions.

To install the php package, you can run:

sudo apt install php

This command installs a complete PHP environment suitable for both command-line and web applications.

When to Use php-cli vs. php

  1. Use php-cli if:
    • You are only working with command-line scripts and have no need for a web server.
    • You are running background processes, cron jobs, or simple scripts that do not require a web interface.
  2. Use php if:
    • You need a complete PHP environment with the ability to handle web requests.
    • You plan to integrate PHP with a web server like Apache or Nginx.
    • You require access to additional PHP modules and configurations that come with the full PHP package.

Other Considerations

While understanding the difference between php-cli and php is important, here are a few additional points to consider when managing PHP on Ubuntu:

  • PHP Versioning: Different PHP packages may come in different versions. Ubuntu repositories may provide a specific version of PHP, but if you need a different version (e.g., PHP 7.4, PHP 8.0, etc.), you might have to install from an external repository like Ondřej Surý's PHP PPA. Use sudo add-apt-repository ppa:ondrej/php to install newer PHP versions.
  • PHP Modules: Both php-cli and php may come with essential modules, but if you’re working on web applications, you might need additional modules like php-mysql, php-xml, php-curl, etc. For these, you can install them separately. For example:
sudo apt install php-mysql php-xml php-curl
  • Web Server Integration: If you're running a web application and need to serve PHP pages, make sure that PHP-FPM is properly configured with your web server. For example, if you're using Nginx, you would configure it to use PHP-FPM for processing PHP requests.
  • Security: Be cautious with PHP versions in production environments. Always ensure that your PHP installation is up-to-date, as older versions may have known security vulnerabilities. Consider using PHP security advisories and patch management tools to keep your system secure.

Finally

The main difference between php-cli and php on Ubuntu boils down to what you need PHP for. If you’re only using the command line and need a lightweight package to run scripts, php-cli is the way to go. If you’re building web applications that require a full PHP runtime with web server integration, then the php package is what you’ll want to install.

Make sure to consider your project requirements, PHP version needs, and whether you’re working in a command-line or web environment when choosing which package to install. With this knowledge, you'll be able to make an informed decision and set up PHP in the most efficient way for your use case.

Support Us