服務(wù)器Centos部署Spring?boot?前后端項目的詳細步驟
使用centos部署前后端項目,使用的是centos 8,后端用的是Spring,前端用的是Vue,使用下面的步驟進行部署:
- 安裝MySQL
這一步有多種方式進行安裝,可以使用包管理器,可以使用安裝包解壓縮進行安裝,但是最終都需要使用對配置文件進行配置,我建議大家可是嘗試下面的MySQL腳本就行安裝,只需要在里面改動值就可以。
vi InstallMySQL.sh
- 創(chuàng)建bash文件,填入下面的內(nèi)容
#!/bin/bash # https://blog.csdn.net/qq_41054313 #數(shù)據(jù)庫密碼 mysqlPWD="lhDream@123" echo "--MySQL5.7安裝--" echo "下載依賴環(huán)境" yum -y install wget wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm echo "開始安裝" yum -y install mysql57-community-release-el7-10.noarch.rpm yum -y install mysql-community-server echo "啟動MySQL" systemctl start mysqld.service systemctl status mysqld.service a=$(systemctl status mysqld.service | grep "active (running)" | wc -l) if [ $a -gt 0 ] then echo "啟動完成,狀態(tài)正常" #配置MySQL echo "配置MySQL" str=$(grep "password is generated for root@localhost:" /var/log/mysqld.log) localPWD=${str##*"root@localhost: "} echo "數(shù)據(jù)庫默認密碼:"$localPWD export MYSQL_PWD=$localPWD echo "重置數(shù)據(jù)庫密碼為:"$mysqlPWD mysql --connect-expired-password -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$mysqlPWD'" echo "刷新權(quán)限" export MYSQL_PWD=$mysqlPWD mysql --connect-expired-password -uroot -e "flush privileges" echo "配置遠程登錄" mysql --connect-expired-password -uroot -e "grant all privileges on *.* to 'root'@'%' identified by '$mysqlPWD' with grant option" echo "配置數(shù)據(jù)庫編碼" echo "[client]" > /etc/my.cnf echo "default-character-set=utf8" >> /etc/my.cnf echo "" >> /etc/my.cnf echo "[mysqld]" >> /etc/my.cnf echo "datadir=/var/lib/mysql" >> /etc/my.cnf echo "socket=/var/lib/mysql/mysql.sock" >> /etc/my.cnf echo "character-set-server=utf8" >> /etc/my.cnf echo "collation-server=utf8_general_ci" >> /etc/my.cnf cat /etc/my.cnf #重啟MySQL查看配置結(jié)果 systemctl restart mysqld systemctl status mysqld.service mysql --connect-expired-password -uroot -e "status" echo "安裝完成" else echo "狀態(tài)異常,安裝失敗" fi #rm -f mysql57-community-release-el7-10.noarch.rpm echo "exit"
bash InstallMySQL.sh
運行腳本,查看安裝進度
安裝Redis
vi InstallRedis.sh
#!/bin/bash # 安裝版本 redis_version="6.2.7" # 安裝目錄 #redis_installDir="/opt/module/redis" redis_installDir="/opt/bigdata/redis" install_redis() { local version=$1 local installDir=$2 local redisconf_file=$installDir/redis-$version/redis.conf # 安裝工具包 yum -y install epel-release gcc make tcl # 下載地址 local downloadUrl="http://download.redis.io/releases/redis-$version.tar.gz" if [ -z "$(command -v wget)" ]; then yum -y install wget if [ $? -eq 0 ]; then echo "wget安裝完成" else echo "wget安裝失敗,請檢查" exit 1 fi fi if [ ! -d "${installDir}" ]; then mkdir -p "${installDir}" if [ $? -eq 0 ]; then echo "安裝目錄${installDir}已創(chuàng)建" else echo "請確保您有足夠的權(quán)限來創(chuàng)建目錄,請增加權(quán)限后再次執(zhí)行" exit 1 fi fi if [ ! -f "/tmp/redis-$version.tar.gz" ] ; then wget "$downloadUrl" -P /tmp if [ $? -eq 0 ]; then echo "redis-$version.tar.gz下載成功" else echo "redis-$version.tar.gz下載失敗,請重試或手動下載到/tmp目錄下再次執(zhí)行" echo "下載地址:$downloadUrl" exit 1 fi else echo "/tmp/redis-$version.tar.gz文件已存在" fi if [ -d "${installDir}/redis-$version" ]; then echo "${installDir}/redis-$version 已存在,正在刪除..." rm -rf "${installDir}/redis-$version" fi tar -zxvf "/tmp/redis-$version.tar.gz" -C "${installDir}" if [ $? -eq 0 ]; then echo "redis-$version.tar.gz解壓成功" else echo "redis-$version.tar.gz解壓失敗,請查看異常信息后重試" exit 1 fi # 編譯和安裝 cd ${installDir}/redis-$version make && make install sed -i "s|daemonize no|daemonize yes|" "$redisconf_file" sed -i "s|bind 127.0.0.1 -::1|bind 127.0.0.1 -::1\nbind 0.0.0.0|" "$redisconf_file" sed -i "s|# requirepass foobared|# requirepass foobared|" "$redisconf_file" # sed -i "s|port 6379|port 6379|" "$redisconf_file" # sed -i "s|# cluster-enabled yes|cluster-enabled yes|" "$redisconf_file" # sed -i "s|# cluster-config-file nodes-6379.conf|cluster-config-file nodes.conf|" "$redisconf_file" # sed -i "s|# cluster-node-timeout 15000|cluster-node-timeout 5000|" "$redisconf_file" # sed -i "s|appendonly no|appendonly yes|" "$redisconf_file" echo "redis 單機版下載、安裝、配置完成" echo "redis安裝目錄:${installDir}/redis-$version" echo "單機版 進入 redis 交互界面命令:redis-cli -h $(ip addr | grep 'inet ' | awk '{print $2}'| tail -n 1 | grep -oP '\d+\.\d+\.\d+\.\d+') -p 6379" echo "集群版 進入 redis 交互界面命令:redis-cli -h $(ip addr | grep 'inet ' | awk '{print $2}'| tail -n 1 | grep -oP '\d+\.\d+\.\d+\.\d+') -c -p 30001" # mkdir -p "${installDir}/redis-$version/cluster-test" # for cluster_dir in {7000..7005} # do # local create_dir="${installDir}/redis-$version/cluster-test/$cluster_dir" # mkdir -p "$create_dir" # cp "$redisconf_file" "$create_dir" # sed -i "s|port 6379|port $cluster_dir|" "$create_dir/redis.conf" # redis-server "$create_dir/redis.conf" # echo "$cluster_dir 端口的redis啟動成功" # done # local dir=${installDir}/redis-$version/utils/create-cluster # sed -i "s|CLUSTER_HOST=127.0.0.1|CLUSTER_HOST=192.168.145.105|" "$dir/create-cluster" # sed -i "s|PROTECTED_MODE=yes|PROTECTED_MODE=no|" "$dir/create-cluster" # $dir/create-cluster start # $dir/create-cluster create } install_redis "$redis_version" "$redis_installDir" exit 0
增加執(zhí)行權(quán)限
chmod a+x /tmp/install_redis.sh
運行當前的bash文件
bash InstallRedis.sh
基本上后端需要這些工具
配置后端項目 Maven項目直接通過 clean 和 package 就可以將項目進行打包
-
先點 clean,之后點package,后端項目就打包完成。之后package完成之后會生成一個target文件,target文件內(nèi)估計生成一個xxxx.jar文件,這個jar文件就是打包的文件,如果在ide中沒有看到這個target文件,可以去項目的本地文件夾中看一下。 將jar文件上傳到服務(wù)器中
這里可以使用任何帶有ftp功能的客戶端進行上傳,Winscp,Xftp,Xterminal都可以,建議在上傳之前創(chuàng)建一個項目文件夾,單獨存放文件。
上傳之后運行jar文件
java -jar xxx.jar -spring.profiles.active=dev
jar后面的內(nèi)容可以刪除,主要是為了換一個環(huán)境進行啟動java項目
之后可以輸入服務(wù)器的網(wǎng)址進行訪問,可以一些返回的結(jié)果
spring:
datasource:
driver-class-name: com.cj.mysql.jdbc.Driver
url: jdbc:mysql://192.168.198.149:3306/xxx?useSSL=false
username: root
password: xxxxx
redis:
host: 127.0.0.1
port: 6379
# password: 115474287Zxcczld
server:
port: 8080
servlet:
context-path: /api #設(shè)置路徑前綴與前端一致,默認是/
database: 0
lettuce:
pool:
max-active: 10
max-idle: 10
min-idle: 1
time-between-eviction-runs: 10s
前端配置 nginx安裝配置
nginx 配置同樣也比較麻煩,我這邊寫了一個 一鍵安裝的腳本,可以修改內(nèi)容直接使用
前端項目打包 上傳
我們使用npm工具進行打包
npm build
在IDE打包之后會生成一個dist文件夾,將這個文件夾的內(nèi)容和剛剛的方式一樣上傳到服務(wù)器中,可以和后端項目在同一個父文件夾中
配置 nginx.conf文件
找到nginx.conf文件,進行配置,主要需要配置的是server里面的內(nèi)容,location的內(nèi)容,location后面跟的內(nèi)容是當前dist文件所在的目錄,比如當前dist文件在 www下面,那么內(nèi)容就是 /www/dist
之后重啟nginx服務(wù)
sudo service nginx restart
或者
sudo systemctl restart nginx
之后在瀏覽器中輸入ip地址之后,就會顯示前端的主頁內(nèi)容
注意
如果運行之后顯示錯誤,返回錯誤頁面,403 或者 404 可以去/var/log/nginx 中查看錯誤文件,是由于什么運行不能運行。
如果你在CentOS操作系統(tǒng)下面運行Nginx服務(wù)出現(xiàn)權(quán)限方面的問題,有可能是SELinux(Security Enhanced Linux)在起作用。SELinux提供了一種機制來限制進程的權(quán)限,以此增強系統(tǒng)的安全性。
臨時禁用SElinux
sudo setenforce 0
sudo nano /etc/selinux/config
修改如下內(nèi)容:
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted
重新運行
相信這時候已經(jīng)OK了!
到此這篇關(guān)于服務(wù)器Centos部署Spring boot 前后端項目的文章就介紹到這了,更多相關(guān)Centos部署Spring boot項目內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
網(wǎng)絡(luò)線路科普之CN2,GIA,CIA,BGP以及IPLC都是什么意思
購買海外vps或者物理服務(wù)器或者海外實體服務(wù)器托管的時候,在中國IDC服務(wù)器商中的有關(guān)網(wǎng)絡(luò)線路帶寬的術(shù)語有很多,今天專門做了一個專題,有關(guān)IPLC專線、CN2、BGP、CIA和普通線路知識普及2021-06-06如何通過Apache Bench實現(xiàn)web壓力測試
這篇文章主要介紹了如何通過Apache Bench實現(xiàn)web壓力測試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05CentOS 6.3 Rsync客戶端與Win2003 cwRsyncServer服務(wù)端實現(xiàn)數(shù)據(jù)同步
這篇文章主要介紹了CentOS 6.3 Rsync客戶端與Win2003 cwRsyncServer服務(wù)端實現(xiàn)數(shù)據(jù)同步,需要的朋友可以參考下2015-07-07vscode內(nèi)網(wǎng)訪問服務(wù)器的方法
這篇文章主要介紹了vscode內(nèi)網(wǎng)訪問服務(wù)器的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06