debian

Debian LAMP如何安装PHP

小樊
35
2025-09-23 11:07:29
栏目: 编程语言

Step 1: Update System Packages
Before installing any components, ensure your Debian system’s package list is up to date and upgrade existing packages to their latest versions. Run the following commands in the terminal:

sudo apt update && sudo apt upgrade -y

This step prevents compatibility issues and ensures you’re working with the most stable software versions.

Step 2: Install Apache Web Server
The LAMP stack requires a web server to serve PHP pages. Install Apache (the most common choice for Debian) using:

sudo apt install apache2 -y

Start the Apache service and enable it to launch on system boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Verify Apache is running by visiting your server’s IP address in a browser— you should see the default Apache welcome page.

Step 3: Install MySQL Database Server
PHP often interacts with databases, so install MySQL (or its drop-in replacement, MariaDB) to store application data. For Debian, MariaDB is recommended due to its compatibility and active maintenance:

sudo apt install mariadb-server -y

Start the MariaDB service and enable it:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Run the security script to harden your installation (e.g., set a root password, remove anonymous users, disable remote root login):

sudo mysql_secure_installation

Follow the on-screen prompts to complete the setup.

Step 4: Install PHP and Essential Extensions
The core of the LAMP stack is PHP. Install PHP along with modules for Apache integration and database connectivity (MySQL). For Debian’s default repositories (as of 2025), use:

sudo apt install php libapache2-mod-php php-mysql -y

After installation, restart Apache to load the PHP module:

sudo systemctl restart apache2

This ensures Apache can handle .php files correctly.

Step 5: Verify PHP Installation
Create a test PHP file in Apache’s default document root (/var/www/html) to confirm PHP is working. Run:

sudo nano /var/www/html/info.php

Add the following code to the file:

<?php phpinfo(); ?>

Save the file and exit the editor. Open a browser and navigate to http://your_server_ip/info.php. You should see a detailed PHP information page (e.g., version, loaded extensions, configuration settings). This confirms PHP is installed and accessible via Apache.

Step 6: Install Additional PHP Extensions (Optional but Recommended)
Depending on your application (e.g., WordPress, Laravel), you may need extra PHP modules. Common extensions include:

Install them using:

sudo apt install php-curl php-gd php-mbstring php-xml php-zip -y

After installation, restart Apache to apply the changes:

sudo systemctl restart apache2

These extensions are widely used in web applications and prevent errors related to missing functionality.

Step 7: Configure PHP Settings (Optional)
You can customize PHP behavior by editing its configuration file. For Debian with Apache, the main configuration file is located at /etc/php/8.2/apache2/php.ini (adjust the version number if you installed a different PHP version, e.g., 7.4).

Common settings to modify include:

After making changes, save the file and restart Apache:

sudo systemctl restart apache2

These adjustments optimize PHP for your application’s needs.

Troubleshooting Tips

0
看了该问题的人还看了