Current Location: Home> Latest Articles> Complete Guide to Installing and Using PHP on Linux

Complete Guide to Installing and Using PHP on Linux

gitbox 2025-08-04

In modern web development, PHP remains a powerful and widely-used server-side scripting language. Combined with the reliability and performance of Linux, it becomes a robust solution for building dynamic websites and web applications. This guide covers everything you need to install, configure, and optimize PHP on a Linux environment.

Why Use PHP on Linux

Running PHP on a Linux server comes with numerous benefits:

  • High Performance: Linux handles concurrent requests efficiently and is suitable for running high-load PHP applications.
  • Security: Linux’s built-in user and permission system provides strong security for PHP applications.
  • Open-source Support: The open-source community offers a wide range of resources and tools for PHP on Linux.

Update the System

Before installing PHP, it’s best to update your system packages:

sudo apt update
sudo apt upgrade

Install PHP and Common Extensions

If you're using a Debian-based system like Ubuntu, install PHP with useful extensions using:

sudo apt install php php-mysql php-xml php-mbstring

After installation, confirm the PHP version:

php -v

Configure PHP Settings

To meet the needs of different applications, you'll often need to edit PHP's configuration file. The php.ini file is usually located in /etc/php/7.X/cli/, where 7.X should match your installed version.

Edit the file using:

sudo nano /etc/php/7.X/cli/php.ini

For example, change memory and upload limits like so:

memory_limit = 256M
upload_max_filesize = 10M

Running PHP on Linux

There are different ways to run PHP code on a Linux server:

Using PHP’s Built-in Server

PHP provides a simple built-in web server for development purposes. From your project directory, run:

php -S localhost:8000

Then open http://localhost:8000 in your browser to view your site.

Using PHP with Apache or Nginx

In production environments, it's recommended to run PHP alongside Apache or Nginx. For example, to enable PHP in Apache, use:

sudo apt install libapache2-mod-php
sudo systemctl restart apache2

Debugging and Optimizing PHP

To improve development efficiency, consider using Xdebug for debugging and profiling.

Install it with:

sudo apt install php-xdebug

Then enable debugging by adding the following to your php.ini file:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1

Conclusion

Using PHP on Linux gives developers access to a powerful, flexible, and secure environment for building web applications. With the help of this guide, you should now be able to install, configure, and efficiently use PHP in your Linux setup for development or production.