LNMT(Linux+Nginx+MySQL+Tomcat)实战
L(Linux)N(Nginx)M(MySQL)T(tomcat)
1、环境
2、nginx 编译安装
3、MySQL二进制包安装
4、tomcat二进制包安装
5、整合
1、环境准备说明
服务 IP 操作系统
Nginx+MySQL+Tomcat 192.168.43.101 centos7
1、本地yum配置
2、能够连接外网
#配置YUM源:
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
#关闭防火墙和SELinux
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
2、安装Nginx
创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]# id nginx
uid=997(nginx) gid=995(nginx) groups=995(nginx)
安装依赖包
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
下载并安装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
启动nginx
[root@localhost ~]# nginx
测试
http://IP
3、安装MySQL
安装依赖包
[root@bogon ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建mysql用户和组
[root@bogon ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@bogon ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
下载并解压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/
配置mysql
#创建软链接,修改文件属主
[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/
#配置环境变量
[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
创建数据存放目录
[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
初始化数据库,并配置
[root@bogon ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
#这里加了insecure选项,所以安装好后可以不用密码登录
[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
生成配置文件
[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
配置服务启动脚本
[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
启动mysql,并修改密码
[root@bogon ~]# service mysqld start
[root@bogon ~]# ss -antl |grep 3306
LISTEN 0 80 [::]:3306 [::]:*
[root@bogon ~]# mysql -e "set password = password('123')"
4、安装tomcat
安装java环境
[root@bogon ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
下载tomcat安装包,并解压至指定目录
[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/
#创建软链接
[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
在tomcat的网页目录中写一个测试页面
[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>
启动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 [::]:*
测试:
http://192.168.43.101:8080/test
5、服务整合测试,配置Nginx负载均衡和反向代理
配置nginx负载均衡和反向代理
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
在http {下添加如下内容:
upstream tomcatsrv {
server 192.168.43.101:8080;
}
在http {下的server {配置如下内容:
location / {
proxy_pass http://tomcatsrv;
proxy_set_header x-real-ip $remote_addr;
root /usr/share/nginx/html;
index index.html index.htm;
}
重启nginx
[root@localhost ~]# nginx -s reload
测试