Linux is a popular choice for PHP development due to its open-source nature, system stability, and security. Its powerful command-line tools and strong community support make problem-solving easier for developers. Additionally, Linux is widely used on servers, so developing on Linux helps better simulate production environments and improve development efficiency.
Start by choosing a Linux distribution that fits your needs, such as Ubuntu, CentOS, or Debian. The installation process is usually guided with either a graphical interface or command line prompts. Follow the instructions to complete the setup and ensure the system boots correctly.
After installation, it is recommended to update all system packages to ensure you have the latest versions, enhancing security and compatibility. Run the following command in the terminal:
sudo apt update && sudo apt upgrade
Installing PHP on Linux is straightforward. Use this command to install the core PHP components, Apache server module, and MySQL extension:
sudo apt install php libapache2-mod-php php-mysql
This command sets up the basic PHP development environment for you.
Composer is the standard tool for managing PHP project dependencies. Follow these steps to install it:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '9f6018a5b3b32e6c68372d061eac0cebf6a52f8c83999c4f4331ff3ac5f4219c2c56c4c7b4bfa0d7ee0adc1e747c9d48') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
After installation, check if Composer is installed successfully by running:
composer --version
It is recommended to use a powerful Integrated Development Environment (IDE) like VS Code or PHPStorm, and install PHP-related plugins to significantly improve coding efficiency and experience.
To better manage multiple projects, configuring Apache virtual hosts is advised. Create a virtual host configuration file:
sudo nano /etc/apache2/sites-available/yourproject.conf
Add the following content to the file:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/yourproject ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Save the file, then enable the site and reload Apache:
sudo a2ensite yourproject.conf sudo systemctl reload apache2
Create a file named info.php in your project directory with the following content:
<?php phpinfo(); ?>
Access http://localhost/info.php in your browser. If the PHP info page appears, your environment is successfully configured.
This article provided a complete walkthrough for setting up a PHP development environment on Linux, covering system updates, PHP and dependency installation, Composer setup, Apache virtual host configuration, and environment testing. Following these steps allows you to quickly build a stable and efficient development platform to support your PHP projects.