Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

搭建前準備

1. 三台獨立主機:

nginx   : 192.168.1.105
php-fpm : 192.168.1.105
mysql   : 192.168.1.103

2. 相關源碼包:nginx-1.10.2、php-5.6.28、mariadb-5.5.53。

3. 開發環境與相依套件:openssl-devel、pcre-devel、zlib-devel;套件組 Development Tools、Server Platform Development。

第一台主機:Nginx

1. 解壓源碼:

tar -xf nginx-1.10.2.tar.gz

2. 建立編譯暫存路徑:

mkdir -pv /var/cache/nginx

3. 進入目錄執行 configure:

./configure --prefix=/usr/local/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/cache/nginx/client_temp \
  --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
  --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
  --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
  --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
  --user=nginx --group=nginx \
  --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-threads --with-stream \
  --with-stream_ssl_module --with-http_slice_module \
  --with-file-aio --with-http_v2_module

4. make && make install。

5. 啟動 nginx,瀏覽器能正常顯示即無問題。

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

6. 配置 nginx 支援 php-fpm,在 /etc/nginx/nginx.conf 寫入:

server {
    listen 80;
    server_name www.a.com;
    location / {
        root /test/html;
        index index.html index.htm index.php;
    }
    location ~ \.php$ {
        root /test/html;
        fastcgi_pass 192.168.1.105:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /www/test1/$fastcgi_script_name;
        include fastcgi_params;
    }
}

第二台主機:PHP

1. 解壓 php-5.6.28 並進入目錄執行:

./configure --prefix=/usr/local/php56 \
  --with-mysql --with-openssl --with-mysqli \
  --enable-mbstring --with-png-dir --with-jpeg-dir \
  --with-freetype-dir --with-zlib --with-libxml-dir=/usr \
  --enable-xml --enable-sockets --with-mcrypt \
  --with-config-file-path=/etc/php56 \
  --with-config-file-scan-dir=/etc/php56.d \
  --with-bz2 --enable-fpm

其中 --enable-fpm 是關鍵選項。

2. make && make install。

3. 設定檔相關操作:

cp php.ini-development /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm   # 加入系統服務
chkconfig --add php-fpm
chkconfig --list php-fpm
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

注意:複製 php-fpm 到 /etc/rc.d/init.d/ 時,該檔案可能沒有執行權限,導致加入服務失敗,自行 chmod +x 即可解決。

4. 修改 php-fpm 設定:調整 pid 路徑、程序屬主與監聽位址。

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰 CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰 CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

這幾項是服務能否正常啟動的關鍵,其餘設定此處不再贅述。

5. 啟動服務並確認 9000 埠已監聽:

service php-fpm start
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

6. 建立動態資源目錄 /www/test1 並放入 index.php 測試。

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

此為請求靜態資源

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

此為請求動態資源

第三台主機:MariaDB

1. 建立使用者與群組:

groupadd mariadb
useradd -r -g mariadb -s /sbin/nologin mariadb

2. 安裝相依:

yum -y install gcc gcc-c++ cmake ncurses libxml2 libxml2-devel bison bison-devel

3. 編譯配置:

cmake . \
  -DMYSQL_UNIX_ADDR=/tmp/mariadb.sock \
  -DSYSCONFDIR=/etc \
  -DMYSQL_TCP_PORT=3306 \
  -DEXTRA_CHARSETS=all \
  -DMYSQL_USER=mariadb \
  -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb \
  -DMYSQL_DATADIR=/mydata/data \
  -DWITH_XTRADB_STORAGE_ENGINE=1 \
  -DWITH_FEDERATEDX_STORAGE_ENGINE=1 \
  -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  -DWITH_MYISAM_STORAGE_ENGINE=1 \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system \
  -DWITH_LOBWRAP=0 \
  -DDEFAULT_CHARSET=utf8 \
  -DDEFAULT_COLLATION=utf8_general_ci

4. make && make install,這一步較慢,請耐心等待。

5. 調整目錄權限:

mkdir -pv /mydata/data
chown -R mariadb:mariadb /mydata/data
cd /usr/local/mariadb
chown -R :mariadb ./*
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰 CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

6. 初始化資料庫並複製設定檔:

scripts/mysql_install_db --user=mariadb --datadir=/mydata/data/
cp mysql.server /etc/rc.d/init.d/mysqld
cp my-large.cnf /etc/my.cnf

7. 在 /etc/my.cnf 的 mysqld 段加入:

datadir = /mydata/data
innodb_file_per_table = ON
skip_name_resolve = ON

8. 加入系統服務並啟動:

chkconfig --add mysqld
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

若啟動失敗,修改 /etc/rc.d/init.d/mysqld 第 311 行:

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

再編輯 /etc/my.cnf,於 mysqld 段加入:

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

並補上一個模組:

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

最後把 /usr/local/mariadb 的屬主屬組改為 mariadb:

chown -R mariadb:mariadb /usr/local/mariadb/

若有其他錯誤,查看 /var/log/mariadb_error.log 對症下藥。

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

在第二台主機寫一段 PHP 連線 MySQL 的程式測試:

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

至此三台主機各自配置完成,LNMP 環境可正常運行。

延伸實作

一、為 PHP 主機安裝 XCache 加速

編譯安裝 xcache-3.2.0,另需 php-devel:

/usr/local/php56/bin/phpize      # 為 PHP 新增一個模組
./configure --enable-xcache --with-php-config=/usr/local/php56/bin/php-config
make && make install
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

把紅框內的字串複製備用,然後:

mkdir -pv /etc/php56.d
cp xcache.ini /etc/php56.d
vim /etc/php56.d/xcache.ini

注意:此處建立的目錄必須與編譯 PHP 時的 --with-config-file-scan-dir 一致,否則 PHP 找不到 xcache.ini,XCache 就不會生效。

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

重載 php-fpm 後重新請求頁面即可看到效果:

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

二、安裝 WordPress

使用 wordpress-4.7-zh_CN.zip,解壓後在 nginx 與 php 兩台各放一份,位置為各自虛擬主機 root 所定義的目錄。

CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

在 PHP 主機上:

cp wp-config-sample.php wp-config.php
vim wp-config.php
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰 CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰

三、安裝 phpMyAdmin

使用 phpMyAdmin-4.6.5.2-all-languages.zip,同樣放到各自對應路徑,然後修改 PHP 主機設定:

cp config.sample.inc.php config.inc.php
vim config.inc.php
CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰 CentOS 6.5 源碼編譯搭建分散式 LNMP:三台獨立主機實戰
Copyright © Linux教程網 All Rights Reserved