Steps to configure a PHP development environment on Linux
To 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)

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.

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

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.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.
