一、配置VM虚拟机
安装包均在家目录(cd ~)在root下
1、配置虚拟机ip地址
vm :
账号:root
密码:123456
修改为:
重启网络
2、linux 安装 jdk
1、创建文件夹 touch
2、批量删除 rm text* -rf
3、cd ~ 家目录、
cd / 跟目录
4、创建目录 mkdir -p /usr/local/java
5、解压 tar -zxvf 压缩包(-zxvf :循环递归解压指定文件 -C:解压到指定目录)
6、配置环境变量
vim :set nu 打开行号
vim /etc/profile
export JAVA_HOME=/usr/local/java/jdk-16/
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
. :表示当前其他路径
::相当于windows的%
$:$JAVA_HOME引用原有的路径
7、重启 source /etc/profile
3、Vsftp搭建
Vsftp:存放图片等。后期tomcat直接通过地址访问Vsftp服务器
1、创建ftp用户
使用命令:useradd ftpuser
设置密码:passwd ftpuser(密码:123456)
删除用户:userdel -rf ftpuser
2、安装Vsftp服务器
命令:yum -y install vsftpd
3、开启安全策略
getsebool:查看SElinux策略内容各项规则的布尔值
setsebool:设置Selinux策略内容各项规则的布尔值
1、getsebool -a | grep ftp
2、打开命令:
setsebool -P 具体选项 on(永久生效)
setsebool 具体选项 on(临时生效)
3、设置指定布尔值(打开,-P:永久生效)
所有操作放行:setsebool -P ftpd_connect_all_unreserved on
全局访问控制:setsebool -P ftpd_full_access on
最基本的home目录:setsebool -P tftp_home_dir on
4、修改防火墙(临时:不用管)
1、先查看防火墙是否有ftp 的端口21
iptables -L -n
-n:以数字显示,否则以ip反向解析成主机明
2、发现防火墙没有监听21端口,添加防火墙规则
iptables -I INPUT -p tcp --dport 21 -j ACCEPT
-I:插入一条新规则,默认在第一行添加
INPUT:允许该数据包通过
-p tcp:当前规则作用于tcp协议包
–dport: 目的端口
-j :采取动作
3、关闭防火墙
centos 6:service iptables stop
centos 7:systemctl stop firewalld
5、修改防火墙(永久修改)
1、在线安装防火墙
yum -y install iptables-services
2、设置并启动
systemctl enable iptables.service
3、编辑防火墙规则
vi /etc/sysconfig/iptables
4、添加ftp的21端口
-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
5、启动防火墙
//方法1
systemctl start iptables.service
//重启防火墙
systemctl restart iptables.service
//方法2
service iptales start
//方法3
service iptables restart
6、启动ftp服务
//方法1
systemctl start vsftpd
//方法2
service vsftp start
查看是否启动成功
ps -ef | grep vsftp
7、注意云服务器开机vsftp(命令不一样)
systemctl list-unit-files | grep vsftpd.service
7、配置fz(端口22)
1、下载地址:https://filezilla-project.org/download.php?type=client
不能正常启动,试试在vm打开防火墙:systemctl start firewalld
,一般情况关闭
2、测试
左边本地站点是此电脑,右边远程站点是vsftp服务器,直接在左边双击就可以实现上传。
3、效果图
8、设置fz,实现代码控制
1、设置为主动模式(fz app:编辑–>设置–>FTP–>主动模式)
9、安装tomcat
查看安装路径
sudo find / -name *tomcat*
1、上传tomcat到vm
2、创建解压目录
mkdir -p /usr/local/tomcat
3、解压到指定路径/usr/local/tomcat
,在文件目录下解压)
tar zxvf apache-tomcat-9.0.56.tar.gz -C /usr/local/tomcat
4、进入目录
cd /usr/local/tomcat/apache-tomcat-9.0.56/bin/
5、启动
./startup.sh
6、过滤管道,查看tomcat相关进程
//方法一
ps -ef | grep tomcat
//方法二
cd logs/
tailf catalina.out
7、访问tomcat
注意:目前无法访问(可以关闭防火墙)
systemctl stop iptables
systemctl stop firwalld
1、修改防火墙配置
vim /etc/sysconfig/iptables
2、重启防火墙
systemctl restart iptables
3、访问
http://192.168.124.24:8080/
10、安装Nginx
Nginx 功能:反向代理,负载均衡
正向代理:浏览器到服务器
反向代理:中间代理
下载地址:nginx
1、准备工作
1、 拷贝一份tomcat(模拟两个服务器,实现负载均衡)
cp -rf apache-tomcat-9.0.56/ apache-tomcat-9.0.56-02/
2、启动tomcat-02
./apache-tomcat-9.0.56-02/bin/startup.sh
3、关闭tomcat(端口占用)
./apache-tomcat-9.0.56-02/bin/shutdown.sh
4、查看端口占用情况
ps -ef | grep tomcat
5、修改端口号
vim apache-tomcat-9.0.56-02/conf/server.xml
1)端口修改为28005
2)端口修改为28080
6、修改首页信息
[root@localhost apache-tomcat-9.0.56-02]# cd webapps/
[root@localhost webapps]# cd ROOT/
vim index.jsp
7、启动tomcat
/usr/local/tomcat/apache-tomcat-9.0.56-02/bin/startup.sh
/usr/local/tomcat/apache-tomcat-9.0.56/bin/startup.sh
8、修改防火墙,允许apache-tomcat-9.0.56-02通过防火墙。
vim /etc/sysconfig/iptables
9、防火墙重启
systemctl restart iptables
10、访问tomcat
http://192.168.124.24:8080/
http://192.168.124.24:28080/
2、安装Nginx
1、上传压缩包到服务器
2、创建nginx安装路径
mkdir -p /usr/local/nginx
3、解压压缩包到家目录
tar zxvf nginx-1.21.4.tar.gz
4、安装nginx到指定目录
1)进入目录 cd nginx-1.21.4/
进入nginx解压目录进行文件检测
./configure --prefix=/usr/local/nginx
configure:用于nginx编译之前配置和检测工作
–prefix:指定nginx安装路径
检测报告说找不到gcc-c++
2)安装gcc-c++
yum -y install gcc-c++
安装后找不到PCRE和Zlib,三个一起安装
yum -y install gcc-c++ pcre-devel zlib-devel
-y:无需手动确定,自动安装
不安装gcc-c++、PCRE、Zlib会报错
3)安装gcc-c++、PCRE、Zlib成功以后,再执行一次./configure --prefix=/usr/local/nginx
命令
4)预编译并且安装到指定目录
make && make install
conf:配置文件
sbin:启动文件
html:默认登陆界面
logs:日志
5)启动
cd sbin/
./nginx
6)防火墙允许通过
nginx:默认80
//修改端口号
vim /etc/sysconfig/iptables
//重启
systemctl restart iptables.service
7)访问路径
http://192.168.124.24:80
5、nginx反向代理(负载均衡)
1)修改配置文件(在conf目录下)
a)修改nginx.conf文件
找到localtion
修改location内容
# root html;
# index index.html index.htm;
proxy_pass http://ego; #注释前面两个内容
b)添加内容
upstream ego {
server 192.168.124.24:8080;
server 192.168.124.24:28080;
}
c)查看配置内容是否编写正确
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
d)重启
./nginx -s reload
e)测试:服务网址,可以实现在端口8080和28080之间不停的切换,实现一个地址,访问不同tomcat,达到了负载均衡的效果。
http://192.168.124.24/
6、Nginx配置http默认路径服务资源(实现照片负载均衡)
1)修改配置文件 nginx.conf
文件路径/home/ftpuser/ego/2021/12/21
检测修改是否正确
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
重启
/usr/local/nginx/sbin/nginx -s reload
2)修改nginx.conf的用户访问
user ftpuser;
worker_processes 1;
3)重启
4)全部流程
11、安装Redis
1、创建文件夹
mkdir -p /usr/local/redis
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
在路径 /root/redis-5.0/src
make MALLOC=libc
问题2、Linux安装redis时报sh:./mkreleasehdr.sh权限不够错误
在路径 /root/redis-5.0/src
chmod +x mkreleasehdr.sh
2、启动
安装目录:
/usr/local/redis/bin
redis-cli:客户端
redis-server:服务器端
启动命令服务端:
./redis-server
切换redis安装路径:
cd /usr/local/redis/bin
登陆:
./redis-cli -p 6379 -a 123456
3、客户端工具连接(Destop Manager)
1)kill 杀死redis 进程
2)启动 ./redis-server ./redis.conf
二、java代码实现文件上传到ftp服务器
1、编写上传文件到ftp服务器的工具类
VM 删除 :rm 文件 -rf
public static void main(String[] args) {
InputStream is = null;
FTPClient client=null;
try {
//1.创建FTPClient对象
client=new FTPClient();
//2.保存FTP控制连接的字符集,必须在连接前设置
client.setControlEncoding("utf-8");
//3.指定服务器地址(端口)
client.connect("192.168.124.24",21);
//4.指定用户名和密码
client.login("ftpuser","123456");
//连接成功或者失败返回状态码
int replyCode = client.getReplyCode();
//返回230表示成功,返回530表示开机密码错误或者用户名错误或者用户权限问题
//System.out.println(replyCode);
if(!FTPReply.isPositiveCompletion(replyCode)){
client.disconnect();//关闭连接
return;
}
//5.创建流
File file=new File("C://Users//Administrator//Desktop//study//file.txt");
is = new FileInputStream(file);
//6.FTP上传路径,一般情况只创建一个文件夹,当创建多个文件夹或出错
String path = "/home/ftpuser/ego";
String temp = "";
//循环处理路径(创建多个文件)
for (String p: path.split("/")) {
temp+=p + "/";
//选择上传到FTP的路径,路径存在返回true,不存在返回false
boolean hasPath = client.changeWorkingDirectory(temp);
if(!hasPath){
//不存在路径,创建
client.makeDirectory(temp);
}
}
//从新上传路径
client.changeWorkingDirectory(path);
//单个文件上传
//选择上传到FTP的路径,路径存在返回true,不存在返回false
// boolean hasPath = client.changeWorkingDirectory(path);
//
// if(!hasPath){
// //不存在路径
// client.makeDirectory(path);
// //从新上传路径
// client.changeWorkingDirectory(path);
// }
//
//7.指定上传方式为二进制
client.setFileType(FTP.BINARY_FILE_TYPE);
//得到文件后缀
String suffix = file.getName().substring(file.getName().lastIndexOf("."));
System.out.println("文件后缀: "+suffix);
// //8.remote指定上传远程服务器的文件名,is指本地的输入流
client.storeFile(UUID.randomUUID().toString() + suffix,is);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (is != null)
is.close();
if (client !=null && client.isConnected())
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、当文件上传过慢
关闭ftp DNS逆向解析
//修改文件内容
vim /etc/vsftp/vsftpd.conf
//关闭DNS逆向解析
reverse_lookup_enable=NO
//重启服务
systemctl restart vsftpd.service
3、常见问题
1、网络正常
2、vm ip地址
3、服务VM应用全部开启
3、linux防火墙关闭
4、访问不成功,也可能是权限问题
chmod 777 ego
4、遇到int replyCode = client.getReplyCode();无法正常工作,重启防火墙和ftp服务
重启防火墙
systemctl restart iptables
重启ftp服务
systemctl start vsftpd
4、重启vm
1、重启tomcat
安装路径 /usr/local/tomcat
进入目录 cd /usr/local/tomcat/apache-tomcat-9.0.56/bin/
启动 ./startup.sh
查看相关进程
ps -ef | grep tomcat
2、重启ftp
//方法1
systemctl start vsftpd
//方法2
service vsftp start
ps -ef | grep vsftp
3、重启nginx
安装路径
cd /usr/local/nginx
启动
cd sbin/
./nginx
出现错误(端口被占用)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
杀死端口
ps -A | grep nginx
kill -9 pid1
重启
/usr/local/nginx/sbin/nginx -s reload
systemctl restart iptables.service
4、重启防火墙
systemctl restart iptables
5、关闭防火墙
systemctl stop firewalld
6、55514#0: *27 directory index of "/home/" is forbidden
当前文件夹不可访问
7、全部删除
rm * -rf
四、配置项目
ginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
杀死端口
ps -A | grep nginx
kill -9 pid1
重启
/usr/local/nginx/sbin/nginx -s reload
systemctl restart iptables.service
4、重启防火墙
systemctl restart iptables
5、关闭防火墙
systemctl stop firewalld
6、55514#0: *27 directory index of "/home/" is forbidden
当前文件夹不可访问
7、全部删除
rm * -rf