CentOS下的LNMP(Linux+Nginx+MySQL+PHP)环境自定义配置主要涉及Nginx、MySQL、PHP三大组件,以下是具体步骤及注意事项:
Nginx的自定义配置可通过修改主配置文件或添加自定义配置文件实现,核心步骤如下:
/usr/local/nginx/conf/nginx.conf
(编译安装)或/etc/nginx/nginx.conf
(yum安装)。可通过nginx -t
命令验证配置文件路径。worker_processes
(如worker_processes auto;
);http
块中添加gzip on;
压缩响应,或配置proxy_cache
加速动态请求;/usr/local/nginx/conf/vhost/
(编译安装)或/etc/nginx/conf.d/
(yum安装)下创建.conf
文件(如my_site.conf
),内容示例:server {
listen 80;
server_name example.com;
root /usr/local/nginx/html/my_site;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
vhost
或conf.d
目录,需在主配置文件的http
块中添加include vhost/*.conf;
或include conf.d/*.conf;
。nginx -t
检查语法,无误后用nginx -s reload
重载配置。MySQL的自定义配置需通过修改my.cnf
文件实现,核心步骤如下:
/etc/my.cnf
、/etc/mysql/my.cnf
或/usr/local/mysql/my.cnf
(编译安装)。可通过mysql --help | grep "my.cnf"
查找优先级最高的路径。[mysqld]
section下添加或修改参数,常见优化项示例:[mysqld]
character_set_server = utf8mb4 # 支持emoji
collation_server = utf8mb4_unicode_ci
innodb_buffer_pool_size = 1G # InnoDB缓冲池大小(建议为物理内存的50%-70%)
max_connections = 200 # 最大连接数
query_cache_size = 64M # 查询缓存大小(MySQL 8.0+已移除)
systemctl restart mysqld
(CentOS 7+)或service mysqld restart
(CentOS 6)使配置生效。SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
,确认参数已修改。PHP的自定义配置可通过修改php.ini或添加自定义.ini文件实现,核心步骤如下:
php --ini
查找Loaded Configuration File
;yum安装的PHP默认路径为/etc/php.ini
。upload_max_filesize = 64M # 上传文件大小限制
post_max_size = 64M # POST数据大小限制
max_execution_time = 300 # 脚本最大执行时间(秒)
memory_limit = 256M # 内存限制
display_errors = Off # 生产环境关闭错误显示
default_charset = "utf-8" # 默认字符集
systemctl restart php-fpm
(CentOS 7+)或service php-fpm restart
(CentOS 6)使配置生效。info.php
文件(<?php phpinfo(); ?>
),访问该文件查看配置是否生效。www-data
或nginx
)、MySQL(mysql
)、PHP-FPM(www-data
或nginx
)用户对项目目录、配置文件有正确访问权限(如chown -R www-data:www-data /usr/local/nginx/html/my_site;
)。cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
),避免配置错误导致服务无法启动。max_connections
)可通过SET GLOBAL
命令动态修改,但重启后会失效,需写入my.cnf
永久生效。通过以上步骤,可灵活自定义CentOS LNMP环境的配置,满足不同项目的需求。