一、源碼編譯安裝LNMP架構環境;
php
(1)安裝編輯工具和環境
html
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make cmake libtool* git tree
(2)安全nginxmysql
[root@localhost opt]# wget http://nginx.org/download/nginx-1.8.1.tar.gz #下載nginx安裝包 [root@localhost opt]# tar xzvf nginx-1.8.1.tar.gz #解壓安裝包 [root@localhost nginx-1.8.1]# ./configure \ > --prefix=/usr/local/nginx \ > --with-http_realip_module \ > --with-http_sub_module \ > --with-http_ssl_module \ > --with-http_gzip_static_module \ > --with-pcre [root@localhost nginx-1.8.1]# make && make install [root@localhost nginx-1.8.1]# vim /usr/local/nginx/conf/nginx.conf #修改php相關配置 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; include fastcgi_params; } [root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx #啓動nginx
(3)安裝mysqlnginx
[root@localhost opt]# groupadd mysql #添加mysql組 [root@localhost opt]# useradd -r -g mysql mysql #添加mysql用戶 [root@localhost opt]# tar xzvf mysql-5.6.24.tar.gz [root@localhost opt]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio #安裝編譯代碼所須要的包 [root@localhost opt]# cd /opt/mysql-5.6.24 [root@localhost mysql-5.6.24]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ [MySQL安裝的根目錄] -DMYSQL_DATADIR=/mydata/mysql/data \ [MySQL數據庫文件存放目錄] -DSYSCONFDIR=/etc \ [MySQL配置文件所在目錄] -DMYSQL_USER=mysql \ [MySQL用戶名] -DWITH_MYISAM_STORAGE_ENGINE=1 \ [MySQL的數據庫引擎] -DWITH_INNOBASE_STORAGE_ENGINE=1 \ [MySQL的數據庫引擎] -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ [MySQL的數據庫引擎] -DWITH_MEMORY_STORAGE_ENGINE=1 \ [MySQL的數據庫引擎] -DWITH_READLINE=1 \ [MySQL的readline library] -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \ [MySQL的通信目錄] -DMYSQL_TCP_PORT=3306 \ [MySQL的監聽端口] -DENABLED_LOCAL_INFILE=1 \ [啓用加載本地數據] -DENABLE_DOWNLOADS=1 \ [編譯時容許自主下載相關文件] -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all \ [使MySQL支持全部的擴展字符] -DDEFAULT_CHARSET=utf8 \ [設置默認字符集爲utf8] -DDEFAULT_COLLATION=utf8_general_ci \ [設置默認字符校對] -DWITH_DEBUG=0 \ [禁用調試模式] -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled \ [通信時支持ssl協議] -DWITH_ZLIB:STRING=bundled [容許使用zlib library] [root@localhost mysql-5.6.24]# make && make install [root@localhost local]# chown -R mysql:mysql /usr/local/mysql/ #修改mysql文件夾屬組和屬主 [root@localhost local]# chown -R mysql:mysql /mydata/mysql/data [root@localhost etc]# vim /etc/my.cnf #修改my.conf配置文件 # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir=/usr/local/mysql datadir=/mydata/mysql/data socket=/var/lib/mysql/mysql.sock character-set-server=utf8 user= mysql # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [root@localhost local]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql/data #初始化mysql數據庫
(4)安裝PHPc++
root@localhost opt]# tar xzvf php-5.6.5.tar.gz [root@localhost php-5.6.5]# ./configure --enable-opcache --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring=all --with-pdo-mysql --enable-sockets --enable-mbstring --enable-fpm --with-curl --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-gd --with-libxml-dir=/usr --enable-xml --with-openssl --with-iconv [root@localhost php-5.6.5]# make && make install [root@localhost etc]# cp /opt/php-5.6.5/php.ini-development /usr/local/php/etc/php.ini [root@localhost etc]# cd /usr/local/php/etc/ [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# /usr/local/php/sbin/php-fpm #啓動php [root@localhost html]# vim /usr/local/nginx/html/index.php #建立一個php頁面 <?php phpinfo(); ?>
二、編寫一個腳本完成如下功能:git
(1)、一鍵搭建LNMP源碼編譯環境;web
(2)、可經過在腳本後面跟上一些參數來自定義安裝目錄等其餘選項。sql
[root@localhost shell]# cat /shell/LNMP.conf #參數較多放在一個配置文件中 nginx_dir=/usr/local/nginx mysql_dir=/user/local/mysql mysqldata_dir=/mysqldata php_dir=/usr/local/php [root@localhost shell]# cat /shell/LNMP.sh #!/bin/bash #加載配置文件 source $1 #nginx安裝函數 nginx() { groupadd -r nginx useradd -s /sbin/nologin -g nginx -r nginx cd /opt echo "------------開始安裝nginx-1.8.1------------" wget http://nginx.org/download/nginx-1.8.1.tar.gz tar xzvf nginx-1.8.1.tar.gz cd nginx-1.8.1 ./configure --prefix=${nginx_dir} --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre make && make install } #mysql安裝模塊 mysql() { groupadd mysql useradd -r -g mysql mysql cd /opt echo "------------開始安裝libmcrypt-2.5.7------------" wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.24.tar.gz tar mysql-5.6.24.tar.gz cd mysql-5.6.24 cmake . -DCMAKE_INSTALL_PREFIX=${mysql_dir} \ -DMYSQL_DATADIR=${mysqldata_dir} \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DMYSQL_USER=mysql \ -DMYSQL_GROUP=mysql make && make install cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf chown -R root:mysql ${mysql_dir} chown -R root:mysql ${mysqldata_dir} } #php安裝模塊 php() { cd /opt echo "------------開始安裝libmcrypt-2.5.7------------" wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz tar xvf libmcrypt-2.5.7.tar.gz ./configure --prefix=/usr/local/libmcrypt && make && make install tar xzvf php-5.6.5.tar.gz echo "------------開始安裝php-5.6.5------------" wget http://cn2.php.net/distributions/php-5.6.5.tar.gz cd php-5.6.5 ./configure --enable-opcache --prefix=${php_dir} --with-config-file-path=${php_dir}/etc --with-mysql=${mysql_dir} --with-mysqli=${mysql_dir}/bin/mysql_config --enable-mbstring=all --with-pdo-mysql --enable-sockets --enable-mbstring --enable-fpm --with-curl --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-gd --with-libxml-dir=/usr --enable-xml --with-openssl --with-iconv && make && make install cp ${php_dir}/etc/php-fpm.conf.default ${php_dir}/etc/php-fpm.conf cp /opt/php-5.6.5/php.ini-development ${php_dir}/etc/php.ini } yum install gcc gcc-c++ make pcre-* zlib-* cmake ncurses-devel libjpeg* libpng* freetype* libcurl-devel libvpx-devel libxml2 libxml2-devel libXpm libXpm-devel libXpm.i686 libXpm.i686-devel openldap-devel -y #安裝nginx軟件nginx if [ $? -eq 0 ];then echo "nginx安裝成功!!!" else echo "nginx安裝異常!!!" exit fi #安裝mysql數據庫 mysql if [ $? -eq 0 ];then echo "mysql安裝成功!!!" else echo "mysql安裝異常!!!" exit fi #安裝php php if [ $? -eq 0 ];then echo "php安裝成功!!!" exit else echo "php安裝異常!!!" exit fi
三、結合圖形描述LVS的工做原理;shell
(1)vs-nat模型數據庫
主要是修改目標IP地址爲挑選出新的RS的IP地址。即請求進入負載均衡器時作DNAT,響應出負載均衡器時作SNAT。
a.當用戶請求到達Director Server,此時請求的數據報文會先到達內核的PREROUTING鏈,此時報文的源IP是CIP,目標IP是VIP。
b.PREROUTING鏈檢查發現數據包的目標IP是本機,將數據包送至INPUT鏈。
c.IPVS內核模塊比對數據包請求的服務是否爲集羣服務,若是是,則修改數據包的目標IP爲後端服務器的IP,而後將數據包發至POSTROUTING鏈,作DNAT轉換。此時報文的源IP是CIP,目標IP是RIP
d.POSTROUTING鏈經過選路,將數據包發送到Real Server。
e.Real Server比對發現目標IP是本身的IP,開始創建響應報文發回給Director Server,此時報文的源IP是RIP,目標IP是CIP.
f.Director Server在響應客戶端以前,此時會將源IP地址修改成本身的IP地址,而後響應給客戶端,作SNAT轉換。此時報文的源IP是VIP。目標IP是CIP。
(2)lvs-dr模型
將請求報文的目標MAC地址設定爲天選出來的RS的MAC地址。即作MAC地址轉換。
a.當用戶請求到達Director Server,此時請求的數據報文huixiandao內核空間的PREROUTING鏈,此時報文的IP是CIP,目標IP是VIP。
b.PREROUTING鏈檢查發現數據包的目標IP是本機,將數據包送至INPUT鏈,
c.IPVS內核模塊比對數據包請求的服務是否爲集羣服務,若是是,將請求報文中的源MAC地址修改成DIP的MAC地址,將目標MAC地址修改成RIP的MAC地址,而後將數據包發至POETROUTING鏈中,此時的源IP和目的IP均未修改,僅修改了源MAC地址爲DIP的MAC地址,目標MAC地址爲RIP的MAC地址。
d.因爲DS和RS實在同一網絡中,因此二者之間的通訊時經過二層協議來傳輸。POSTROUTING鏈檢查目標MAC地址爲RIP的MAC地址,那麼此時數據包將會發至Real Server。
e.RS發現請求報文的MAC地址是本身的MAC地址,就接受此報文,處理完成之後,將響應報文經過IO接口傳送給eth0網卡,而後向外發出,不通過負載均衡器。此時源IP地址爲VIP,目標IP是CIP。
f.響應報文最終送至客戶端。
(3)lvs-tun模型
在原有的IP報文外再次封裝多一層IP首部,內部IP首部(源地址爲CIP,目標地址爲VIP)外部IP地址首部(源地址爲DIP,目標地址爲RIP)
a.當用戶請求報文到達DS,此時請求的數據報文會先到內核的PREROUTING鏈。此時源IP是CIP,目標IP是VIP。
b.PREROUTING鏈檢查發現數據包的目標IP是本機,將數據包送至INPUT鏈。
c.IPVS比對數據包請求的服務是否爲集羣服務,若是是,在請求報文的首部再次封裝一層IP報文,封裝源IP爲DIP,目標IP是RIP,而後發至POSTROUTING鏈。此時源IP是爲DIP,目標IP是RIP。
d.POSTROUTING鏈根據最新封裝的IP報文,將數據包發至RS(由於外層封裝多了一層IP首部,因此能夠理解爲此時經過隧道傳輸)。此時源IP是DIP,目標IP是RIP。
e.RS收到報文後發現是本身的IP地址,就會將報文接受下來,拆除最外層的IP後,會發現裏面還有一層IP首部,並且目標地址是本身的lo接口VIP,那麼此時RS開始處理此請求,處理完成滯後,經過lo接口送給eth0網卡,而後向外傳遞。此時的源IP地址爲VIP,目標IP爲CIP。
f.響應報文送達至客戶端。
四、闡述varnish的功能及其應用場景,並經過實際的應用案例來描述配置、測試、調試過程。
varnish實現php和html動靜分離、負載均衡
php頁面
http://10.10.10.200:8080http://10.10.10.201:8080html頁面
http://10.10.10.200:8081
http://10.10.10.201:8081
[root@localhost opt]# wget -c https://repo.varnish-cache.org/source/varnish-4.1.3.tar.gz [root@localhost opt]# tar xzvf varnish-4.1.3.tar.gz [root@localhost opt]# cd varnish-4.1.3 [root@localhost varnish-4.1.3]# ./configure --prefix=/usr/local/varnish [root@localhost varnish-4.1.3]# make && make install [root@localhost varnish-4.1.3]# cd /usr/local/varnish/var/varnish/ [root@localhost varnish-4.1.3]# vim default.vcl #定義後端服務器 backend web1 { .host = "10.10.10.200"; .port = "8081"; .connect_timeout = 20s; } backend web2 { .host = "10.10.10.201"; .port = "8081"; .connect_timeout = 20s; } backend php1 { .host = "10.10.10.200"; .port = "8080"; .connect_timeout = 20s; } backend php2 { .host = "10.10.10.201"; .port = "8080"; .connect_timeout = 20s; } import directors; #定義html和php集羣 sub vcl_init { new web_cluster = directors.round_robin(); web_cluster.add_backend(web1); web_cluster.add_backend(web2); new php_cluster = directors.round_robin(); php_cluster.add_backend(php1); php_cluster.add_backend(php2); } #定義acl,設置清理緩存的IP acl allow_purge_cache { "127.0.0.1"; "10.18.11.0"/24; } sub vcl_recv { #配置動靜分離 if (req.url ~ "\.php$"){ set req.backend_hint = php_cluster.backend(); } else{ set req.backend_hint = web_cluster.backend(); } #使得後端服務能記錄訪問者的真實IP if (req.http.X-Forward-For) { set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip; } else { set req.http.X-Forward-For = client.ip; } #定義清楚緩存IP,調用上面的Acl if (req.method == "PURGE") { if (!client.ip ~ allow_purge_cache) { return(synth(405,"Not allowed")); } return(hash); } #支持壓縮功能 if (req.http.Accept-Encoding) { if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { remove req.http.Accept-Encoding; } else if (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } else if (req.http.Accept-Encoding ~ "deflate") { set req.http.Accept-Encoding = "deflate"; } else { remove req.http.Accept-Encoding; } } } sub vcl_deliver { #定義Header標識,判斷緩存是否命中 if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; #命中則返回HIT } else { set resp.http.X-Cache = "MISS"; #未命中則返回MISS } } [root@localhost varnish]# /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/var/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:80 #啓動varnish [root@localhost varnish]# curl -I http://10.10.10.200/index.php #訪問php頁面 HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Tue, 22 Nov 2016 08:38:05 GMT Content-Type: text/html; charset=UTF-8 X-Powered-By: PHP/5.6.5 X-Varnish: 2 Age: 0 Via: 1.1 varnish-v4 X-Cache: MISS #第一次訪問沒有緩存 Accept-Ranges: bytes Connection: keep-alive [root@localhost varnish]# curl -I http://10.10.10.200/index.php HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Tue, 22 Nov 2016 08:38:05 GMT Content-Type: text/html; charset=UTF-8 X-Powered-By: PHP/5.6.5 X-Varnish: 5 3 Age: 3 Via: 1.1 varnish-v4 X-Cache: HIT #第二次訪問命中緩存 Accept-Ranges: bytes Connection: keep-alive [root@localhost varnish]# curl -I http://10.10.10.200/index.html #訪問html頁面 HTTP/1.1 200 OK Date: Tue, 22 Nov 2016 08:46:02 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Tue, 22 Nov 2016 07:00:59 GMT ETag: "15-541de55a6c15a" Content-Length: 21 Content-Type: text/html; charset=UTF-8 X-Varnish: 32772 Age: 0 Via: 1.1 varnish-v4 X-Cache: MISS Accept-Ranges: bytes Connection: keep-alive [root@localhost varnish]# curl -I http://10.10.10.200/index.html HTTP/1.1 200 OK Date: Tue, 22 Nov 2016 08:46:02 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Tue, 22 Nov 2016 07:00:59 GMT ETag: "15-541de55a6c15a" Content-Length: 21 Content-Type: text/html; charset=UTF-8 X-Varnish: 9 32773 Age: 1 Via: 1.1 varnish-v4 X-Cache: HIT Accept-Ranges: bytes Connection: keep-alive
五、搭建一套LVS-DR模型的高性能集羣,並用Keepalived實現nginx與lvs的高可用集羣,同時實現如下功能:
(1)、wordpress程序經過nfs共享給各個realserver;
(2)、後端realserver中的nginx和php分離
名稱
用途 |
IP |
LVS-Master |
10.18.11.31 |
LVS-BACKUP | 10.18.11.32 |
LVS-VIP | 10.18.11.40 |
wordpress1 | 10.18.11.29 |
wordpress2 | 10.18.11.30 |
(1)配置NFS共享wordpress程序
#在10.18.11.29上配置共享目錄 [root@localhost ~]# vim /etc/exports #在10.18.11.29上配置共享目錄 /usr/local/nginx/html/ 10.18.11.29(rw,sync,fsid=0,no_root_squash) [root@localhost /]# systemctl start rpcbind.service #啓動rpcbind服務 [root@localhost /]# systemctl start nfs-server.service #啓動nfs服務 #在10.18.11.30上掛載共享目錄 [root@localhost nginx]# mount -t nfs 10.18.11.29:/usr/local/nginx/html /usr/local/nginx/htmlmount -t nfs 10.18.11.29:/usr/local/nginx/html /usr/local/nginx/html [root@localhost nginx]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda2 75G 4.6G 66G 7% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 25M 1.9G 2% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda1 477M 94M 354M 21% /boot /dev/mapper/vgdata-mylv 6.8G 23M 6.4G 1% /users tmpfs 380M 0 380M 0% /run/user/0 10.18.11.29:/usr/local/nginx/html 75G 8.7G 62G 13% /usr/local/nginx/html [root@localhost nginx]# ll /usr/local/nginx/html/ #wordpress工程文件已共享 總用量 28 -rw-r--r-- 1 root root 537 11月 16 17:22 50x.html -rw-r--r-- 1 root root 145 11月 26 23:14 inc.php -rw-r--r-- 1 root root 612 11月 16 17:22 index.html -rw-r--r-- 1 root root 20 11月 25 15:23 index.php -rw-r--r-- 1 root root 6 11月 21 17:39 test.html -rw-r--r-- 1 root root 145 11月 26 23:15 test.php drwxrwxrwx 5 root root 4096 11月 27 16:37 wordpress
(2)安裝workpress
[root@localhost opt]# wget [root@localhost opt]# tar xzvf wordpress-4.5.3-zh_CN.tar.gz [root@localhost opt]# cp -r wordpress /usr/local/nginx/html/
(3)在遊覽器中訪問http://10.18.11.29/wordpress/wp-admin/install.php進行數據庫配置