在Debian上定制LNMP(Linux+Nginx+MySQL/MariaDB+PHP)功能,可按以下步骤操作,结合实际需求调整配置:
更新系统
sudo apt update && sudo apt upgrade -y
安装Nginx
sudo apt install nginx
sudo systemctl start nginx && sudo systemctl enable nginx
安装数据库(MySQL/MariaDB)
sudo apt install mariadb-server
sudo mysql_secure_installation # 设置root密码及安全选项
my.cnf
。安装PHP及扩展
sudo apt install php-fpm php-mysql php-gd php-curl php-mbstring
sudo apt install php-redis php-pdo
配置PHP-FPM
编辑/etc/php/<version>/fpm/pool.d/www.conf
(<version>
为PHP版本号,如7.4
):
listen = /run/php/php7.4-fpm.sock # 确保与Nginx配置一致
listen.owner = www-data
listen.group = www-data
重启服务:
sudo systemctl restart php7.4-fpm
配置Nginx支持PHP
编辑站点配置文件(如/etc/nginx/sites-available/default
):
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 与PHP-FPM监听路径一致
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
测试配置并重启Nginx:
sudo nginx -t && sudo systemctl restart nginx
数据库主从复制(集群场景)
/etc/mysql/my.cnf
,设置server-id=1
、log-bin=mysql-bin
,并重启MySQL。server-id=2
、relay-log=mysql-relay-bin
,并执行CHANGE MASTER TO
命令指向主服务器。sudo systemctl start mysql-slave
。SSL证书配置(HTTPS支持)
使用Let’s Encrypt免费证书:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
虚拟主机配置
创建自定义虚拟主机文件(如/etc/nginx/sites-available/example.com.conf
):
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
启用配置:sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
,然后重启Nginx。
性能优化
http
模块中添加proxy_cache_path
配置。php.ini
设置memory_limit=512M
。my.cnf
中设置query_cache_type=1
。防火墙配置
使用UFW允许HTTP/HTTPS流量:
sudo ufw allow 'Nginx Full'
sudo ufw deny 3306/tcp # 禁止数据库远程访问(生产环境建议限制IP)
日志管理
/var/log/nginx/access.log
、/var/log/nginx/error.log
。php.ini
设置error_log=/var/log/php_errors.log
。服务监控
htop
监控系统资源:sudo apt install htop
。systemctl
查看服务状态:sudo systemctl status nginx mysql php7.4-fpm
。/var/www/html/info.php
:<?php phpinfo(); ?>
http://服务器IP/info.php
,确认PHP信息页面正常显示,且数据库连接正常。根据实际需求,可进一步调整Nginx的负载均衡、数据库分片或PHP的OPcache等高级功能。