概述
服务器1 | 服务器2 | |
---|---|---|
ip | 192.168.47.137 | 192.168.47.138 |
安装的服务 | nginx mysql | tomcat |
1.服务器1上安装nginx服务
//关闭防火墙与selinux
[root@peng ~]# systemctl stop firewalld
[root@peng ~]# setenforce 0
//创建nginx服务的用户和组
[root@peng ~]# groupadd -r nginx
[root@peng ~]# useradd -r -M -s /sbin/nologin -g nginx nginx
//安装依赖环境
[root@peng ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
//安装开发工具包
[root@peng ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@peng ~]# mkdir -p /var/log/nginx
[root@peng ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@peng ~]# cd /usr/src/
[root@peng src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
//编译安装
[root@peng src]# tar xf nginx-1.14.2.tar.gz
root@peng src]# cd nginx-1.14.2
[root@peng nginx-1.14.2]# ./configure
> --prefix=/usr/local/nginx
> --user=nginx
> --group=nginx
> --with-debug
> --with-http_ssl_module
> --with-http_realip_module
> --with-http_image_filter_module
> --with-http_gunzip_module
> --with-http_gzip_static_module
> --with-http_stub_status_module
> --http-log-path=/var/log/nginx/access.log
> --error-log-path=/var/log/nginx/error.log
编译过程略...
[root@peng nginx-1.14.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
安装过程略....
//配置环境变量
[root@peng nginx-1.14.2]# cd
[root@peng ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@peng ~]# . /etc/profile.d/nginx.sh
//启动nginx
[root@peng ~]# nginx
[root@peng ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 80 :::3306 :::*
2.服务器1安装mysql服务
//安装依赖包
[root@peng ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
安装过程略...
//创建用户和组
[root@peng ~]# groupadd -r -g 306 mysql
[root@peng ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//下载二进制格式的mysql软件包
[root@peng ~]# cd /usr/src/
[root@peng src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
下载过程略...
//解压软件至/usr/local/
[root@peng src]# ls
debug kernels mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@peng src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@peng ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.23-linux-glibc2.12-x86_64 share
[root@peng ~]# cd /usr/local/
[root@peng local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@peng local]# ll
总用量 0
drwxr-xr-x. 13 root root 152 2月 20 16:01 apache
drwxr-xr-x. 6 root root 58 2月 20 15:52 apr
drwxr-xr-x. 5 root root 43 2月 20 15:53 apr-util
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
lrwxrwxrwx. 1 root root 36 2月 20 16:16 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 2月 20 16:15 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 1月 15 18:31 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
//修改目录/usr/local/mysql的属主属组
[root@peng ~]# chown -R mysql.mysql /usr/local/mysql
[root@peng ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 2月 20 16:16 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
//添加环境变量
[root@peng ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@peng ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@peng ~]# . /etc/profile.d/mysql.sh
[root@peng ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[root@peng ~]# cd /usr/local/mysql/
[root@peng mysql]# mkdir /opt/data
[root@peng mysql]# chown -R mysql.mysql /opt/data/
[root@peng mysql]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 2月 20 16:21 data
//初始化数据库
[root@peng ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-02-20T08:21:51.243481Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-02-20T08:21:51.843605Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-02-20T08:21:51.923869Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-02-20T08:21:52.023144Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9352971c-34e8-11e9-9119-000c29ec8de1.
2019-02-20T08:21:52.024217Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-02-20T08:21:52.025010Z 1 [Note] A temporary password is generated for root@localhost: 6#?IsD2r1-ar
//请注意,这个命令的最后会生成一个临时密码,此处密码是6#?IsD2r1-ar
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
//配置mysql
[root@peng ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@peng ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@peng ~]# ldconfig -v
内容略...
//生成配置文件
[root@peng ~]# vim /etc/my.cnf
[root@peng ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//配置服务启动脚本
[root@peng ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@peng ~]# sed -ri 's#^(basedir=).*#1/usr/local/mysql#g' /etc/init.d/mysqld
[root@peng ~]# sed -ri 's#^(datadir=).*#1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@peng ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@peng ~]# ps -ef|grep mysql
root 61067 1 0 16:35 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 61247 61067 0 16:35 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=peng.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 61277 12253 0 16:36 pts/1 00:00:00 grep --color=auto mysql
[root@peng ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//修改密码
//使用临时密码登录
[root@peng ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.23
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
//设置新密码
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
3.服务器2安装tomcat,并启用二个tomcat
//关闭防火墙与selinux
[root@ye ~]# systemctl stop firewalld
[root@ye ~]# setenforce 0
//java环境安装
[root@ye ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
安装过程略....
//查看安装的版本
[root@ye local]# java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
//下载tomcat
[root@ye ~]# cd /usr/src/
[root@ye src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
安装过程略....
//解压部署
[root@ye src]# ls
apache-tomcat-9.0.8.tar.gz debug kernels
[root@ye src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@ye src]# cd /usr/local/
[root@ye local]# ls
apache-tomcat-9.0.8 etc include lib64 sbin src
bin games lib libexec share
[root@ye local]# ln -s apache-tomcat-9.0.8/ tomcat
[root@ye local]# ll
总用量 0
drwxr-xr-x 9 root root 160 3月 7 15:07 apache-tomcat-9.0.8
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 11月 29 01:27 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
lrwxrwxrwx 1 root root 20 3月 7 15:07 tomcat -> apache-tomcat-9.0.8/
[root@ye local]# mkdir /usr/local/tomcat/webapps/test
[root@ye local]# cp index.jsp /usr/local/tomcat/webapps/test/
[root@ye local]# ll /usr/local/tomcat/webapps/test/
总用量 4
-rw-r--r-- 1 root root 128 3月 7 15:12 index.jsp
//启动tomcat
[root@ye local]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@ye local]# ps -ef |grep tomcat
root 1785 1 19 15:12 pts/0 00:00:02 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/localtomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 1828 1549 0 15:12 pts/0 00:00:00 grep --color=auto tomcat
[root@ye local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 127.0.0.1:25151 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 100 :::8009 :::*
LISTEN 0 5 :::873 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//启用第二个tomcat
[root@ye local]# cd /usr/src/
[root@ye src]# ls
apache-tomcat-9.0.8.tar.gz debug kernels
[root@ye src]# tar xf apache-tomcat-9.0.8.tar.gz
[root@ye src]# ls
apache-tomcat-9.0.8 apache-tomcat-9.0.8.tar.gz debug kernels
[root@ye src]# cp -a apache-tomcat-9.0.8 /usr/local/apache-tomcat2
[root@ye src]# ln -s /usr/local/apache-tomcat2/ /usr/local/tomcat2
//修改配置文件,修改端口号,避免重复
[root@ye src]# vim /usr/local/tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN"> #修改三个端口号与Tomcat不同
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8010 -->
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
//给tomcat2创建测试页
[root@ye src]# mkdir /usr/local/tomcat2/webapps/test
[root@ye src]# cd /usr/local/tomcat2/webapps/test
[root@ye test]# vim index.jsp
[root@ye test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("happy");
%>
</body>
<html>
//启动tomcat2
[root@ye test]# /usr/local/tomcat2/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.
[root@ye test]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 100 :::8081 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8006 :::*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8010 :::*
验证:
4.在服务器1上修改nginx配置文件,配置负载均衡和反向代理
[root@peng ~]# vim /usr/local/nginx/conf/nginx.conf
...
#gzip on;
#新增下面4行,配置负载均衡
upstream web.com {
server 192.168.47.138:8080; //服务器2的ip
server 192.168.47.138:8081; //服务器2的ip
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#新增下面3行 添加反向代理location
location ~* .(jsp|do)$ {
proxy_pass http://web.com;
}
location / {
root html;
index index.html index.htm;
}
//重启nginx服务
[root@peng ~]# nginx -s reload
...
5.验证
在网页上输入:192.168.47.137/test/index.jsp
刷新网页,为tomcat2测试页
最后
以上就是迷人可乐为你收集整理的两台服务器搭建lnmt架构的全部内容,希望文章能够帮你解决两台服务器搭建lnmt架构所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复