登录
首页 >  文章 >  linux

LNMP集群搭建攻略及方案

时间:2025-04-13 09:47:23 298浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《LNMP集群搭建方案》,聊聊,我们一起来看看吧!

LNMP集群搭建方案

LNMP架构(Linux、Nginx、MySQL、PHP)是构建高性能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,将 usergroup 修改为 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 "" > /usr/share/nginx/html/info.php
  2. 访问测试页面: 在浏览器中访问服务器公网IP地址/info.php,如果显示PHP信息页面,则表示LNMP集群搭建成功。

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

以上就是《LNMP集群搭建攻略及方案》的详细内容,更多关于的资料请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>