五、准备LNMP环境
1.编译安装tengine:
1 yum -y install pcre-devel 2 useradd -r nginx //要保证两台主机的nginx用户id和组id完全一致 3 tar xf tengine-2.1.0.tar.gz 4 cd tengine-2.1.0 5 ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/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/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --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 --with-pcre 6 make && make install
2.为tengine提供脚本:
1 vim /etc/init.d/nginx 2 ------------------------------------ 3 #!/bin/sh 4 # 5 # nginx - this script starts and stops the nginx daemon 6 # 7 # chkconfig: - 85 15 8 # description: Nginx is an HTTP(S) server, HTTP(S) reverse 9 # proxy and IMAP/POP3 proxy server 10 # processname: nginx 11 # config: /etc/nginx/nginx.conf 12 # config: /etc/sysconfig/nginx 13 # pidfile: /var/run/nginx.pid 14 15 # Source function library. 16 . /etc/rc.d/init.d/functions 17 18 # Source networking configuration. 19 . /etc/sysconfig/network 20 21 # Check that networking is up. 22 [ "$NETWORKING" = "no" ] && exit 0 23 24 nginx="/usr/local/nginx/sbin/nginx" 25 prog=$(basename $nginx) 26 27 NGINX_CONF_FILE="/etc/nginx/nginx.conf" 28 29 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 30 31 lockfile=/var/lock/subsys/nginx 32 33 make_dirs() { 34 # make required directories 35 user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 36 options=`$nginx -V 2>&1 | grep 'configure arguments:'` 37 for opt in $options; do 38 if [ `echo $opt | grep '.*-temp-path'` ]; then 39 value=`echo $opt | cut -d "=" -f 2` 40 if [ ! -d "$value" ]; then 41 # echo "creating" $value 42 mkdir -p $value && chown -R $user $value 43 fi 44 fi 45 done 46 } 47 48 start() { 49 [ -x $nginx ] || exit 5 50 [ -f $NGINX_CONF_FILE ] || exit 6 51 make_dirs 52 echo -n $"Starting $prog: " 53 daemon $nginx -c $NGINX_CONF_FILE 54 retval=$? 55 echo 56 [ $retval -eq 0 ] && touch $lockfile 57 return $retval 58 } 59 60 stop() { 61 echo -n $"Stopping $prog: " 62 killproc $prog -QUIT 63 retval=$? 64 echo 65 [ $retval -eq 0 ] && rm -f $lockfile 66 return $retval 67 } 68 69 restart() { 70 configtest || return $? 71 stop 72 sleep 1 73 start 74 } 75 76 reload() { 77 configtest || return $? 78 echo -n $"Reloading $prog: " 79 killproc $nginx -HUP 80 RETVAL=$? 81 echo 82 } 83 84 force_reload() { 85 restart 86 } 87 88 configtest() { 89 $nginx -t -c $NGINX_CONF_FILE 90 } 91 92 rh_status() { 93 status $prog 94 } 95 96 rh_status_q() { 97 rh_status >/dev/null 2>&1 98 } 99 100 case "$1" in 101 start) 102 rh_status_q && exit 0 103 $1 104 ;; 105 stop) 106 rh_status_q || exit 0 107 $1 108 ;; 109 restart|configtest) 110 $1 111 ;; 112 reload) 113 rh_status_q || exit 7 114 $1 115 ;; 116 force-reload) 117 force_reload 118 ;; 119 status) 120 rh_status 121 ;; 122 condrestart|try-restart) 123 rh_status_q || exit 0 124 ;; 125 *) 126 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 127 exit 2 128 esac 129 ------------------------------------ 130 chmod +x /etc/init.d/nginx 131 chkconfig --add nginx 132 chkconfig nginx off //一会作为corosync的资源,所以不要开机自动启动
3.安装mysql,man和path路径输出这里不再演示:
1 useradd -r mysql //保证两台主机的mysql用户id和组id完全一致 2 tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/ 3 cd /usr/local/ 4 ln -sv mariadb-10.0.20-linux-x86_64 mysql 5 cd mysql/ 6 chown -R root:mysql . 7 ln -sv /usr/local/mysql/include /usr/include/mysql //输出mysql的头文件至系统头文件路径 8 echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf //输出mysql的库文件给系统库查找路径 9 ldconfig
本试验中coro1为drbd的Primary,所以下面的操作在coro1上进行,提前将/dev/brbd0挂载至/mydata:
1 chown -R mysql:mysql /mydata/data/ 2 /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ //初始化mysql 3 cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf //提供配置文件 4 vim /etc/my.cnf 5 ------------------------------------ 6 datadir = /mydata/data //增加此项 7 ------------------------------------ 8 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //提供服务脚本 9 scp -p /etc/init.d/mysqld coro2:/etc/init.d/ 10 scp -p /etc/my.cnf coro2:/etc/ 11 chkconfig --add mysqld 12 chkconfig mysqld off //一会作为corosync的资源,所以不要开机自动启动 13 service mysqld start 14 /usr/local/mysql/bin/mysql //连接mysql 15 ------------------------------------ 16 CREATE DATABASE abcd; //创建abcd数据库 17 SHOW DATABASES; //显示数据库列表
停掉mysql,让drbd主节点切换至coro2并挂载,在coro2上连接mysql,切换主次步骤请参考前面:
1 chkconfig --add mysqld 2 chkconfig mysqld off 3 service mysqld start 4 /usr/local/mysql/bin/mysql 5 ----------------------------------- 6 SHOW DATABASES;
mysql测试成功,coro2上也可以看到coro1创建的数据。停掉mysql,让主节点再次切换到coro1上并挂载/dev/drbd0。
4.编译安装php:
1 yum -y install libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel 2 tar xf php-5.6.11.tar.bz2 3 cd php-5.6.11 4 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl --with-pdo-mysql=/usr/local/mysql 5 make && make install
5.为php和php-fpm提供配置文件:
1 cp php.ini-production /etc/php.ini //提供php配置文件 2 cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //提供脚本 3 chmod +x /etc/init.d/php-fpm 4 chkconfig --add php-fpm 5 chkconfig php-fpm off //一会作为corosync的资源,所以不要开机自动启动 6 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf //提供php-fpm配置文件 7 vim /usr/local/php/etc/php-fpm.conf 8 --------------------------------------------------- 9 pm.max_children = 150 10 pm.start_servers = 8 11 pm.min_spare_servers = 5 12 pm.max_spare_servers = 10 13 pid = /usr/local/php/var/run/php-fpm.pid 14 user = nginx 15 group = nginx 16 listen = 192.168.19.150:9000 ?? //监听在VIP的9000端口
六、LNMP与DRBD结合
1.现在试验中coro1为drbd的Primary节点,所以以下操作在coro1上进行:
1 chown -R nginx:nginx /mydata/www/ 2 vim /etc/nginx/nginx.conf 3 ---------------------------------------- 4 location / { 5 root /mydata/www; 6 index index.php index.html index.htm; 7 } 8 location ~ \.php$ { 9 root /mydata/www; 10 fastcgi_pass 192.168.19.150:9000; 11 fastcgi_index index.php; 12 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 13 include fastcgi_params; 14 } 15 ---------------------------------------- 16 vim /etc/nginx/fastcgi_params //将此文件内容改为如下几行 17 ---------------------------------------- 18 fastcgi_param GATEWAY_INTERFACE CGI/1.1; 19 fastcgi_param SERVER_SOFTWARE nginx; 20 fastcgi_param QUERY_STRING $query_string; 21 fastcgi_param REQUEST_METHOD $request_method; 22 fastcgi_param CONTENT_TYPE $content_type; 23 fastcgi_param CONTENT_LENGTH $content_length; 24 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 25 fastcgi_param SCRIPT_NAME $fastcgi_script_name; 26 fastcgi_param REQUEST_URI $request_uri; 27 fastcgi_param DOCUMENT_URI $document_uri; 28 fastcgi_param DOCUMENT_ROOT $document_root; 29 fastcgi_param SERVER_PROTOCOL $server_protocol; 30 fastcgi_param REMOTE_ADDR $remote_addr; 31 fastcgi_param REMOTE_PORT $remote_port; 32 fastcgi_param SERVER_ADDR $server_addr; 33 fastcgi_param SERVER_PORT $server_port; 34 fastcgi_param SERVER_NAME $server_name; 35 ---------------------------------------- 36 scp /etc/nginx/nginx.conf coro2:/etc/nginx/ 37 scp /etc/nginx/fastcgi_params coro2:/etc/nginx/ 38 ifconfig eth0:0 192.168.19.150/24 up //手动启动VIP 39 service nginx start 40 service mysqld start 41 service php-fpm start
在/mydata/www下放入网页文件,此处以wordpress为例。用浏览器打开,如果没问题,则会出现安装页面,按照提示安装即可。我这里手动创建了wordpress数据库,并给root用户添加了密码,安装时就直接使用root。
1 /usr/local/mysql/bin/mysql 2 ----------------------------------------- 3 CREATE DATABASE wordpress; 4 UPDATE mysql.user SET Password=PASSWORD(123456) WHERE User='root'; 5 FLUSH PRIVILEGES;
2.切换drbd的Primary节点为coro2,并测试:
在coro1上操作:
1 service nginx stop 2 service mysqld stop 3 service php-fpm stop 4 umount /mydata/ 5 drbdadm secondary web 6 ifconfig eth0:0 down
在coro2上操作:
1 drbdadm primary web 2 mount /dev/drbd0 /mydata/ 3 ifconfig eth0:0 192.168.19.150/24 up 4 service nginx start 5 service mysqld start 6 service php-fpm start
手动切换完毕之后,同样打开,进行任何操作均没问题。至此,drbd+LNMP搭建完毕。下一步,就要将corosync和pacemaker整合进来了。现在检查两台主机的VIP、nginx、php-fpm、mysql、drbd均为关闭状态,且不能开机自动启动。