Hello! 欢迎来到小浪云!


LNMP集群搭建方案


avatar
小浪云 2025-04-09 11

LNMP集群搭建方案

LNMP架构Linux、Nginx、mysqlphp)是构建高性能Web应用的流行技术。本文将指导您逐步搭建LNMP集群。

一、准备工作

在开始之前,请确保已完成以下步骤:

  1. 关闭防火墙和SELinux: 这对于集群的正常运行至关重要。执行以下命令

    systemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config reboot
  2. 更新系统软件包: 使用以下命令更新系统:

    yum update -y

二、安装nginx

  1. 安装依赖: 安装Nginx所需的依赖库:

    yum install -y pcre-devel zlib-devel openssl-devel
  2. 下载并解压Nginx: 下载指定版本的Nginx源码并解压到 /usr/local/src/ 目录:

    wget http://nginx.org/download/nginx-1.15.8.tar.gz tar -zxvf nginx-1.15.8.tar.gz -C /usr/local/src/ cd /usr/local/src/nginx-1.15.8
  3. 配置和编译安装: 使用以下命令配置并编译安装Nginx,并指定安装路径及相关参数:

    ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --user=nginx --group=nginx --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-threads --with-stream --with-stream_ssl_module make && make install
  4. 创建Nginx用户: 创建并设置Nginx运行用户和用户组:

    useradd nginx chown -R nginx:nginx /usr/local/nginx
  5. 启动并设置开机自启动: 启动Nginx服务并将其设置为开机自启动:

    systemctl start nginx systemctl enable nginx

三、安装mysql

  1. 安装MariaDB: 安装mariadb数据库服务器

    yum install -y mariadb-server mariadb systemctl start mariadb systemctl enable mariadb
  2. 安全配置: 执行MySQL安全配置向导:

    mysql_secure_installation

四、安装PHP

  1. 安装PHP和扩展: 安装PHP及其必要的扩展:

    yum install -y php php-fpm php-mysql php-gd
  2. 配置PHP-FPM: 修改PHP-FPM配置文件 /etc/php-fpm.d/www.conf,将 user 和 group 修改为 nginx

  3. 启动并设置开机自启动: 启动PHP-FPM服务并将其设置为开机自启动:

    systemctl start php-fpm systemctl enable php-fpm
  4. 配置Nginx: 编辑 /etc/nginx/conf.d/default.conf 文件,在 location ~ .php$ 块中添加以下配置:

    fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params;
  5. 重启Nginx: 重启Nginx服务使配置生效:

    systemctl restart nginx

五、验证安装

  1. 创建测试文件: 创建一个PHP测试文件:

    echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
  2. 访问测试页面:浏览器中访问服务器公网IP地址/info.php,如果显示PHP信息页面,则表示LNMP集群搭建成功。

注意: 以上步骤仅为LNMP集群搭建的基本流程,生产环境中需要考虑负载均衡、高可用性、安全加固等更多因素。 请根据实际情况进行调整和优化。

相关阅读