温馨提示×

Debian LAMP服务器部署指南

小樊
35
2025-11-09 04:39:56
栏目: 云计算

Debian LAMP Server Deployment Guide

The LAMP stack (Linux, Apache, MySQL/MariaDB, PHP) is a popular open-source web development platform. This guide walks through deploying a LAMP server on Debian, covering system preparation, component installation, configuration, and security hardening.

1. Update System Packages

Before installing any software, update your system’s package list and upgrade existing packages to their latest versions. This ensures compatibility and security.

sudo apt update && sudo apt upgrade -y

2. Install Apache Web Server

Apache is the most widely used web server for Debian. Install it using apt, start the service, and enable it to launch at boot.

sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2

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

3. Install MariaDB/MySQL Database

Debian uses MariaDB (a MySQL fork) as the default database server. Install it and run the mysql_secure_installation script to enhance security (e.g., set a root password, remove anonymous users).

sudo apt install mariadb-server -y
sudo mysql_secure_installation

After setup, log in to MariaDB to verify:

sudo mysql -u root -p

Create a database and user for your application (replace placeholders with secure values):

CREATE DATABASE my_database;
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

4. Install PHP and Essential Extensions

PHP is the scripting engine for dynamic content. Install PHP and common extensions (e.g., mysql for database connectivity, gd for image processing).

sudo apt install php libapache2-mod-php php-mysql php-cli php-gd php-mbstring php-curl php-xml -y

Restart Apache to load PHP modules:

sudo systemctl restart apache2

Test PHP by creating an info.php file in the web root (/var/www/html):

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Visit http://your_server_ip/info.php to confirm PHP is working.

5. Configure Apache for PHP

Ensure Apache can handle PHP files by modifying the default virtual host configuration. Edit the file and enable .php file handling:

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following inside the <VirtualHost *:80> block (under <Directory /var/www/html>):

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Save the file, then restart Apache:

sudo systemctl restart apache2

6. Secure the Server

a. Configure UFW Firewall

Allow HTTP (port 80) and HTTPS (port 443) traffic to enable web access:

sudo apt install ufw -y
sudo ufw allow 'Apache Full'
sudo ufw enable

b. Harden MySQL Security

  • Use strong passwords for all database users.
  • Restrict remote root login (disable in mysql_secure_installation).
  • Regularly back up databases using mysqldump.

c. Keep Software Updated

Regularly run sudo apt update && sudo apt upgrade -y to patch vulnerabilities.

7. Optional: Install phpMyAdmin for Database Management

phpMyAdmin provides a web-based interface for managing MySQL/MariaDB. Install it and configure Apache:

sudo apt install phpmyadmin -y

During installation, select Apache as the web server and configure a database for phpMyAdmin. Access it via http://your_server_ip/phpmyadmin.

8. Optional: Enable HTTPS with Let’s Encrypt

Use Certbot to obtain a free SSL certificate and redirect HTTP to HTTPS for secure connections:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

Follow the prompts to secure your domain. Certbot will automatically configure HTTPS and redirect traffic.

By following these steps, you’ll have a fully functional LAMP server on Debian, ready to host dynamic web applications. Adjust configurations (e.g., PHP versions, database settings) based on your application requirements.

0