溫馨提示×

LNMP集群搭建方案

小樊
33
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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女