LNMP是指Linux、Nginx、MySQL和PHP组成的网站架构,常用于中小型网站服务。在Debian上配置LNMP环境时,以下是一些最佳实践:
安装Nginx
- 更新系统包列表:首先,确保你的系统包列表是最新的:
sudo apt update
- 安装Nginx:使用以下命令来安装Nginx:
sudo apt install nginx
- 启动并启用Nginx服务:
sudo systemctl start nginx sudo systemctl enable nginx
- 验证Nginx安装:你可以通过访问服务器的IP地址或域名来验证Nginx是否安装成功。
安装MySQL/MariaDB
- 安装MySQL或MariaDB:这里以MariaDB为例:
sudo apt install mariadb-server mariadb-client
- 启动并启用MariaDB服务:
sudo systemctl start mariadb sudo systemctl enable mariadb
- 运行安全脚本:以设置root密码和其他安全选项:
sudo mysql_secure_installation
安装PHP
- 安装PHP及其常用模块:
sudo apt install php-fpm php-mysql
- 配置PHP-FPM以使用Nginx:
- 编辑PHP-FPM配置文件:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
- 找到
listen
行,修改为:listen /run/php/php7.4-fpm.sock
- 保存并退出编辑器,然后重启PHP-FPM服务:
sudo systemctl restart php7.4-fpm
- 编辑PHP-FPM配置文件:
配置Nginx以处理PHP请求
- 编辑Nginx默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
- 修改以下内容:
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.4-fpm.sock; } location ~ /\.ht { deny all; } }
- 测试Nginx配置并重启服务:
sudo nginx -t sudo systemctl restart nginx
创建一个简单的PHP文件进行测试
- 创建一个简单的PHP文件:
sudo nano /var/www/html/index.php
- 添加以下内容:
- 保存并退出编辑器,然后在浏览器中访问你的服务器IP地址或域名,你应该能看到PHP信息页面。
优化LNMP性能
- Nginx优化:
worker_processes auto; worker_cpu_affinity auto; worker_rlimit_nofile 65535; http { sendfile on; sendfile_max_chunk 512k; 关闭压缩; fastcgi_read_timeout 300000; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 100000; server_tokens off; }
- 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;
- 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.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环境。