目錄
服務器:騰訊雲服務器,服務器版本:centos7.6.1810
如何查看版本號?如下命令:
[root@VM_0_11_centos /]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
1.yum環境配置
騰訊雲的這款服務器默認自帶了yum,如果你的服務器運行這個命令報錯,請自行安裝。
因爲下一步需要安裝jdk,所以先直接使用命令查看服務器是否已經安裝了jdk:
yum list installed | grep java*
結果出現如下提示:
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fats
大意是服務器上的數據倉庫太老了,需要更新到最新的版本。直接按照提示,執行run後面的語句
yum makecache fats
yum配置完成之後,再執行yum list installed | grep java* 查看是否有已安裝的jdk環境,本服務器上沒有,直接進行下一步。若服務器有的話需要使用yum remove java*先刪除再進行下一步。
2.jdk1.8安裝(yum)
yum list java*
出現一大片java不同版本,找到我們需要的版本
... ...
java-1.8.0-openjdk.i686 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk.x86_64 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-accessibility.i686 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-demo.i686 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-demo.x86_64 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-devel.i686 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-devel.x86_64 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-headless.i686 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-javadoc-zip.noarch 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-src.i686 1:1.8.0.252.b09-2.el7_8 updates
java-1.8.0-openjdk-src.x86_64 1:1.8.0.252.b09-2.el7_8 updates
... ...
安裝:
yum install java-1.8.0-openjdk.x86_64
中間需要選擇的,輸入y回車即可
安裝完成之後,驗證:
[root@VM_0_11_centos ~]# java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
出現版本信息表示安裝成功。
3.tomcat安裝
因爲我要使用nginx配置證書,nginx+tomcat配置負載均衡,所以不用yum方式安裝tomcat,使用yum方式安裝tomcat的方式參見CentOS 6.9/7通過yum安裝指定版本的Tomcat
首先下載tomcat包:
wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.90/bin/apache-tomcat-7.0.90.tar.gz
解壓:
tar -zxvf apache-tomcat-7.0.90.tar.gz
改名:
mv apache-tomcat-7.0.90 kuaf
啓動:
# /server 是我存放tomcat的路徑
sh /server/kuaf/bin/startup.sh
這個時候訪問一下對應的網址,熟悉的界面出現,tomcat啓動成功
tomcat的配置我就不講了,不知道的各位自行百度。
4.mysql安裝和配置(yum)
參考資料:CentOS7 yum 安裝與配置MySQL5.7
由於yum庫中沒有mysql的包,所以我們要自己配置yum源
# 下載mysql源安裝包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安裝mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm
配置好源之後,開始安裝(安裝過程超級長,主要是要下載東西,速度一言難盡,當然如果網速好的話還是很快的):
yum install mysql-community-server
安裝好之後啓動mysql服務
systemctl start mysqld
或者
service mysqld start
然後使用以下命令測試是否啓動成功:
[root@VM_0_11_centos ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
若返回的結果是密碼錯誤,則表示服務啓動成功。
接下來,將服務設置成開機啓動:
[root@VM_0_14_centos mysql]# systemctl enable mysqld
[root@VM_0_14_centos mysql]# systemctl daemon-reload
接下來,登錄,修改密碼。
首先,查看自動生成的密碼
[root@VM_0_14_centos mysql]# grep 'temporary password' /var/log/mysqld.log
2020-06-08T08:47:30.224733Z 1 [Note] A temporary password is generated for root@localhost: 5o%hKkOl:,S6
使用用戶名root,密碼5o%hKkOl:,S6登錄進去,修改密碼。
[root@VM_0_14_centos mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30
Copyright (c) 2000, 2020, 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> show databases
-> ;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'TestRoot@123132';
Query OK, 0 rows affected (0.00 sec)
注意:1.修改密碼之前你是不能進行任何操作的。2.密碼默認必須包含大小寫字母、數字和符號。
接下來,我們用本地的數據庫連接工具連接一下看看:
連不進去是正常的,彆氣餒,你還需要配置遠程賬戶,不然你就只能在連接服務器這個小黑窗裏面寫數據庫代碼了。
配置遠程賬戶步驟:
(1)進入mysql數據庫:
[root@VM_0_14_centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> 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>
(2)創建用戶,賦予權限
mysql> create user 'remoteUser'@'localhost' identified by 'RemoteUsr@2020';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'remoteUser'@'localhost' identified by 'RemoteUsr@2020' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
(3)查到自己的公網IP:百度ip即可
(4)修改剛添加的用戶的IP爲自己目前的公網IP
mysql> update user set Host='xxx.xxx.xxx.xxx' where User='remoteUser';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
注意:這裏的xxx.xxx.xxx.xxx是自己的公網IP;更新一定要加flush privileges不然修改不會生效。
接下來就可以遠程登錄了:
附:mysql中user表記錄的密碼修改方法
#mysql 5.7以上
update user SET authentication_string = password('newpassword') WHERE user = 'root';
#mysql 5.7以下
update user SET password = password('newpassword') WHERE user = 'root';
flush privileges;
我在安裝過程中出現了一個問題:mysqld_pre_systemd[26821]: 2020-06-08T08:11:58.961782Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
出現的原因是之前安裝了別的版本的mysql所以默認數據路徑下有舊的數據文件,刪除就好了,參照:https://blog.csdn.net/nkshuangyuan2012/article/details/106624352
5.nginx安裝和配置(yum)
查看nginx版本:yum list -y nginx
[root@VM_0_14_centos ~]# yum list -y nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Packages
nginx.x86_64
安裝nginx:yum -y install nginx,中間需要確認
[root@VM_0_3_centos /]# yum -y install nginx
Loaded plugins: fastestmirror, langpacks
...............................
...............................
............................
.....
Complete!
啓動:
[root@VM_0_14_centos nginx]# cd /usr/sbin
[root@VM_0_14_centos sbin]# ./nginx
出現如下界面,表示nginx啓動成功:
配置nginx映射到tomcat服務器:vim /etc/nginx/nginx/conf
重點配置如下,其中8080是我的tomcat的http端口
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:8080
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
接下來,把java服務部署上去,啓動tomcat
sh /root/tomcat/bin/startup.sh
訪問網址,出現我們的網站,部署成功:
接下來配置證書,使用https訪問,這樣網址導航欄就不會出現不安全字樣了
6.證書配置
證書的申請請參考:https://blog.csdn.net/nkshuangyuan2012/article/details/106637704
下載申請好的證書,解壓,找到我們需要的nginx證書,傳到centos上的任意位置
在nginx配置文件中,server模塊下,增加一個server,按照騰訊雲的證書部署指引格式來寫就好了
# Settings for a TLS enabled server.
server {
listen 443;#SSL 訪問端口號爲 443
server_name xxxx.xxxx.com; #填寫綁定證書的域名
ssl on;#啓用 SSL 功能
ssl_certificate cert/1_xxxx.xxxx.com_bundle.crt;#證書文件名稱
ssl_certificate_key cert/2_xxxx.xxxx.com.key;#私鑰文件名稱
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;##請按照這個套件配置,配置加密套件,寫法遵循 openssl 標準。
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
然後重新加載nginx配置文件
[root@VM_0_14_centos mywebapps]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@VM_0_14_centos mywebapps]# nginx -s reload
使用https模式訪問,就變成了這樣
--------------完結的分割線----------------------------
加油