温馨提示×

LNMP集群搭建方案

小樊
53
2025-04-01 06:13:58
栏目: 编程语言

LNMP是指Linux、Nginx、MySQL和PHP的组合,这是一个非常流行的用于部署Web应用程序的技术栈。以下是LNMP集群搭建的基本步骤:

1. 安装前的准备

  • 关闭防火墙和SELinux

    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0
    vi /etc/selinux/config
    # 将 selinux=enforcing 改为 selinux=disabled,重启系统使设置生效
    
  • 更新系统

    yum update -y
    

2. 安装Nginx

  • 安装依赖包

    yum install -y pcre-devel zlib-devel openssl-devel
    
  • 下载并解压Nginx源码

    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
    
  • 配置并编译安装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
    
  • 创建运行Nginx的用户

    useradd nginx
    chown -R nginx:nginx /usr/local/nginx
    
  • 启动并设置Nginx开机自启动

    systemctl start nginx
    systemctl enable nginx
    

3. 安装MySQL

  • 安装MariaDB(或MySQL):

    yum install -y mariadb-server mariadb
    systemctl start mariadb
    systemctl enable mariadb
    
  • 配置MySQL

    mysql_secure_installation
    

4. 安装PHP

  • 安装PHP及其扩展

    yum install -y php php-fpm php-mysql php-gd
    
  • 修改PHP配置文件

    vi /etc/php-fpm.d/www.conf
    # 修改 user 和 group 为 nginx
    systemctl start php-fpm
    systemctl enable php-fpm
    
  • 配置Nginx以支持PHP

    编辑 /etc/nginx/conf.d/default.conf,在 location ~ \.php$ 部分添加以下内容:

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    
  • 重启Nginx

    systemctl restart nginx
    

5. 验证安装

  • 创建PHP测试文件

    echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
    
  • 访问测试文件

    在浏览器中访问服务器的公网IP地址/info.php,如果看到PHP信息页面,则说明LNMP环境搭建成功。

以上是LNMP集群搭建的基本步骤。对于生产环境,还需要考虑更多的优化和安全措施,例如配置负载均衡、SSL证书、监控和日志分析等。

0