根据DNS的A记录负载均衡web服务请求
做為論壇站點:有兩種類型的數據要處理:
| 1 2 | ?1、結構化的數據:??如:用戶名、用戶發表的評論等,這些可以存儲在關系型數據庫中: ?2、非結構化的數據:如:用戶上傳的附件。則存儲到文件系統中。 |
論壇的架構:
? ? ? ? ? 使用兩臺httpd服務器來服務用戶的訪問請求。使用DNS的A記錄做客戶訪問輪循到這兩臺服務器上。php做為httpd的模塊工作。
論壇架構如下圖所示:
一、搭建NFS文件共享服務器。
要點:保證192.168.60.99與192.168.60.40兩臺httpd服務器的進程能夠往共享文件系統(目錄)中寫數據。
1、創建共享目錄。
| 1 2 3 | [root@nfs?~]#?vim?/etc/exports [root@nfs?~]#?cat?/etc/exports /web?????192.168.60.0/24(rw,no_root_squash) |
說明:只要是:192.168.60.0網段的用戶都可以掛載使用NFS共享的目錄。
2、兩臺httpd服務器嘗試是否可以掛載NFS共享的目錄。
(1)、httpd A(192.168.60.99):服務器掛載 /web
查看NFS服務器共享的目錄(文件系統)
| 1 2 3 | [root@stu13?~]#?showmount?-e?192.168.60.88 Export?list?for?192.168.60.88: /web?192.168.60.0/24 |
掛載共享目錄
| 1 | [root@stu13?~]#?mount?-t?nfs?192.168.60.88:/web?/mnt/nfs/ |
查看是否掛載上。
| 1 2 | [root@stu13?~]#?mount??|?grep?"nfs[[:space:]]" 192.168.60.88:/web?on?/mnt/nfs?type?nfs?(rw,addr=192.168.60.88) |
(2)、httpd B(192.168.60.40):服務器也掛載:/web
查看NFS服務器共享的目錄(文件系統)
| 1 2 3 | [root@localhost?~]#?showmount?-e?192.168.60.88 Export?list?for?192.168.60.88: /web?192.168.60.0/24 |
掛載共享目錄
| 1 | [root@localhost?~]#?mount?-t?nfs?192.168.60.88:/web?/mnt/nfs/ |
查看是否掛載成功
| 1 2 | [root@localhost?~]#?mount?|?grep?"nfs[[:space:]]" 192.168.60.88:/web?on?/mnt/nfs?type?nfs?(rw,addr=192.168.60.88) |
3、創建使用NFS服務器共享文件系統(目錄)的用戶;
httpd A(192.168.60.99)與 httpd B(192.168.60.40) 服務器的: User、Group都使用:usenfs
也就是服務用戶請求的進程或線程的屬主與屬組是:usenfs. 由于用戶上傳附件的話,要往共享文件系統中寫數據。所以,我們一定要保證該用戶能夠往,共享服務器NFS的共享文件目錄中寫數據。
(1)、分別在httpd A與httpd B服務器中設置usenfs 用戶,用戶ID號為400.
| 1 2 | [root@stu13?~]#?id?usenfs uid=400(usenfs)?gid=400(usenfs)?groups=400(usenfs) |
| 1 2 3 | [root@localhost?~]#?useradd?-u?400?-r?usenfs [root@localhost?~]#?id?usenfs uid=400(usenfs)?gid=400(usenfs)?groups=400(usenfs) |
(2)、在NFS服務器上,也創建一樣的用戶(指的是id號一樣)且要求該用戶要有NFS共享出去的文件系統(/web)有讀寫執行權限。
| 1 2 3 | [root@nfs?~]#?useradd??-u?400?-r?usenfs [root@nfs?~]#?id?usenfs uid=400(usenfs)?gid=400(usenfs)?groups=400(usenfs) |
使用控制列表的文件授權
設置前:
| 1 2 3 4 5 6 7 8 | [root@nfs?~]#?getfacl?/web getfacl:?Removing?leading?'/'?from?absolute?path?names #?file:?web #?owner:?root #?group:?root user::rwx group::r-x other::r-x |
說明:
? ?從上述信息可知,usenfs用戶是無法往該目錄中創建文件的。
通過文件權限列表方法,授與某用戶權限。
| 1 2 3 4 5 6 7 8 9 10 11 | [root@nfs?~]#?setfacl?-m?u:usenfs:rwx?/web [root@nfs?~]#?getfacl?/web getfacl:?Removing?leading?'/'?from?absolute?path?names #?file:?web #?owner:?root #?group:?root user::rwx user:usenfs:rwx group::r-x mask::rwx other::r-x |
說明:
? ? 由于usenfs用戶有rwx權限,當以該用戶的身份發起創建文件的進程就可以往該目錄中創建文件了。
二、創建LAMP平臺
說明:這里使用php做為模塊的方式跟httpd交互。數據庫mariadb與NFS服務器在同一臺主機192.168.60.88工作。
1、編譯httpd服務器軟件:
兩臺httpd服務器都使用一樣的設置進行源碼編譯安裝。
方法如下:
需要的軟件包:
| 1 2 3 | httpd-2.4.9.tar.bz2 apr-util-1.4.1.tar.bz2 apr-1.4.6.tar.bz2 |
說明:httpd-2.4版本要求:apr的版本一定在1.4以上。
查看系統的apr庫的版本,
| 1 2 3 4 | [root@stu13?admin]#?ldconfig?-v?|?grep?apr ????????libaprutil-1.so.0?->?libaprutil-1.so.0.3.9 ????????libgstdataprotocol-0.10.so.0?->?libgstdataprotocol-0.10.so.0.25.0 ????????libapr-1.so.0?->?libapr-1.so.0.3.9 |
說明:由于apr的版本不適合httpd-2.4,所以要升級apr庫。升級方式:rpm包;源碼包。這里選擇源碼包。
安裝:apr-1.4.6.tar.bz2 和 apr-util-1.4.1.tar.bz2
| 1 2 3 4 | [root@stu13?admin]#?tar?-xf?apr-1.4.6.tar.bz2 [root@stu13?admin]#?cd?apr-1.4.6 [root@stu13?apr-1.4.6]#?./configure?--prefix=/usr/local/apr-1.4 [root@stu13?apr-1.4.6]#?make?&&?make?install |
| 1 2 3 4 | [root@stu13?admin]#?tar?-xf?apr-util-1.4.1.tar.bz2 [root@stu13?admin]#?cd?apr-util-1.4.1 [root@stu13?apr-util-1.4.1]#?./configure?--prefix=/usr/local/apr-util-1.4?--with-apr=/usr/local/apr-1.4 [root@stu13?apr-util-1.4.1]#?make?&&?make?install |
編譯安裝httpd服務器
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@stu13?admin]#?tar??-xf?httpd-2.4.9.tar.bz2 [root@stu13?httpd-2.4.9]#?./configure?\ --prefix=/usr/local/httpd-2.4.9?\???安裝程序存放的位置 --sysconfdir=/etc/httpd-2.4.9?\?????配置文件存放的位置 --enable-so?\???????????????????采用共享模塊的方式編譯安裝httpd。可以靈活加載卸載模塊。 --enable-ssl?\??????????????????????啟用ssl安全傳輸功能 --enable-cgi?\??????????????????????啟用cgi功能 --enable-rewrite?\??????????????????啟用重定向功能 --with-zlib?\???????????????????????支持壓縮功能。傳輸傳輸時,啟用該功能可以節省帶寬 --with-pcre?\???????????????????????加密 --with-apr=/usr/local/apr-1.4?\?使用新安裝的apr,由于沒有導出該庫。所以要指定在哪個目錄 --with-apr-util=/usr/local/apr-util-1.4?\ --enable-modules=all?\??????????????安裝全部模塊 --enable-mpms-shared=all?\???把prefork、worker、event都編譯。httpd-2.4支持動態切換mpm。 --with-mpm=event????????????????????默認使用的mpm為:event |
| 1 | [root@stu13?httpd-2.4.9]#?make?&&?make?install |
安裝加密工具:
安裝 libmcrypt?
| 1 2 3 4 | [root@stu13?lamp]#?tar?-xf?libmcrypt-2.5.8.tar.bz2 [root@stu13?lamp]#?cd?libmcrypt-2.5.8 [root@stu13?libmcrypt-2.5.8]#?./configure??--prefix=/usr/local/libmcrypt [root@stu13?libmcrypt-2.5.8]#?make?&&?make?install |
2、編譯php
(1)、編譯php
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [root@stu13?admin]#?tar?-xf?php-5.4.26.tar.bz2 [root@stu13?admin]#?cd?php-5.4.26 [root@stu13?php-5.4.26]#?./configure?\ --prefix=/usr/local/php-5.4?\??????php的安裝目錄 --with-mysql=mysqlnd?\??????????使用php內置連接數據庫的驅動??? --with-pdo-mysql=mysqlnd?\??????使用php內置連接數據庫的驅動 --with-mysqli=mysqlnd?\?????????使用php內置連接數據庫的驅動 --with-openssl?\????????????????啟用支持ssl --enable-mbstring?\?????????????支持更多字集 --with-freetype-dir?\???????????? --with-jpeg-dir?\???????????????支持jpeg格式的圖片 --with-png-dir?\????????????????支持png格式的圖片 --with-libxml-dir?\?????????????支持擴展標記語言xml --enable-xml??\ --with-apxs2=/usr/local/httpd-2.4.9/bin/apxs?\??把php編輯成httpd的模塊 --with-mcrypt=/usr/local/libmcrypt??\???????????使用加密 --with-config-file-path=/etc/???????????????????配置文件的存放位置 --with-config-file-scan-dir=/etc/php.d?\???????? --with-bz2?\????????????????????????????????????支持bz2數據壓縮??????????????????? --enable-maintainer-zts???????????安全線程,使用httpd的:event、wordker就要啟用該選項。 [root@stu13?php-5.4.26]#?make?&&?make?install |
復制配置文件
| 1 | [root@stu13?php-5.4.26]#?cp?php.ini-production?/etc/php.ini |
(2)、編輯httpd服務器的配置文件,讓httpd服務器與php結合。
提供index.php的主頁
| 1 2 3 | <IfModule?dir_module> ????DirectoryIndex?index.php?index.html </IfModule> |
支持.php格式網頁
| 1 2 | ??AddType?application/x-httpd-php?.php ??AddType?application/x-httpd-php-source?.phps |
查看是否啟用php模塊
| 1 2 | [root@stu13?php-5.4.26]#?grep?"^LoadModule[[:space:]]php.*"?/etc/httpd-2.4.9/httpd.conf LoadModule?php5_module????????modules/libphp5.so |
修改httpd會話服務進程的屬主屬組
| 1 2 | User?usenfs Group?usenfs |
修改網頁文檔的根:
| 1 2 3 4 5 6 | DocumentRoot?"/mnt/nfs" <Directory?"/mnt/nfs"> ????Options?-Indexes?-FollowSymLinks ????AllowOverride?None ????Require?all?granted </Directory> |
3、安裝二進制格式的mariadb數據庫
| 1 2 3 4 5 | [root@localhost?admin]#?tar?-xf?mariadb-5.5.36-linux-i686.tar.gz?-C?/usr/local [root@localhost?local]#?ln?-sv?mariadb-5.5.36-linux-i686?mysql create?symbolic?link?`mysql'?to?`mariadb-5.5.36-linux-i686' [root@localhost?local]#?ll?|?grep?mysql lrwxrwxrwx??1?root?root???25?Aug?17?20:16?mysql?->?mariadb-5.5.36-linux-i686 |
創建mysql用戶,最好把它創建為系統用戶。
| 1 2 3 | root@localhost?local]#?useradd?-r?mysql [root@localhost?local]#?id?mysql uid=101(mysql)?gid=103(mysql)?groups=103(mysql) |
數據目錄放在/mydata。
要點:
| 1 2 3 | 數據的冗余:????要把存放數據的塊設備做成提供數據冗余的raid組合。 數據的備份:????要把數據存放的塊設備做成lvm. 這里就略過。。。。。 |
(1)、修改數據庫數據存放目錄的屬主屬組
| 1 2 3 | [root@localhost?/]#?chown?mysql:mysql?/mydata/ [root@localhost?/]#?ll?|?grep?mydata drwxr-xr-x????2?mysql?mysql??4096?Aug?17?21:03?mydata |
(2)、修改解壓后的二進制程序的:屬主為mysql,屬組為mysql
| 1 2 3 4 5 6 7 8 | [root@localhost?mysql]#?chown?-R?mysql:mysql?./* [root@localhost?mysql]#?ll total?216 drwxr-xr-x??2?mysql?mysql???4096?Aug?17?20:02?bin -rw-r--r--??1?mysql?mysql??17987?Feb?24?07:50?COPYING 。。。。。。 drwxr-xr-x??4?mysql?mysql???4096?Aug?17?20:02?sql-bench drwxr-xr-x??4?mysql?mysql???4096?Aug?17?20:02?support-files |
(3)、初始化mysql。
| 1 2 3 4 5 | [root@localhost?mysql]#??scripts/mysql_install_db?--user=mysql?--datadir=/mydata Installing?MariaDB/MySQL?system?tables?in?'/mydata'?... OK Filling?help?tables... OK |
把屬主改為root
| 1 2 | [root@localhost?mysql]#?chown?-R?root?. [root@localhost?mysql]#?cp?support-files/my-large.cnf??/etc/my.cnf |
根據實現情況編輯配置文件添加下述兩行
| 1 2 3 | [root@localhost?mysql]#?vim?/etc/my.cn thread_concurrency?=?4?????------>?CPU與線程的對應關系 datadir?=?/web?????????????------>?存放數據的目錄 |
(4)、提供MySQL的啟動腳本,并檢查是否有執行權限
| 1 2 3 4 5 | [root@localhost?mysql]#?cp?support-files/mysql.server??/etc/init.d/mysqld [root@localhost?mysql]#?chkconfig?--add?mysqld [root@localhost?mysql]#?chkconfig?--list?mysqld mysqld??????????0:off???1:off???2:on????3:on????4:on????5:on????6:off [root@localhost?mysql]#?chkconfig?mysqld?off |
(5)、啟動數據庫服務器
| 1 2 | [root@localhost?mysql]#?service??mysqld?start Starting?MySQL..???????????????????????????????????????????[??OK??] |
測試是否可以操作數據庫
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@localhost?mysql]#?./bin/mysql Welcome?to?the?MariaDB?monitor.??Commands?end?with?;?or?\g. Your?MariaDB?connection?id?is?2 。。。。。 MariaDB?[(none)]>?show?databases; +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?mydb???????????????| |?mysql??????????????| |?performance_schema?| |?test???????????????| +--------------------+ 5?rows?in?set?(0.00?sec) |
注意:安裝數據庫時的默認用戶。都是無需密碼就可以登陸數據庫系統。為了安全考慮。所以,要把它刪除或加密碼。
| 1 2 3 4 5 6 7 8 9 10 11 12 | MariaDB?[(none)]>?select?user,host,password?from?mysql.user; +------+----------------+----------+ |?user?|?host???????????|?password?| +------+----------------+----------+ |?root?|?localhost??????|??????????| |?root?|?nfs.9527du.com?|??????????| |?root?|?127.0.0.1??????|??????????| |?root?|?::1????????????|??????????| |??????|?localhost??????|??????????| |??????|?nfs.9527du.com?|??????????| +------+----------------+----------+ 6?rows?in?set?(0.00?sec) |
清除不需要的用戶
| 1 2 3 4 5 | MariaDB?[(none)]>?drop?user?'root'@'nfs.9527du.com'; MariaDB?[(none)]>?drop?user?'root'@'::1'; MariaDB?[(none)]>?drop?user?''@'localhost'; MariaDB?[(none)]>?drop?user?''@'nfs.9527du.com'; MariaDB?[(none)]>?drop?user?'root'@'localhost'; |
設置密碼:
| 1 2 3 4 5 6 7 8 | MariaDB?[(none)]>?set?password?for?'root'@'127.0.0.1'?=?password('root'); MariaDB?[(none)]>?select?user,host,password?from?mysql.user; +------+-----------+-------------------------------------------+ |?user?|?host??????|?password??????????????????????????????????| +------+-----------+-------------------------------------------+ |?root?|?127.0.0.1?|?*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B?| +------+-----------+-------------------------------------------+ 1?row?in?set?(0.00?sec) |
添加一個管理數據庫的用戶可以從任意IPv4的地址登陸到數據庫。授權有操作數據庫的一切權利
| 1 2 | MariaDB?[(none)]>?grant?all?on?*.*?to?'root'@'%.%.%.%'?identified?by?'root'; Query?OK,?0?rows?affected?(0.00?sec) |
為了立即生效刷新數據庫授權表
| 1 2 | MariaDB?[(none)]>?flush?privileges; Query?OK,?0?rows?affected?(0.00?sec) |
三、測試LAMP平臺:
1、httpd A(192.168.60.99) 與 httpd B(192.168.60.40) 分別掛載NFS服務器共享的/web目錄。
查看NFS共享的目錄
| 1 2 3 | [root@stu13?httpd-2.4.9]#?showmount?-e?192.168.60.88 Export?list?for?192.168.60.88: /web?192.168.60.0/24 |
掛載并檢查是否成功
| 1 2 3 | [root@stu13?httpd-2.4.9]#?mount?-t?nfs?192.168.60.88:/web??/mnt/nfs/ [root@stu13?httpd-2.4.9]#?mount?|?grep?"nfs[[:space:]]" 192.168.60.88:/web?on?/mnt/nfs?type?nfs?(rw,addr=192.168.60.88) |
在另一臺httpd服務器查看NFS共享的目錄
| 1 2 3 | [root@node1?~]#?showmount?-e?192.168.60.88 Export?list?for?192.168.60.88: /web?192.168.60.0/24 |
掛載共享目錄并檢查是否掛載成功
| 1 2 3 | [root@node1?~]#?mount?-t?nfs?192.168.60.88:/web?/mnt/nfs/ [root@node1?http-2.4.9]#?mount?|?grep?"nfs[[:space:]]" 192.168.60.88:/web?on?/mnt/nfs?type?nfs?(rw,addr=192.168.60.88) |
2、啟動httpd A 與 httpd B服務器,確保服務會話的進程的屬主為:usenfs;
httpd A(192.168.60.99)
| 1 2 3 4 5 | [root@stu13?httpd-2.4.9]#?ps?aux?|?grep?httpd root??????2427??0.2??2.0??24704?10656??????????Ss???14:52???0:00?/usr/local/httpd-2.4.9/bin/httpd?-k?start usenfs????2428??0.0??1.9?302436??9968??????????Sl???14:52???0:00?/usr/local/httpd-2.4.9/bin/httpd?-k?start usenfs????2429??0.0??1.9?302436??9972??????????Sl???14:52???0:00?/usr/local/httpd-2.4.9/bin/httpd?-k?start usenfs????2431??0.0??1.9?303460??9976??????????Sl???14:52???0:00?/usr/local/httpd-2.4.9/bin/httpd?-k?start |
httpd A(192.168.60.40)
| 1 2 3 4 5 | [root@node1?http-2.4.9]#?ps?aux?|?grep?httpd root??????1888??0.0??2.0??24704?10660??????????Ss???15:12???0:00?/usr/local/http-2.4.9/bin/httpd?-k?start usenfs????1889??0.0??1.9?302436??9972??????????Sl???15:12???0:00?/usr/local/http-2.4.9/bin/httpd?-k?start usenfs????1890??0.0??1.9?303460??9980??????????Sl???15:12???0:00?/usr/local/http-2.4.9/bin/httpd?-k?start usenfs????1896??0.0??1.9?302436??9976??????????Sl???15:12???0:00?/usr/local/http-2.4.9/bin/httpd?-k?start |
3、添加測試頁面,是否能夠連接MySQL以及httpd服務器能夠解釋.php格式的網頁。
在共享目錄/web中創建:checkmysql.php網頁文件。內容所下。
| 1 2 3 4 5 6 7 | <?php ????$link?=?mysql_connect('192.168.60.88','root','root'); ????if?($link) ???????echo?"Success..."; ????else ???????echo?"Failure..."; ?> |
4、訪問頁面測試
啟動mariadb數據庫:
| 1 2 | [root@nfs?~]#?service?mysqld?start Starting?MySQL.....????????????????????????????????????????[??OK??] |
測試httpd A(192.168.60.99):
| 1 2 | [root@nfs?~]#?curl?-eI?http://192.168.60.99/checkmysql.php Success... |
說明:httpd A(192.168.60.99)連接mariadb數據庫成功。且能夠啟用php解釋php代碼。
測試httpd B(192.168.60.40):
| 1 2 | [root@nfs?~]#?curl?-eI?http://192.168.60.40/checkmysql.php Success... |
說明:httpd A(192.168.60.40)連接mariadb數據庫成功。且能夠啟用php解釋php代碼。
關閉mariadb數據庫并測試一次。
| 1 2 3 4 5 6 | [root@nfs?~]#?service?mysqld?stop Shutting?down?MySQL.???????????????????????????????????????[??OK??] [root@nfs?~]#?curl?-eI?http://192.168.60.99/checkmysql.php Failure... [roocurl?-eI?http://192.168.60.40/checkmysql.php Failure... |
說明:
? ? 兩臺httpd服務器已經能夠與后端的MySQL服務器交互了。且能夠解釋php格式的頁面文檔了。
四、部署: wordpress-3.3.1.zh.CN博客程序:
1、創建wordpress博客程序連接MySQL數據庫的用戶以及創建它使用的數據庫。
| 1 2 3 | MariaDB?[(none)]>?create?database?wordpress; MariaDB?[(none)]>?grant?all?on?wordpress.*?to?'wordpress'@'192.168.60.%'?identified?by?'wordpress'; MariaDB?[(none)]>?flush?privileges |
說明:
? ? 基于安全方面的考慮,wordpress用戶只有使用wordpress數據庫的權利。沒有操作其它數據庫的權利。
2、配置wordpress博客程序
| 1 2 | [root@stu13?nfs]#?unzip?wordpress-3.3.1-zh_CN.zip [root@stu13?wordpress]#?cp?wp-config-sample.php?wp-config.php |
編輯wp-config.php配置文件,輸入連接MySQL數據的用戶和密碼以及使用的數據庫
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@stu13?wordpress]#?vim?wp-config.php //?**?MySQL?設置?-?具體信息來自您正在使用的主機?**?// /**?WordPress?數據庫的名稱?*/ define('DB_NAME',?'wordpress'); /**?MySQL?數據庫用戶名?*/ define('DB_USER',?'wordpress'); /**?MySQL?數據庫密碼?*/ define('DB_PASSWORD',?'wordpress'); /**?MySQL?主機?*/ define('DB_HOST',?'192.168.60.88'); /**?創建數據表時默認的文字編碼?*/ define('DB_CHARSET',?'utf8'); |
在192.168.60.99這臺服務器上,打開wordpress程序安裝向導
在192.168.60.40?這臺httpd服務器上也打開wordpress的安裝向導并安裝wordpress博客程序。下圖為安裝后的界面:
通過另一臺服務器瀏覽
OK....
? ? ?本文轉自成長的小蟲 51CTO博客,原文鏈接:http://blog.51cto.com/9528du/1541380,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的根据DNS的A记录负载均衡web服务请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搭建 LNMP 环境
- 下一篇: Oracle Data Guard 主库