使用阿里雲服務器搭建自己的開源博客系統


如果只是搭建博客系統,平常記錄學習筆記使用,推薦大家購買阿里雲的輕量級應用服務器,香港的,288/年,24/月,如圖:

阿里雲官網www.aliyun.com

登錄之后點擊輕量應用服務器

 

點擊立即購買

 

選擇香港或者新加坡的,境外的服務器不需要備案,可以省很多事情

默認是給你選擇應用鏡像WordPress,這個是給不會建站的小白用的,我們選擇系統鏡像centos

購買時長隨你自己選擇,我這里是購買一年,點擊立即購買。

購買之后如果不能用ssh工具鏈接,有可能是你防火牆規則沒有添加,添加一下就好了。

 

 確保能夠使用ssh工具連接之后,然后我們開始建站,建站的方式有很多種,

你可以自己開發一個博客系統,也可以使用開源的,我這里使用的開源的mtons

gitee地址: https://github.com/langhsu/mblog
github地址:https://gitee.com/mtons/mblog
官方文檔: https://langhsu.github.io/mblog/#/
官網地址:http://www.mtons.com/

官網目前已不能訪問,可能是作者域名或者服務器到期了,只要項目還在就沒問題。

好了,我們開始建站

所需環境jdk1.8、mysql5.7

可以參考這兩個博客進行安裝

https://www.cnblogs.com/reasonzzy/p/11150130.html

https://www.cnblogs.com/reasonzzy/p/11150131.html

1.將代碼從遠程倉庫down下來

git clone https://github.com/langhsu/mblog

 

有幾個地方是需要做修改的,

application.yml

8080是一個常用的端口,如果不想用,就改成其他端口

 同時添加防火牆規則

 

數據庫地址localhost改成你服務器的ip

作者數據處理框架用的jpa,是不需要導入數據的,但是需要建立數據庫

數據庫名稱與配置文件的保持一致。

如果你不想用作者默認的頭像或者logo,也可以換。

把這三張圖片替換成你自己的logo

數據庫這張表顯示的內容是頁眉跟頁腳

 

再改一下默認頭像的地址,

 我這里還是用的默認的

你自己可以配置一下nginx靜態資源

這里弄好之后,我們開始打包項目

打包成功之后將target目錄下的jar上傳到linux服務器

上傳好之后,使用命令后台運行並且輸出日志

nohup java -jar mblog-latest.jar >mblog-latest.log 2>&1 &

源碼里面有一個run.sh執行腳本,把那個上傳之后改一下然后授權就可以用了。

完整腳本

#!/bin/bash
## java env
export JAVA_HOME=/usr/java/jdk1.8.0_131
export JRE_HOME=$JAVA_HOME/jre

API_NAME=mblog-latest
JAR_NAME=$API_NAME\.jar
#PID  代表是PID文件
PID=$API_NAME\.pid

#使用說明,用來提示輸入參數
usage() {
    echo "Usage: sh 執行腳本.sh [start|stop|restart|status]"
    exit 1
}

#檢查程序是否在運行
is_exist(){
  pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}

#啟動方法
start(){
  is_exist
  if [ $? -eq "0" ]; then 
    echo ">>> ${JAR_NAME} is already running PID=${pid} <<<" 
  else 
    nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME >$API_NAME.log 2>&1 &
    echo $! > $PID
    echo ">>> start $JAR_NAME successed PID=$! <<<" 
   fi
  }

#停止方法
stop(){
  #is_exist
  pidf=$(cat $PID)
  #echo "$pidf"  
  echo ">>> api PID = $pidf begin kill $pidf <<<"
  kill $pidf
  rm -rf $PID
  sleep 2
  is_exist
  if [ $? -eq "0" ]; then 
    echo ">>> api 2 PID = $pid begin kill -9 $pid  <<<"
    kill -9  $pid
    sleep 2
    echo ">>> $JAR_NAME process stopped <<<"  
  else
    echo ">>> ${JAR_NAME} is not running <<<"
  fi  
}

#輸出運行狀態
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo ">>> ${JAR_NAME} is running PID is ${pid} <<<"
  else
    echo ">>> ${JAR_NAME} is not running <<<"
  fi
}

#重啟
restart(){
  stop
  start
}

#根據輸入參數,選擇執行對應方法,不輸入則執行使用說明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac
exit 0

如果不輸出日志 把 $API_NAME.log 改成 /dev/null

如果就是當前路徑,就把target跟前面的路徑刪除,然后授權

chmod 777 run.sh

如果是docker部署,需要先安裝docker,

參考文檔:https://www.cnblogs.com/reasonzzy/p/11127296.html

然后自己編寫Dockerfile文件,作者自己寫的不能用,

他上面寫的用的h2的數據庫,構建的時候有問題,我們自己來寫一個Dockerfile

#docker build -t mblog:latest .
#docker run -d --name mblog-latest --restart=always -p 7100:7100 mblog:latest 
FROM openjdk:8-jdk-alpine
EXPOSE 7100
ADD mblog-latest.jar /mblog-latest.jar
ENTRYPOINT ["java","-jar","mblog-latest.jar"]

 記得將這兩個文件放在同一目錄下

構建鏡像

docker build -t mblog:latest .

 

運行容器

docker run -d --name mblog-latest --restart=always -p 7100:7100 mblog:latest

然后查看日志

docker logs 0adaa3b59654

出現項目訪問地址說明啟動成功了,

將docker中項目日志掛載在宿主機,需要自己改一下application.yml文件

修改內容

file: ./logs/mblog-latest.log

重新部署

docker run -d --name mblog-latest -v /app/mblog/logs:/logs/ -p 7100:7100 mblog:latest

查看日志

 

接下來,我們還可以配置nginx端口映射

 拉取鏡像

docker pull nginx

創建目錄

mkdir -p /data/nginx/{conf,conf.d,html,logs}

創建配置文件nginx.conf

vim /data/nginx/conf/nginx.conf

文件內容

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  reasonzzy.cn;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://pic; 
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    upstream pic{
                server localhost:7100;
    }

}

 

修改內容說明

運行nginx容器

docker run --name mynginx -d -p 80:80  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx

 

然后就可以通過域名訪問你的項目了。

 

之前改的地方成功顯示出來了。

阿里雲ssl證書配置-nginx

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log  /var/log/nginx/error.log;    #日志存放目錄
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    server {
        listen 443 ssl;
        server_name www.reasonzzy.cn;     #你的域名
        
        root /var/www/reasonzzy.cn;        #前台文件存放文件夾,可改成別的
        
        ssl_certificate  cert/2728558_www.reasonzzy.cn.pem;        #改成你的證書的名字
        ssl_certificate_key cert/2728558_www.reasonzzy.cn.key;    #你的證書的名字
        
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        
        location / {
            proxy_pass http://pic;
        }
    }
    server {
        listen 80;
        server_name www.reasonzzy.cn;    #你的域名
        rewrite ^(.*)$ https://$host$1 permanent;    #把http的域名請求轉成https
    }    

    upstream pic{
                server localhost:7100;
    }

}

詳情參考阿里雲官方文檔,有詳細視頻講解

購買證書頁面: https://yundunnext.console.aliyun.com/?spm=5176.2020520207.products-recently-visited.1.5a3b4c12YBA2xa&p=cas#/overview/cn-hangzhou

nginx證書部署文檔: https://help.aliyun.com/document_detail/98728.html?spm=5176.2020520163.0.0.4292aFCtaFCthD

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM