I Course Link (free): https://edu.51cto.com/course/27025.html Author: Zhang Yanfeng, please indicate the source
First of all, let's talk about what the LNMT architecture is. The so-called LNMT architecture refers to the deployment of Nginx web server, MySQL database server, and Tomcat middleware server on the Linux operating system. In fact, it is to combine the advantages of these services together for external services. For example: Nginx, Nginx everyone should have heard of, Nginx is a WEB server, it is more dominant for static resource requests, but it is not dominant for dynamic resource requests, so we can combine something similar to Apache, Tomcat separates dynamic and static resources, because our Tomcat server is dominant in processing dynamic requests, so let them be combined to give full play to their advantages.
Explain content:
1. Environmental introduction
Two, Nginx installation
Three, MySQL installation
Four, Tomcat installation
5. Service integration test, configure Nginx load balancing and reverse proxy
1. Environmental description
We can prepare one server here, of course, you can also prepare three servers, and each server runs a service.
service | IP | operating system |
Nginx+MySQL+Tomcat | 192.168.43.101 | CentOS 7.8 |
Configure YUM source:
yum -y install wget
cd /etc/yum.repos.d/
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum -y install lrzsz net-tools
Turn off the firewall and SELinux:
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
Second, install Nginx
Create users and groups for the apache service:
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]# id nginx
uid=997(nginx) gid=995(nginx) groups=995(nginx)
Install dependent packages:
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
Download and install nginx:
[root@localhost ~]# wget http://nginx.org/download/nginx-1.19.5.tar.gz
[root@localhost ~]# tar xf nginx-1.19.5.tar.gz
[root@localhost ~]# cd nginx-1.19.5
[root@localhost nginx-1.19.5]#
./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@localhost nginx-1.19.5]# make && make install
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh
Start nginx:
[root@localhost ~]# nginx
test:
The browser enters http://192.168.43.101 to test, the following interface appears to indicate that the deployment is complete
Three, install MySQL
Install dependent packages:
[root@bogon ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
Create mysql users and groups:
[root@bogon ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@bogon ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
Download and unzip mysql:
[root@bogon ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@bogon ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
Configure mysql:
#Create a soft link, modify the owner of the file
[root@bogon ~]# cd /usr/local/
[root@bogon local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@bogon local]# chown -R mysql.mysql mysql*
[root@bogon local]# ll -d mysql
lrwxrwxrwx. 1 mysql mysql 36 Nov 26 04:57 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
Configure environment variables:
[root@bogon local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@bogon local]# source /etc/profile.d/mysql.sh
Create a data storage directory:
[root@bogon ~]# mkdir /opt/data
[root@bogon ~]# chown -R mysql.mysql /opt/data
[root@bogon ~]# ll -d /opt/data
drwxr-xr-x. 2 mysql mysql 6 Nov 26 05:00 /opt/data
Initialize the database and configure:
[root@bogon ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
#The insecure option is added here, so you can log in without a password after installation
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/local/include/mysql
[root@localhost ~]# echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig
Generate configuration file
[root@bogon ~]# cat > /etc/my.cnf <<EOF
[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
EOF
Configure the service startup script:
[root@bogon ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@bogon ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@bogon ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
Start mysql and change the password:
[root@bogon ~]# service mysqld start
[root@bogon ~]# ss -antl |grep 3306
LISTEN 0 80 [::]:3306 [::]:*
[root@bogon ~]# mysql -e "set password = password('123')"
Fourth, install Tomcat
Install the java environment:
[root@bogon ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
Download the tomcat installation package and unzip it to the specified directory:
[root@bogon ~]# wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.43/bin/apache-tomcat-9.0.43.tar.gz
[root@bogon ~]# ll apache-tomcat-9.0.43.tar.gz
-rw-r--r--. 1 root root 11437266 Nov 12 10:53 apache-tomcat-9.0.43.tar.gz
[root@bogon ~]# tar -xf apache-tomcat-9.0.40.tar.gz -C /usr/local/
Create soft link
[root@bogon ~]# cd /usr/local/
[root@bogon local]# ln -s apache-tomcat-9.0.40 tomcat
[root@bogon local]# ll | grep tomcat
drwxr-xr-x. 9 root root 220 Nov 26 00:20 apache-tomcat-9.0.40
lrwxrwxrwx. 1 root root 20 Nov 26 00:21 tomcat -> apache-tomcat-9.0.40
Write a test page in the tomcat web directory
[root@bogon ~]# cd /usr/local/tomcat/webapps/
[root@bogon webapps]# mkdir test
[root@bogon webapps]# vi test/index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hello World");
%>
</body>
</html>
Start tomcat
[root@bogon ~]# /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
Using CATALINA_OPTS:
Tomcat started.
[root@bogon ~]# 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 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 [::]:*
test:
http://192.168.43.101:8080/test
5. Service integration test, configure Nginx load balancing and reverse proxy
Configure nginx load balancing and reverse proxy:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
Add the following content under http {:
upstream tomcatsrv {
server 192.168.43.101:8080;
}
Configure the following content on the server {under http {:
location / {
proxy_pass http://tomcatsrv;
proxy_set_header x-real-ip $remote_addr;
root /usr/share/nginx/html;
index index.html index.htm;
}
Restart nginx:
[root@localhost ~]# nginx -s reload
test: