To install a PHP code runner on an Ubuntu server, you must use the apt package manager. The simplest way is to install the base package, but it’s generally better to install some common modules to make PHP more useful.
First, it’s always good practice to update your package index to ensure you have the most up-to-date information.
sudo apt update
Install PHP
You can install the PHP runtime and its most common modules with a single command. The php package itself installs the basics, but adding other packages is very useful for most projects.
sudo apt install php php-cli php-mysql php-mbstring php-xml
- php: The main executor.
- php-cli: Allows you to execute PHP scripts directly from the command line (php my_script.php).
- php-mysql: Required for connecting PHP to a MySQL or MariaDB database.
- php-mbstring: Required for working with multibyte strings (non-ASCII characters).
- php-xml: Essential for processing XML documents
After the command completes, you can verify that PHP was installed correctly by running the following line:
php -v
If the installation was successful, you will see the PHP version in your terminal.