How to Install PHP in Ubuntu

The Server-Side Execution Vector

If you are engineering a LAMP stack (Linux, Apache, MySQL, PHP) on a headless Ubuntu server to host a dynamic web application like WordPress or a custom Laravel framework, the HTML payload is mathematically insufficient. The OS kernel requires a dedicated interpreter to process dynamic backend logic, interface with the relational database, and compile the final HTML string before the Apache daemon fires it across the internet. You must instruct the Ubuntu package manager to inject the core PHP binaries (Hypertext Preprocessor) directly into the execution path.

How to Install PHP in Ubuntu

Canonical mathematically maintains highly stable, pre-compiled PHP binaries within the native `apt` registry. A standard installation requires injecting both the core language runtime and the specific module required for the web server (Apache or Nginx) to handshake with it.

1. Open your terminal application or connect to the server via SSH.
2. Phase 1: The Dependency Pre-Flight (Updating the Cache):
* Before injecting the scripting architecture, force the local package manager to mathematically synchronize its index.
* Type exactly:

sudo apt update

* Press Enter and authenticate with your `sudo` root password.
3. Phase 2: The Execution Trigger (The Installation for Apache):
* If you are running the Apache web server, you must install the `php` core and the `libapache2-mod-php` binding library.
* Type exactly:

sudo apt install php libapache2-mod-php php-mysql -y

* (Note: We include `php-mysql` as it is mathematically required if your PHP application needs to communicate with a MySQL database).
* Press Enter.
* The Mathematical Result: The OS calculates the storage (usually ~20MB), downloads the binaries, and automatically restarts the Apache daemon to load the new PHP module.
4. Phase 3: The Alternative Execution (For Nginx / FPM):
* If you are utilizing Nginx, it does not use a built-in module. It requires a dedicated background daemon known as PHP-FPM (FastCGI Process Manager).
* Type exactly:

sudo apt install php-fpm php-mysql -y

* Press Enter. You will then need to manually configure your Nginx server block to route `.php` requests to the FPM socket.
5. Phase 4: The Verification Handshake:
* To mathematically prove the core OS can now interpret PHP scripts.
* Type exactly:

php -v

* Press Enter. The terminal must output a string defining the runtime version, such as `PHP 8.1.2-1ubuntu2.14 (cli)`.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.