Steps to configure a PHP development environment on Linux
Jun 30, 2025 am 01:57 AMTo set up a PHP development environment on Linux, install PHP and required extensions, set up a web server like Apache or Nginx, test with a PHP file, and optionally install MySQL and Composer. 1. Install PHP and extensions via package manager (e.g., sudo apt install php php-mysql php-curl php-mbstring). 2. Install Apache (sudo apt install apache2) or Nginx and PHP-FPM (sudo apt install nginx php-fpm), then configure and restart the server. 3. Test by creating an info.php file in /var/www/html/ and accessing it via browser. 4. Optionally install MySQL (sudo apt install mysql-server) and secure it, then install Composer for dependency management using the official installer.
Setting up a PHP development environment on Linux is straightforward, especially if you're using a popular distro like Ubuntu. The key is to install the right tools and configure them properly so you can start building and testing PHP applications locally.

Install PHP and Required Extensions
The first step is getting PHP installed on your system. Most Linux distributions have PHP in their package repositories, so you can use the built-in package manager.

On Ubuntu or Debian-based systems, run:
sudo apt update
sudo apt install php
This installs the core PHP package. However, most projects require additional extensions like php-mysql
, php-curl
, or php-mbstring
. You can install them individually:

sudo apt install php-mysql php-curl php-mbstring
Make sure to install the extensions that match your project’s needs — it’s better to install them early than to debug issues later.
Set Up a Web Server (Apache or Nginx)
PHP alone isn’t enough — you need a web server to run PHP scripts through a browser.
For Apache:
Install Apache with:
sudo apt install apache2
Then restart Apache after installing PHP to ensure everything loads correctly:
sudo systemctl restart apache2
Your PHP files should go into /var/www/html/
by default.
For Nginx:
Install Nginx and PHP-FPM:
sudo apt install nginx php-fpm
Then configure Nginx to handle PHP requests. Open your site config file (e.g., /etc/nginx/sites-available/default
) and make sure it includes:
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; }
Restart Nginx afterward:
sudo systemctl restart nginx
Both servers work well, but Nginx tends to be more lightweight and efficient for high-traffic setups.
Test Your Setup with a Simple PHP File
Once everything is installed, test your environment to confirm it's working.
Create a simple PHP file:
sudo nano /var/www/html/info.php
Add the following content:
<?php phpinfo(); ?>
Save and exit, then visit http://localhost/info.php
in your browser. If you see the PHP info page, everything is configured correctly.
If not, check:
- That the web server is running (
systemctl status apache2
ornginx
) - That the file is placed in the correct directory
- That PHP modules are enabled if needed
You can delete this file afterward since it exposes system details.
(Optional) Install MySQL and Composer for Full Stack Development
If your project uses a database, installing MySQL or MariaDB is a good idea:
sudo apt install mysql-server
Secure it with:
sudo mysql_secure_installation
Then create a database and user for your project.
Also, install Composer, the PHP dependency manager:
- Download with:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- Run the installer:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Now you can use composer init
or install packages like Laravel or Symfony easily.
That’s basically all you need to get a basic PHP development environment up and running on Linux. It’s not complicated, but each step has small details that can trip you up if skipped.
The above is the detailed content of Steps to configure a PHP development environment on Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on

To determine the strength of the password, it is necessary to combine regular and logical processing. The basic requirements include: 1. The length is no less than 8 digits; 2. At least containing lowercase letters, uppercase letters, and numbers; 3. Special character restrictions can be added; in terms of advanced aspects, continuous duplication of characters and incremental/decreasing sequences need to be avoided, which requires PHP function detection; at the same time, blacklists should be introduced to filter common weak passwords such as password and 123456; finally it is recommended to combine the zxcvbn library to improve the evaluation accuracy.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

There are two ways to create an array in PHP: use the array() function or use brackets []. 1. Using the array() function is a traditional way, with good compatibility. Define index arrays such as $fruits=array("apple","banana","orange"), and associative arrays such as $user=array("name"=>"John","age"=>25); 2. Using [] is a simpler way to support since PHP5.4, such as $color
