Loading

使用阿里云服务器搭建自己的开源博客系统

如果只是搭建博客系统,平常记录学习笔记使用,推荐大家购买阿里云的轻量级应用服务器,香港的,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

 

posted @ 2019-08-26 17:28  Reasonzzy  阅读(3768)  评论(2编辑  收藏  举报