There are two main ways to install PHP on Linux: using package manager installation and source code compilation and installation. For newbies or users who have no special requirements for the version, it is recommended to use a package manager to install it. For example, running sudo apt update and sudo apt install php on Ubuntu/Debian. On CentOS, you can first install the EPEL source, and then install it with yum. After the installation is completed, you can verify and install common extensions through php -v. If you need a specific version or custom function, you should choose source code compilation and installation. The specific steps include downloading the source code package, decompression, configuration (such as ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-mysqli), compilation (make) and installation (sudo make install). After installation, you also need to modify the php.ini configuration file setting time zone and other parameters, start the PHP-FPM service, and test whether the PHP module is successfully loaded and whether the running environment is normal by creating an info.php file. When encountering problems, you should prioritize checking the log to locate the cause and resolve it.
It is not difficult to install PHP on Linux. The key is to choose the appropriate installation method according to your system environment and needs. If you are using common distributions, such as Ubuntu, Debian or CentOS, you can basically do it quickly through the package manager. Of course, if you have specific version requirements or want to customize the configuration, you can also consider compiling from the source code.

Install PHP using package manager (recommended for beginners)
In most cases, using the system's package manager is the easiest way to do it. Taking Ubuntu/Debian as an example, you can do this:
- Update software source:
sudo apt update
- Install PHP:
sudo apt install php
- Check whether the installation is successful:
php -v
After the installation is completed, you can also install commonly used extensions as needed, such as php-mysql
, php-curl
, etc. The command is sudo apt install php-擴(kuò)展名
.

CentOS users can use yum
or dnf
:
- Install the EPEL source (if not installed):
sudo yum install epel-release
- Install PHP:
sudo yum install php
- Check the version to confirm the installation:
php -v
The advantage of this method is that it is easy to use and is suitable for scenarios where the development environment or no special requirements for the version.

Source code compilation and installation (suitable for customization)
If you need to use a specific version of PHP, or want to enable certain modules that are not enabled by default, you can consider downloading the source code from the official website to compile and install it.
The steps are roughly as follows:
- Download the source code package: go to php.net to find the version you need
- Unzip and enter the directory:
tar -zxvf php-xxxtar.gz && cd php-xxx
- Configuration parameters, for example:
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-mysqli
- Compile and install:
make && sudo make install
The key to this step is to select the parameters of configure, otherwise the functions may be missing or they cannot be run. It is recommended to check the options description in the official documentation first.
Configuration and testing
After installation, don't forget to make some basic configurations:
- Find the configuration file
php.ini
, usually in/etc/php/
or the path you specified - Modify some common settings, such as time zone
date.timezone = Asia/Shanghai
- If you use PHP-FPM, remember to start the service:
sudo systemctl start php-fpm
- Write a simple
info.php
file to test it: write<?php phpinfo(); ?>
content, and visit this page to see PHP running information, which means there is no problem
Sometimes you will find that the web page cannot be opened or the error is reported, mostly because of permission problems or not correctly connected to the web server (such as Nginx). At this time, you need to check the log.
Basically that's it. The installation process is not complicated, but some details are easy to ignore, such as path, service status, whether the extension is loaded, etc. When encountering problems, you can check the log first and then search for keywords. Generally, you can find a solution.
The above is the detailed content of PHP Installation 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)

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Avoid N 1 query problems, reduce the number of database queries by loading associated data in advance; 2. Select only the required fields to avoid loading complete entities to save memory and bandwidth; 3. Use cache strategies reasonably, such as Doctrine's secondary cache or Redis cache high-frequency query results; 4. Optimize the entity life cycle and call clear() regularly to free up memory to prevent memory overflow; 5. Ensure that the database index exists and analyze the generated SQL statements to avoid inefficient queries; 6. Disable automatic change tracking in scenarios where changes are not required, and use arrays or lightweight modes to improve performance. Correct use of ORM requires combining SQL monitoring, caching, batch processing and appropriate optimization to ensure application performance while maintaining development efficiency.

To build a flexible PHP microservice, you need to use RabbitMQ to achieve asynchronous communication, 1. Decouple the service through message queues to avoid cascade failures; 2. Configure persistent queues, persistent messages, release confirmation and manual ACK to ensure reliability; 3. Use exponential backoff retry, TTL and dead letter queue security processing failures; 4. Use tools such as supervisord to protect consumer processes and enable heartbeat mechanisms to ensure service health; and ultimately realize the ability of the system to continuously operate in failures.

Use subprocess.run() to safely execute shell commands and capture output. It is recommended to pass parameters in lists to avoid injection risks; 2. When shell characteristics are required, you can set shell=True, but beware of command injection; 3. Use subprocess.Popen to realize real-time output processing; 4. Set check=True to throw exceptions when the command fails; 5. You can directly call chains to obtain output in a simple scenario; you should give priority to subprocess.run() in daily life to avoid using os.system() or deprecated modules. The above methods override the core usage of executing shell commands in Python.

Using the correct PHP basic image and configuring a secure, performance-optimized Docker environment is the key to achieving production ready. 1. Select php:8.3-fpm-alpine as the basic image to reduce the attack surface and improve performance; 2. Disable dangerous functions through custom php.ini, turn off error display, and enable Opcache and JIT to enhance security and performance; 3. Use Nginx as the reverse proxy to restrict access to sensitive files and correctly forward PHP requests to PHP-FPM; 4. Use multi-stage optimization images to remove development dependencies, and set up non-root users to run containers; 5. Optional Supervisord to manage multiple processes such as cron; 6. Verify that no sensitive information leakage before deployment

PHP's garbage collection mechanism is based on reference counting, but circular references need to be processed by a periodic circular garbage collector; 1. Reference count releases memory immediately when there is no reference to the variable; 2. Reference reference causes memory to be unable to be automatically released, and it depends on GC to detect and clean it; 3. GC is triggered when the "possible root" zval reaches the threshold or manually calls gc_collect_cycles(); 4. Long-term running PHP applications should monitor gc_status() and call gc_collect_cycles() in time to avoid memory leakage; 5. Best practices include avoiding circular references, using gc_disable() to optimize performance key areas, and dereference objects through the ORM's clear() method.

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

Bref enables PHP developers to build scalable, cost-effective applications without managing servers. 1.Bref brings PHP to AWSLambda by providing an optimized PHP runtime layer, supports PHP8.3 and other versions, and seamlessly integrates with frameworks such as Laravel and Symfony; 2. The deployment steps include: installing Bref using Composer, configuring serverless.yml to define functions and events, such as HTTP endpoints and Artisan commands; 3. Execute serverlessdeploy command to complete the deployment, automatically configure APIGateway and generate access URLs; 4. For Lambda restrictions, Bref provides solutions.
