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.
Running PHP on a Linux server comes with numerous benefits:
Before installing PHP, it’s best to update your system packages:
sudo apt update sudo apt upgrade
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
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
There are different ways to run PHP code on a Linux 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.
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
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
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.