详细阿里云CentOS服务器搭建
安装了一晚上的阿里云服务器,错误满满,最终还是搭建成功了。
搭建服务器,要先购买一个服务器,可以买阿里云、腾讯云等服务器供应商。我是阿里云做活动,99块钱买的,感觉很值了。
首先安装Jdk和Tomcat,直接去官网下载需要的版本,也可以点击链接下载。 链接: https://pan.baidu.com/s/1IazjVB5c_HAy2Quowy-HHA 密码: kr69
链接里有SSHSecureShellClient-3.2.9客户端,我是用这个的,自己可以选择其他的远程连接工具。
安装完SSHSecureShellClient-3.2.9,可以看到
创建soft文件夹,把软件包拖拽,直接上传到linus的soft文件夹中
1.安装JDK
查看当前Linux系统是否已经安装java
(1)[root@CentOS ~]# rpm -qa | grep java
(2)[root@CentOS ~]# rpm -e --nodeps 要卸载的软件名
新买的服务器前两步基本不用执行
(3)解压jdk到/usr/local下
[root@CentOS soft]# tar –xvf jdk-7u71-linux-i586.tar.gz –C /usr/local
(4)配置jdk环境变量,打开/etc/profile配置文件,将下面配置拷贝进去
#set java environment
JAVA_HOME=/usr/local/jdk1.7.0_71
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
(5)重新加载/etc/profile配置文件source /etc/profile
(6)[root@CentOS local]# java -version 可以查看java的版本
2.安装tomcat
(1)直接解压tomcat
[root@CentOS ~]# tar -xvf apache-tomcat-7.0.57.tar -C /usr/local
[root@CentOS ~]# cd /usr/local/
[root@CentOS local]# mv apache-tomcat-7.0.57 tomcat(改文件夹名字)
[root@CentOS local]# tomcat/
[root@CentOS tomcat]# bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.7.0_71
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
(2)此时,tomcat安装完毕,接下来打开端口:
[root@CentOS tomcat]# systemctl stop firewalld.service(关闭firewalld服务)
[root@CentOS tomcat]# systemctl disable firewalld.service(在开机时禁用firewalld服务)
[root@CentOS tomcat]# systemctl mask firewalld.service(屏蔽服务(让它不能启动))
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@CentOS tomcat]# cd ~
[root@CentOS ~]# yum install iptables-services -y(安装 iptables-services )
[root@CentOS ~]# systemctl enable iptables(在开机时启用firewalld服务)
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@CentOS ~]# systemctl start iptables(启动 iptables服务)
[root@CentOS ~]# systemctl status iptables(显示 iptables的状态)
[root@CentOS usr]# systemctl unmask firewalld
Removed symlink /etc/systemd/system/firewalld.service.
[root@CentOS usr]# systemctl start firewalld
[root@CentOS usr]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@CentOS usr]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@CentOS usr]# firewall-cmd --zone=public --add-port=22/tcp --permanent
success
[root@CentOS usr]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
(打开需要的端口)
[root@CentOS usr]# firewall-cmd --reload (重新载入)
success
现在应该可以外网访问服务器ip:8080看到Tomcat初始界面了!
3.安装mysql
(1)CentOS7的yum源没有mysql,要先下载mysql的repo源
[root@CentOS ~]# wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
(2)安装mysql rpm包
[root@CentOS ~]# rpm -ivh mysql57-community-release-el7-7.noarch.rpm
(我安装时提示需要依赖包,如libncurses.so.5 is needed by MySQL-client-5.6.22-1.el6.i686
[root@CentOS ~]#yum install libncurses.so.5
缺什么依赖包,安装就行)
(3)安装mysql
[root@CentOS ~]# yum install mysql-server
中间输入若干次y
[root@CentOS ~]# yum install mysql-devel
中间输入若干次y
[root@CentOS ~]# yum install mysql
(4)检查一下MySQL:
[root@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
(5)[root@CentOS ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
(6)[root@CentOS ~]# vim /etc/my.cnf (mysql的启动参数)
添加一条语句:skip-grant-tables (不使用mysql数据库里的信息来进行访问控制)
保存退出
(7)[root@CentOS ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
(8)[root@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'; (更改mysql密码)
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
(9)[root@CentOS ~]# vim /etc/my.cnf
删除掉刚才添加的那条语句,保存退出。
[root@CentOS ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[root@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;(但是密码也至少为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;(mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效。)
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'; (把已存在的用户(例如root)修改成允许远程主机访问(不增加记录),需要指定访问主机,可以把%替换为主机的IP或者主机名)
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
4.修改tomcat的默认端口
[root@CentOS ~]# cd /usr/local/tomcat/conf
[root@CentOS 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" />
可以从文件底部开始找这个标签<Host></Host>,在该标签内添加 <Context path="" docBase="/Gulang" /> docBase属性填写你的项目位置,一般项目路径为 .../webapps/Gulang。
重启tomcat
现在你可以统过外网IP直接访问了。