LNMP是指Linux系统下Nginx、MySQL/MariaDB和PHP这种网站服务器架构,通常用于中小型网站服务。在Debian系统上优化配置LNMP环境,可以参考以下步骤和建议:
sudo apt update
sudo apt upgrade -y
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo apt install mariadb-server mariadb-client -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
sudo apt install php-fpm php-mysql -y
编辑 /etc/php/7.x/fpm/pool.d/www.conf
文件(根据你的PHP版本调整路径),找到以下行并取消注释:
listen = /run/php/php7.x-fpm.sock
重启PHP-FPM服务:
sudo systemctl restart php7.x-fpm
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
修改以下内容以支持PHP:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
保存并退出编辑器,然后测试Nginx配置并重启服务:
sudo nginx -t
sudo systemctl reload 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/log/nginx/error.log以减少日志记录对性能的影响。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
innodb_buffer_pool_size
,通常设置为系统内存的50%-80%。max_connections
以匹配服务器的处理能力。query_cache_size
和 query_cache_type
。cgroups
或 ulimit
来限制Nginx、PHP-FPM和MariaDB的资源使用,防止单个进程消耗过多资源。top
、htop
、vmstat
和 iostat
来监控系统资源使用情况,及时发现并解决性能瓶颈。iptables
或 ufw
来限制对服务器的访问,只允许必要的端口和服务通过。通过上述步骤,你可以在Debian系统上成功配置并优化LNMP环境。请根据你的具体需求和硬件配置进行调整。在进行任何重大更改之前,建议先在测试环境中验证更改的效果。