Ubuntu中LNMP(Linux+Nginx+MySQL+PHP)的核心配置文件及作用如下:
/etc/nginx/nginx.confinclude指令引入站点配置文件。/etc/nginx/sites-available/{站点名}.conf
listen指定端口(如80/443),server_name设置域名或IP。root指定网站根目录,index设置默认访问文件。location块配置proxy_pass转发请求到后端服务器,upstream定义后端服务器组。location ~ \.php$块中通过fastcgi_pass连接PHP-FPM,需指定套接字路径(如unix:/var/run/php/php7.4-fpm.sock)。/etc/mysql/mysql.conf.d/mysqld.cnf
bind-address控制监听IP(127.0.0.1仅本地访问,0.0.0.0允许远程),port设置端口(默认3306)。innodb_buffer_pool_size调整InnoDB缓冲池大小,max_connections设置最大连接数。log_bin和server-id,从服务器通过CHANGE MASTER TO指定主服务器信息。/etc/php/{version}/fpm/php-fpm.confpid(进程ID文件路径)、error_log(错误日志路径)等。/etc/php/{version}/fpm/pool.d/www.conf
pm设置进程模式(dynamic动态调整),pm.max_children控制最大子进程数。request_terminate_timeout设置请求超时时间,listen指定监听套接字或端口。/etc/php/{version}/cli/php.ini(命令行)或/etc/php/{version}/fpm/php.ini(FPM模式)
display_errors关闭错误显示(生产环境),log_errors开启日志记录。memory_limit调整内存限制,upload_max_filesize限制上传文件大小。fastcgi_pass将PHP请求转发至PHP-FPM进程,需确保两者监听的套接字或端口一致。http块中使用upstream定义后端服务器组,支持轮询(默认)、加权轮询等策略。server_name避免主机头攻击,限制敏感目录访问(如location ~ ^/(wp-admin|config))。sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak)。sudo nginx -t测试Nginx配置语法,sudo systemctl reload nginx重启服务。sudo systemctl restart mysql。sudo systemctl restart php{version}-fpm。参考来源: