0,准备工作
点击:注册阿里云账号
然后做一下实名
1.连接服务器,准备环境
利用XShell连接远程云服务器,准备Jdk、mysql、tomcat、redis的安装
1.1设置服务器防火墙
开放以下端口
Redis : 6379端口
Mysql : 3306端口
HTTP : 80端口
HTTPS : 443端口
SSH : 22端口
编辑切换为居中
添加图片注释,不超过 140 字(可选)
1.2去官网下载安装包,上传到云服务器
Tomcat 8 :Tomcat 8 Software Downloads
Mysql :https://dev.mysql.com/downloads/repo/yum/
Redis:https://redis.io/download
添加图片注释,不超过 140 字(可选)
上传至云服务器然后解压
tar -xvzf redis-5.0.5.tar.gz tar -xvzf apache-tomcat-8.5.43.tar.gz
1.3安装JDK
[root@iz2ze488eeqkbsq8cxntxbz ~]# yum list | grep jdk 查看yum里的JDK版本 [root@iz2ze488eeqkbsq8cxntxbz ~]# yum install java-1.8.0-* 安装JDK1.8 [root@iz2ze488eeqkbsq8cxntxbz ~]# java -version 安装成功 openjdk version "1.8.0_222" OpenJDK Runtime Environment (build 1.8.0_222-b10) OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
1.4安装MySQL
[root@iz2ze488eeqkbsq8cxntxbz ~]# rpm -Uvh mysql80-community-release-el7-3.noarch.rpm 安装用来配置Mysql的yum源的rpm包 [root@iz2ze488eeqkbsq8cxntxbz ~]# yum install mysql-community-server 安装Mysql [root@iz2ze488eeqkbsq8cxntxbz ~]# service mysqld start Redirecting to /bin/systemctl start mysqld.service 启动Mysql [root@iz2ze488eeqkbsq8cxntxbz ~]# ps -ef | grep mysqld mysql 3131 1 9 01:57 ? 00:00:01 /usr/sbin/mysqld root 3178 2353 0 01:57 pts/0 00:00:00 grep --color=auto mysqld 查看Mysql进程 [root@iz2ze488eeqkbsq8cxntxbz ~]# grep 'temporary password' /var/log/mysqld.log 2019-08-08T17:57:03.766499Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fm;v-i#nj0oH 查看Mysql自动生成的密码 [root@iz2ze488eeqkbsq8cxntxbz ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.17 用初始密码登录Mysql mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Hr980526'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Hr980526!' -> ; Query OK, 0 rows affected (0.01 sec) 修改密码 密码需要大小写字母数字以及特殊符号混合 否则提示密码不符合策略 mysql> create user 'lzx'@'%' identified by 'Hr980526!'; Query OK, 0 rows affected (0.01 sec) 创建一个新用户供客户端连接 mysql> grant select,insert.update,delete on *.* to 'lzx'@'%'; Query OK, 0 rows affected (0.01 sec) 赋权限给新用户 mysql> show grants for 'lzx'@'%'; +----------------------------------------------------------+ | Grants for lzx@% | +----------------------------------------------------------+ | GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO `lzx`@`%` | +----------------------------------------------------------+ 1 row in set (0.00 sec) 查看权限 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) 立即生效
云服务器安装的8.0Mysql,本机5.6Mysql,由于加密方式问题,可能导致本地连接云服务器Mysql失败
编辑
添加图片注释,不超过 140 字(可选)
编辑切换为居中
添加图片注释,不超过 140 字(可选)
登录云服务器数据库,执行以下命令即可:
[root@iz2ze488eeqkbsq8cxntxbz ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 30 Server version: 8.0.17 MySQL Community Server - GPL Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Hr980526!'; Query OK, 0 rows affected (0.01 sec)
1.5安装Redis
[root@iz2ze488eeqkbsq8cxntxbz ~]# ls apache-tomcat-8.5.43 mysql80-community-release-el7-3.noarch.rpm redis-5.0.5 进入redis解压后的目录 [root@iz2ze488eeqkbsq8cxntxbz ~]# cd redis-5.0.5 利用make命令将redis源代码编译 [root@iz2ze488eeqkbsq8cxntxbz redis-5.0.5]# make 修改配置文件 [root@iz2ze488eeqkbsq8cxntxbz redis-5.0.5]# ls 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src utils [root@iz2ze488eeqkbsq8cxntxbz redis-5.0.5]# vim redis.conf 将daemonize改为yes 后台启动 # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize yes 将bind 127.0.0.1 注释掉 开放远程连接 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #bind 127.0.0.1 redis测试 [root@iz2ze488eeqkbsq8cxntxbz redis-5.0.5]# src/redis-server redis.conf 8842:C 09 Aug 2019 03:31:59.408 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8842:C 09 Aug 2019 03:31:59.408 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=8842, just started 8842:C 09 Aug 2019 03:31:59.408 # Configuration loaded [root@iz2ze488eeqkbsq8cxntxbz redis-5.0.5]# src/redis-cli 127.0.0.1:6379> set 'name' 'lzx' OK 127.0.0.1:6379> get 'name' "lzx" 127.0.0.1:6379>
1.6安装Tomcat8.5
[root@iz2ze488eeqkbsq8cxntxbz bin]# ./startup.sh Using CATALINA_BASE: /root/apache-tomcat-8.5.43 Using CATALINA_HOME: /root/apache-tomcat-8.5.43 Using CATALINA_TMPDIR: /root/apache-tomcat-8.5.43/temp Using JRE_HOME: /usr Using CLASSPATH: /root/apache-tomcat-8.5.43/bin/bootstrap.jar:/root/apache-tomcat-8.5.43/bin/tomcat-juli.jar Tomcat started. 解压后,进入tomcat的src的bin目录下 startup命令即可启动
2.项目的发布
前面已经完成了服务器和环境的配置,接下来就可以将自己的项目打包发布了。
2.1项目打包
我的项目是IDEA开发的SpringBoot项目,打包过程如下:
- 添加tomcat依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。 相当于compile,但是打包阶段做了exclude操作--> <scope>provided</scope> </dependency>
修改包类型,打成war包
<groupId>cn.lzx</groupId> <artifactId>university_shop</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
修改启动类,继承SpringBootServletInitializer类,重写config方法
@SpringBootApplication @MapperScan("cn.lzx.university_shop.mapper") @ImportResource(locations = {"classpath:kaptcha.xml"}) public class UniversityShopApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(UniversityShopApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(UniversityShopApplication.class); } }
设置打包之后包名(可选)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warName>o2o</warName> </configuration> </plugin>
完成以上设置后,利用maven打包,即可在项目目录下生成target文件夹,里面存放了生成的war包。
如果之前打过包,需要先maven clean一下,然后maven package打包。
编辑
添加图片注释,不超过 140 字(可选)
2.2项目发布
- 启动云服务器的tomcat,然后将war包放在tomcat/webapps目录
编辑切换为居中
添加图片注释,不超过 140 字(可选)
过几秒钟,tomcat会自动将该war包解压,生成对应的文件夹
编辑切换为居中
添加图片注释,不超过 140 字(可选)
然后本机浏览器输入云服务器IP/域名+项目路径即可访问
编辑切换为居中
添加图片注释,不超过 140 字(可选)
至此,项目发布完成,可以在公网进行访问,对云服务器也可以购买域名进行域名绑定。
3.注意事项
- 3.1项目发布后,本机无法访问?请求超时?
购买的云服务器配置较低,tomcat也没有进行调优,启动较慢,可以启动tomcat后等待几分钟再去访问。