在 Debian 上配置 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 时,“最优”通常指 性能、稳定性、安全性和可维护性 的平衡。下面给你一套 生产环境推荐配置方案(Debian 12/11 通用),并解释每一步为什么这样配。
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget vim unzip
sudo dpkg-reconfigure tzdata
sudo apt install -y locales
sudo locale-gen en_US.UTF-8
sudo vim /etc/security/limits.conf
建议:
* soft nofile 65535
* hard nofile 65535
sudo apt install -y apache2 apache2-utils
sudo a2dismod autoindex status cgid
sudo a2enmod rewrite ssl headers expires
✅ 推荐:event MPM(高并发)
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
编辑:
sudo vim /etc/apache2/mods-available/mpm_event.conf
推荐配置(2核4G 示例):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
sudo vim /etc/apache2/conf-available/security.conf
ServerTokens Prod
ServerSignature Off
sudo a2enconf security
sudo apt install -y mariadb-server mariadb-client
sudo mysql_secure_installation
✅ 建议:
sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf
示例(4G 内存):
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_size = 0
query_cache_type = 0
max_connections = 150
重启:
sudo systemctl restart mariadb
sudo apt install -y php php-cli php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-gd php-opcache
✅ 推荐 Apache + PHP-FPM(比 mod_php 性能好)
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm
sudo systemctl restart apache2
sudo vim /etc/php/8.2/fpm/php.ini
推荐:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 128M
max_execution_time = 120
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
重启:
sudo systemctl restart php8.2-fpm
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache
✅ 自动续期:
sudo systemctl enable certbot.timer
sudo apt install -y ufw
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo apt install -y fail2ban
<Directory /var/www/html>
Options -Indexes
</Directory>
/var/www/
├── example.com/
│ ├── public_html/
│ └── logs/
虚拟主机示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>
| 方案 | 性能 | 稳定性 | 推荐 |
|---|---|---|---|
| Apache + mod_php | 中 | 中 | ❌ |
| Apache + PHP-FPM | 高 | 高 | ✅ |
| Nginx + PHP-FPM | 最高 | 高 | ✅(非 LAMP) |
| Apache MPM prefork | 低 | 中 | ❌ |
Debian 12
Apache 2.4 + mpm_event
PHP 8.2/8.3 + PHP-FPM
MariaDB 10.11
HTTPS (Let's Encrypt)
Fail2ban + UFW
OPcache 开启
如果你愿意,我可以:
直接告诉我你的 服务器规格 + 使用场景 即可。