腾讯云centos7搭建javaweb服务器(亲测成功)

时间:2021-10-15 13:02:11

原文地址;http://www.2cto.com/kf/201707/654874.html

腾讯云centos7搭建javaweb服务器(亲测成功)

若干秒之后,重装完毕,即可开始搭建环境!

去官网下载jdk和tomcat的压缩包

我下载的是tar包,Linux x64 jdk-8u131-linux-x64.tar.gz

tomcat

我下载的是apache-tomcat-8.5.16.tar.gz

恩,下载好之后,

用户可以使用putty,mac用户可以使用ssh连接到服务器

使用root用户输入密码之后即可登录服务器

依次执行:

[root@VM_77_172_centos usr]# cd /usr

[root@VM_77_172_centos usr]# mkdir java

本人使用的是filezilla软件分别将下载好的jdk和Tomcat包上传到服务器中的/usr/java目录下和/usr目录下。(注意)

先安装java:

[root@VM_77_172_centos usr]# cd java

[root@VM_77_172_centos java]# tar -xvzfjdk-8u131-linux-x64.tar.gz

[root@VM_77_172_centos java]# ls

jdk1.8.0_131 jdk-8u131-linux-x64.tar.gz

[root@VM_77_172_centos java]# vim /etc/profile

单击i可进行插入,在文末插入如下三行语句:

export JAVA_HOME=/usr/java/jdk1.8.0_131

export JRE_HOME=/$JAVA_HOME/jre

export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

单击esc之后输入:x保存并退出

[root@VM_77_172_centos java]# source /etc/profile

此时,java安装完毕,可输入java -version查看:

[root@VM_77_172_centos java]# java -version

java version "1.8.0_131"

Java(TM) SE Runtime Environment (build 1.8.0_131-b11)

Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

下面开始Tomcat安装:

[root@VM_77_172_centos java]# cd ..

(注意,之前已经将tomcat包上传到/usr目录下)

[root@VM_77_172_centos usr]# tar -xvzfapache-tomcat-8.5.16.tar.gz

[root@VM_77_172_centos usr]# rm -rapache-tomcat-8.5.16.tar.gz

rm: remove regular file ’apache-tomcat-8.5.16.tar.gz'? y

[root@VM_77_172_centos usr]#mvapache-tomcat-8.5.16 tomcat

[root@VM_77_172_centos usr]# ls

bin games java lib64 local share tmp

etc include lib libexec sbin src tomcat

[root@VM_77_172_centos tomcat]# /usr/tomcat/bin/startup.sh

Using CATALINA_BASE: /usr/tomcat

Using CATALINA_HOME: /usr/tomcat

Using CATALINA_TMPDIR: /usr/tomcat/temp

Using JRE_HOME: //usr/java/jdk1.8.0_131/jre

Using CLASSPATH: /usr/tomcat/bin/bootstrap.jar:/usr/tomcat/bin/tomcat-juli.jar

Tomcat started.

此时,tomcat安装完毕,接下来打开端口:

[root@VM_77_172_centos tomcat]# systemctl stop firewalld.service

[root@VM_77_172_centos tomcat]# systemctl disable firewalld.service

[root@VM_77_172_centos tomcat]# systemctl mask firewalld.service

Created symlink from /etc/systemd/system/firewalld.service to /dev/null.

[root@VM_77_172_centos tomcat]# cd ~

[root@VM_77_172_centos ~]# yum install iptables-services -y

[root@VM_77_172_centos ~]# systemctl enable iptables

Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

[root@VM_77_172_centos ~]# systemctl start iptables

[root@VM_77_172_centos ~]# systemctl status iptables

[root@VM_77_172_centos usr]# systemctl unmask firewalld

Removed symlink /etc/systemd/system/firewalld.service.

[root@VM_77_172_centos usr]# systemctl start firewalld

[root@VM_77_172_centos usr]# firewall-cmd --zone=public --add-port=8080/tcp --permanent

success

[root@VM_77_172_centos usr]# firewall-cmd --zone=public --add-port=80/tcp --permanent

success

[root@VM_77_172_centos usr]# firewall-cmd --zone=public --add-port=22/tcp --permanent

success

[root@VM_77_172_centos usr]# firewall-cmd --zone=public --add-port=3306/tcp --permanent

success

[root@VM_77_172_centos usr]# firewall-cmd --reload

success

至此,不出意外的话,就可以外网访问服务器ip:8080看到Tomcat初始界面了!

接下来,安装mysql

[root@VM_77_172_centos usr]# cd ~

[root@VM_77_172_centos ~]# wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm

[root@VM_77_172_centos ~]# rpm -ivh mysql57-community-release-el7-7.noarch.rpm

[root@VM_77_172_centos ~]# yum install mysql-server

中间输入若干次y

[root@VM_77_172_centos ~]# yum install mysql-devel

中间输入若干次y

[root@VM_77_172_centos ~]# yum install mysql

检查一下MySQL:

[root@VM_77_172_centos ~]# rpm -qa|grep -i mysql

mysql-community-libs-5.7.18-1.el7.x86_64

mysql-community-libs-compat-5.7.18-1.el7.x86_64

mysql57-community-release-el7-7.noarch

mysql-community-common-5.7.18-1.el7.x86_64

mysql-community-client-5.7.18-1.el7.x86_64

mysql-community-server-5.7.18-1.el7.x86_64

mysql-community-devel-5.7.18-1.el7.x86_64

[root@VM_77_172_centos ~]# service mysqld start

Redirecting to /bin/systemctl start mysqld.service

[root@VM_77_172_centos ~]# vim /etc/my.cnf

添加一条语句:skip-grant-tables

保存退出

[root@VM_77_172_centos ~]# service mysqld restart

Redirecting to /bin/systemctl restart mysqld.service

[root@VM_77_172_centos ~]# mysql -u root

即可进入mysql

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update mysql.user set authentication_string=password('123456') where user='root';

Query OK, 1 row affected, 1 warning (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> exit;

Bye

[root@VM_77_172_centos ~]# vim /etc/my.cnf

删除掉刚才添加的那条语句,保存退出。

[root@VM_77_172_centos ~]# service mysqld restart

Redirecting to /bin/systemctl restart mysqld.service

[root@VM_77_172_centos ~]# mysql -uroot -p

输入密码登录MySQL

mysql> set global validate_password_policy=0;

Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=6;

Query OK, 0 rows affected (0.00 sec)

mysql> set PASSWORD = PASSWORD('123456');

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set host='%' where user='root' and host='localhost';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> quit

Bye

至此,MySQL也安装完毕!

享受没有配置错误的流程吧!


  • 域名:
  • 有一个服务器已经部署好项目
  • 已做域名解析
  • 使用IP:8080/项目名称 能够访问项目

    • 进入tomcat文件夹

    cd 到你的tomcat下

  • 修改server.xml文件中监听端口

  • 修改前是这样的:

cd conf vim server.xml <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />修改后是这样的:
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> *也就是说将监听端口,修改为默认的80端口。*
  • 继续修改server.xml中Host
找到 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> 修改为: <Host name="你的域名" appBase="webapps" unpackWARs="true" autoDeploy="true">
以及在该<Host>...</Host>标签内添加<Context />标签 <Context path="" docBase="/myApp" /> docBase属性填写你的项目位置,最好是绝对路径即...../webapps/myApp。
  • 重启tomcat

进入bin 
./shutdown.sh

./startup.sh

  • 打开浏览器

输入域名