LNMP是指Linux系统下Nginx、MySQL/MariaDB、PHP这种网站服务器架构。在Debian上优化LNMP环境可以从多个方面进行,包括性能、安全性、资源利用率等。以下是优化步骤和建议:
sudo apt update
sudo apt upgrade
sudo apt install nginx
sudo apt install mariadb-server mariadb-client
sudo apt install php-fpm php-mysql
sudo nano /etc/nginx/nginx.conf
/var/www/html
目录下创建info.php
文件,添加PHP代码。<?php
phpinfo();
?>
sudo nginx -t
sudo systemctl restart nginx
Nginx优化:
worker_processes
:设置为auto以自动适应CPU核心数。worker_cpu_affinity
:设置为auto以自动分配CPU亲和性。worker_rlimit_nofile
:设置为65535以允许更多的文件描述符。sendfile
:开启以启用sendfile系统调用。gzip
:关闭压缩以减少CPU使用。fastcgi_read_timeout
:设置为300000以增加FastCGI应用程序的响应时间。tcp_nodelay
:开启以提高I/O性能。server_tokens
:关闭以减少HTTP头信息的大小。keepalive_timeout
:设置为65以保持连接复用。keepalive_requests
:设置为100000以允许更多的保持活动请求。error_log
:设置为/var/logs/nginx/error.log以减少日志记录对性能的影响。PHP优化:
opcache.enable=1
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=10000
opcache.memory_consumption=256
opcache.save_comments=1
opcache.fast_shutdown=1
opcache.validate_timestamps=1
opcache.fast_shutdown=1
opcache.jit_buffer_size=128
MySQL优化:
innodb_buffer_pool_size
,通常设置为系统内存的50%-80%。max_connections
以匹配服务器的处理能力。query_cache_size
和query_cache_type
。Linux系统优化:
ulimit -n 65535
提高文件描述符限制。sysctl -w net.core.somaxconn=65535
sysctl -w net.ipv4.ip_local_port_range="1024 65535"
sysctl -w net.ipv4.tcp_fin_timeout="30"
sysctl -w net.ipv4.tcp_slow_start_after_idle="0"
sysctl -w net.ipv4.tcp_fastopen="3"
sysctl -w net.ipv4.tcp_syncookies="1"
sysctl -w net.core.netdev_max_backlog=65535
sysctl -w net.nf_conntrack_max=2097152
通过上述优化措施,可以显著提高Debian LNMP系统的性能,确保系统在高负载下的稳定性和响应速度。