ubuntu

LNMP在Ubuntu上的性能测试方法

小樊
45
2025-07-25 13:24:02
栏目: 智能运维

LNMP是指Linux、Nginx、MySQL(MariaDB)和PHP这四种软件的组合,常用于搭建动态网站或服务器。在Ubuntu上配置和测试LNMP环境,可以按照以下步骤进行:

LNMP在Ubuntu上的安装

  1. 安装Nginx
sudo apt update
sudo apt install nginx
  1. 安装MySQL(MariaDB)
sudo apt install mysql-server

在安装过程中,系统会提示设置MySQL的root用户密码,请务必记住该密码,以便日后使用。

  1. 安装PHP及PHP-FPM
sudo apt install php-fpm php-mysql
  1. 配置Nginx支持PHP
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

注意:根据你的PHP版本,fastcgi_pass 行中的路径可能会有所不同。

  1. 重启Nginx和PHP-FPM服务
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm

性能测试

  1. 使用ApacheBench(ab)进行压力测试
sudo apt install apache2-utils
ab -n 1000 -c 10 http://your-domain.com/

这个命令会对你的网站进行1000次请求,每次请求10个并发连接,以测试服务器的性能。

在进行性能测试之前,建议先对服务器资源(如内存、CPU)进行监控,以确保测试结果的准确性。可以使用工具如 tophtop 来实时查看资源使用情况。

优化LNMP性能

worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
http {
    sendfile on;
    sendfile_max_chunk 512k;
    gzip off;
    fastcgi_read_timeout 300000;
    tcp_nodelay on;
    keepalive_timeout 65;
    keepalive_requests 100000;
    error_log /var/log/nginx/error.log;
    access_log off;
    server {
        proxy_socket_keepalive on;
        keepalive_requests 100000;
    }
}
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.jit=on;
opcache.jit_buffer_size=128M;
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
sysctl -w net.netfilter.nf_conntrack_max=2097152
sysctl -w net.netfilter.nf_conntrack_tcp_timeout_fin_wait=60
sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=60

通过以上步骤和优化,你可以对Debian LNMP环境进行压力测试,并根据测试结果进行相应的调优,以确保服务器在高并发情况下的稳定性和性能。

0
看了该问题的人还看了