client -> cdn -> HA+LB -> web(apache/nginx) -> app(tomcat) -> DB
实验所需软件
mysql-boost-5.7.17.tar.gz
cmake-2.8.12.2-4.el6.x86_64.rpm
php-5.6.35.tar.bz2
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
libmcrypt-2.5.8-9.el6.x86_64.rpm
re2c-0.13.5-1.el6.x86_64.rpm
1.安装mysql
[root@server1~]# tar zxf mysql-boost-5.7.17.tar.gz
[root@server1~]# yum install cmake-2.8.12.2-4.el6.x86_64.rpm -y
[root@server1~]# cd mysql-5.7.17/
[root@server1 mysql-5.7.17/]# yum install gcc gcc-c++ -y
[root@server1~]# yum install ncurses-devel -y
[root@server1~]# yum install bison -y
[root@server1~]# rm -f CMakeCache.txt ##请错误安装缓存
[root@server1~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=./boost/boost_1_59_0/
[root@server1~]# make
[root@server1~]# make install
2.配置mysql
[root@server1 mysql]# cd /etc/
[root@server1 etc]# mv my.cnf my.cnf.bak
[root@server1 etc]# cd /usr/local/lnmp/mysql/support-files/
[root@server1 support-files]# cp my-default.cnf /etc/my.cnf
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld
[root@server1 mysql]# useradd mysql
[root@server1 mysql]# chown mysql.mysql . -R
[root@server1 mysql]# cd bin/
[root@server1 bin]# pwd
/usr/local/lnmp/mysql/bin
[root@server1 bin]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin ##添加环境变量
[root@server1 bin]# source ~/.bash_profile
[root@server1 bin]# mysqld --initialize
[root@server1 bin]# cd ..
[root@server1 mysql]# chown root.mysql . -R
[root@server1 mysql]# chown mysql data/ -R
[root@server1 data]# /etc/init.d/mysqld start
[root@server1 data]# mysql -p
Enter password:
mysql> alter user root@localhost identified by 'westos';
Query OK, 0 rows affected (0.00 sec)
3.配置安装php
[root@server1 ~]# tar jxf php-5.6.35.tar.bz2
[root@server1 ~]# cd php-5.6.35
[root@server1 php-5.6.35]# ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-pear -with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash
[root@server1 ~]# rpm -ivh re2c-0.13.5-1.el6.x86_64.rpm
[root@server1 ~]# rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm
[root@server1 php-5.6.35]# make
[root@server1 php-5.6.35]# make install
[root@server1 local]# useradd nginx
[root@server1 php-5.6.35]# cd /usr/local/lnmp/php/etc
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# vim php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/lnmp/php/var
; Default Value: none
pid = run/php-fpm.pid ##打开,否则默认为none
[root@server1 etc]# cd /root/php-5.6.35
[root@server1 php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[root@server1 php-5.6.35]# cd /root/php-5.6.35/sapi/fpm
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm
[root@server1 fpm]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@server1 fpm]# cd
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 nginx-1.10.1]# vim src/core/nginx.h
***************************************
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define nginx_version 1010001
#define NGINX_VERSION "1.10.1"
#define NGINX_VER "nginx" ##删除版本号
***************************************
[root@server1 nginx-1.10.1]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" ##不启动debug检测
[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.10.1]# make && make install
[root@server1 nginx-1.10.1]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# vim nginx.conf
*****************************************************
user nginx;
worker_processes auto;
location / {
root html;
index index.php index.html index.htm; ##添加index.php
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; ##本地9000接口
fastcgi_index index.php; ##默认发布目录
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf; ##通过fastcgi.conf文件
}
*****************************************************
[root@server1 conf]# vim fastcgi.conf
[root@server1 conf]# cd /usr/local/lnmp/nginx/html
[root@server1 html]# vim index.html
[root@server1 html]# vim index.php
<?php
phpinfo()
?>
[root@server1 html]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/bin/ ##做启动链接
[root@server1 html]# nginx -t ##检验nginx是否有报错
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 html]# nginx
[root@server1 html]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini
; http://php.net/date.timezone ##更改时区
date.timezone = Asia/Shanghai
[root@server1 etc]# /etc/init.d/php-fpm reload ##平滑重启
Reload service php-fpm done
检测:
访问172.25.18.1
4.建立论坛
[root@server1 mysql]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!
[root@server1 ~]# unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html/
[root@server1 ~]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# mv upload/ bbs
[root@server1 html]# cd bbs/
[root@server1 bbs]# pwd
/usr/local/lnmp/nginx/html/bbs
[root@server1 bbs]# cd /usr/local/lnmp/nginx/html/readme
[root@server1 bbs]# cat readme.txt ##安装说明
测试:
在浏览器中运行 install 程序,即访问 http://您的域名/论坛目录/install/
[root@server1 bbs]# chmod 777 config/
[root@server1 bbs]# chmod 777 data/ -R
[root@server1 bbs]# chmod 777 uc_client/ uc_server/ -R
[root@server1 bbs]# cd /usr/local/lnmp/mysql/data/
[root@server1 data]# pwd
/usr/local/lnmp/mysql/data
[root@server1 data]# vim /usr/local/lnmp/php/etc/php.ini
pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock ##添加mysql.sock
mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock ##
mysqli.default_socket =/usr/local/lnmp/mysql/data/mysql.sock ##
[root@server1 data]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@server1 mysql]# cd ..
[root@server1 mysql]# chmod 755 data/
[root@server1 mysql]# cd /usr/local/lnmp/nginx/html/bbs/install
[root@server1 install]# rm -rf index.php ##避免重复安装
[root@server1 install]# yum install httpd-tools-2.2.15-29.el6_4.x86_64 -y
[root@server1 install]# ab -c 10 -n 1000 http://172.25.18.1/index.html ##ab压力测试工具,http提供的
[root@server1 install]# ab -c 10 -n 1000 http://172.25.18.1/index.php ##-c 并发为10 -n 1000数据
[root@server1 install]# ab -c 10 -n 1000 http://172.25.18.1/index.php
[root@server1 install]# ab -c 10 -n 1000 http://172.25.18.1/bbs/forum.php
模型:
client -> nginx -> fastcgi -> php-fpm -> nginx -> client
优化模型:
client -> nginx -> fastcgi -> php-fpm -> nginx -> client
|
|
memcache
5.模型优化
[root@server1 ~]# yum install memcached -y
[root@server1 ~]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[root@server1 ~]# tar zxf memcache-2.2.5.tgz
[root@server1 ~]# cd /usr/local/lnmp/php/bin/
[root@server1 bin]# pwd
/usr/local/lnmp/php/bin
[root@server1 bin]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin ##添加环境变量
[root@server1 bin]# source ~/.bash_profile
[root@server1 memcache-2.2.5]# phpize ##自动写出/.configure 等安装配置
[root@server1 memcache-2.2.5]# ./configure --enable-memcache
[root@server1 memcache-2.2.5]# make && make install
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 no-debug-non-zts-20131226]# ls
memcache.so opcache.a opcache.so
[root@server1 no-debug-non-zts-20131226]# php -m | grep memcache
[root@server1 no-debug-non-zts-20131226]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini
extension=memcache.so ##打开memcache
[root@server1 etc]# /etc/init.d/php-fpm reload
[root@server1 etc]# php -m | grep memcache
memcache
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# cp memcache.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cp example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php
*****************************************************************************
define('ADMIN_USERNAME','memcache'); // Admin Username ##帐号
define('ADMIN_PASSWORD','westos'); // Admin Password ##密码
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = '172.25.18.1:11211'; // add more as an array ##server
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
*****************************************************************************
测试:
浏览器访问 http://172.25.18.1/memcache.php
http://172.25.18.1/example.php
[root@server1 html]# yum install telnet -y
[root@server1 ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get
ERROR
get str_key
END
6.openresty包装后的nginx
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# gmake && gmake install
[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# nginx -s stop
[root@server1 conf]# vim nginx.conf
****************************************************************************
user nginx;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
*****************************************************************************
[root@server1 conf]# vim /etc/security/limits.conf
nginx - nofile 65535
[root@server1 sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@server1 sbin]# ./nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 html]# pwd
/usr/local/lnmp/nginx/html
[root@server1 html]# cp index.php example.php memcache.php /usr/local/openresty/nginx/html/
[root@server1 html]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ./nginx
[root@server1 conf]# vim nginx.conf
*********************************************
http {
upstream memcache{
server 172.25.18.1:11211;
keepalive 512;
}
include mime.types;
default_type application/octet-stream;
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
*********************************************
测试:
在浏览器上访问
7.动态测试,tomcat
jdk-7u79-linux-x64.tar.gz
apache-tomcat-8.5.24.tar.gz
[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server1 local]# ln -s jdk1.7.0_79/ java
[root@server1 local]# vim /etc/profile 添加java环境变量
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
[root@server1 local]# source /etc/profile
[root@server1 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin:/usr/local/java/bin
[root@server1 local]# echo $CLASSPATH
.:/usr/local/java/lib:/usr/local/java/jre/lib
[root@server1 ~]# vim test.java
public class test {
public static void main(String[] args)
{
System.out.println("hello world!");
}
}
[root@server1 ~]# javac test.java
[root@server1 ~]# java test
hello world!
[root@server1 ~]# tar zxf apache-tomcat-8.5.24.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s apache-tomcat-8.5.24/ tomcat
[root@server1 local]# cd tomcat/bin/
[root@server1 bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
访问测试:
浏览器访问http://172.25.18.1:8080/
##时间测试
[root@server1 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@server1 ROOT]# vim test.jsp
time is: <%=new java.util.Date()%>
访问测试:
浏览器访问http://172.25.18.1:8080/test.jsp
##重定向 ##tomcat与nginx发布目录最好重定向在一个目录下,否则会出问题
[root@server1 conf]# vim nginx.conf
65 location ~ \.jsp$ {
66 proxy_pass http://127.0.0.1:8080;
67 }
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload
访问测试:
浏览器访问http://172.25.18.1/test.jsp
8.负载均衡
server2 安装 jdk-7u79-linux-x64.tar.gz apache-tomcat-8.5.24.tar.gz
并设置好环境
[root@server1 conf]# vim nginx.conf
http {
upstream memcache{
server 172.25.18.1:11211;
keepalive 512;
}
upstream tomcat {
server 172.25.18.1:8080;
server 172.25.18.2:8080;
}
include mime.types;
default_type application/octet-stream;
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload
[root@server1 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@server1 ROOT]# vim test.jsp
******************************************************************************
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
*******************************************************************************
##采用sticky模块,关闭openresty的nginx,配置nginx1.12版本
[root@server1 /]# /usr/local/openresty/nginx/sbin/nginx -s stop
[root@server1 /]# vim /usr/local/lnmp/nginx/conf/nginx.conf
****************************************************
http {
upstream memcache{
server 172.25.18.1:11211;
keepalive 512;
}
upstream tomcat {
sticky;
server 172.25.18.1:8080;
server 172.25.18.2:8080;
}
include mime.types;
default_type application/octet-stream;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
****************************************************
[root@server1 /]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 /]# nginx
[root@server2 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@server2 ROOT]# vim test.jsp
******************************************************************************
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
*******************************************************************************
访问测试:
浏览器访问http://172.25.18.1/test.jsp
##交叉memcache
[root@server1 tomcat]# bin/shutdown.sh ##关掉,重新安装一个7.x的版本
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server1 local]# rm -f tomcat ##删除之前的软链接
[root@server1 local]# tar zxf ~/apache-tomcat-7.0.37.tar.gz
[root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server1 ROOT]# cp test.jsp /usr/local/tomcat/webapps/ROOT/ ##把之前的test.jsp复制过来
[root@server2 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server2 tomcat]# bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 local]# rm -f tomcat
[root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server2 local]# cp apache-tomcat-8.5.24/webapps/ROOT/test.jsp tomcat/webapps/ROOT/
[root@server1 local]# tomcat/bin/startup.sh
[root@server2 local]# tomcat/bin/startup.sh
[root@server1 lib]# 在这个目录下在jar包
[root@server2 lib]# 在这个目录下在jar包
[root@server1 jar]# rm -rf memcached-session-manager-tc6-1.6.3.jar
[root@server2 jar]# rm -rf memcached-session-manager-tc6-1.6.3.jar
[root@server2 local]# yum install memcached -y
[root@server2 local]# /etc/init.d/memcached start
Starting memcached: [ OK ]
测试:
访问测试:
浏览器访问http://172.25.18.1/test.jsp
[root@server2 tomcat]# bin/shutdown.sh 看实验结果