一、开始
配置本地 node 环境
node
下载 官网下载,可以的话推荐使用 nvm
安装
安装记得把目录改到其他盘,这里我具体是在 D:\programming\nodejs
打开cmd
查看node
安装情况
- 配置全局环境
进入安装目录,创建文件夹node_global
和node_cache
并执行
1 2
| npm config set prefix "D:\programming\nodejs\node_global" npm config set cache "D:\programming\nodejs\node_cache"
|
环境配置:新增环境变量NODE_PATH
和添加Path
,两个值都为 D:\programming\nodejs\node_global
- 安装
hexo-cli
如果显示下面情况,恭喜你成功全局模块调用
如果出现命令未找到,或不是可执行程序
,别着急!先仔细重复 3-4 步,一般是能够给解决的,如果实在是不能解决,往后看
随便找个地方初始化文件,执行如下命令:
1 2
| mkdir hexo-blog cd hexo-blog && npm init -y
|
现在有 3 种解决方法,任选其一
- 法 1:检查全局目录是否的
hexo
模块,执行如下命令:
1
| D:\programming\nodejs\node_global\hexo
|
- 法 2:如果觉得每次加前缀麻烦,使用
link
命令链接
package.json 中新建脚本如下
1 2 3
| npm i hexo-cli
npm run hexo
|
初始化hexo
项目
- 如果是按照上一节步骤 4 过来的,则在
hexo-blog
文件下初始化
1 2
| hexo init myblog && cd myblog npm i
|
- 下载主题
1
| git clone https://github.com/iissnan/hexo-theme-next themes/next
|
在本地配置文件中设置theme
属性
- 本地执行
hexo
项目,添加start
脚本
打开 http://localhost:4000 验证效果吧
git
环境搭建
git
安装:官网下载
生成ssh
认证,执行如下命令
1 2 3 4 5
| git config --global user.name "yourname" git config --global user.email "youremail@example.com" ssh-keygen -t rsa -C "youremail@example.com"
git config --global core.autocrlf false
|
最后获取到的ssh
认证在C:\Users\yourname\.ssh
中
三、服务器配置
搭建远程Git
私库
登录到远程服务器,推荐使用Xshell 5
安装 git
CentOS
1 2 3 4
| git --version yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel yum install -y git
|
Ubuntu
1 2 3 4
| git --version sudo apt-get update apt install git
|
- 创建用户并配置其仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| useradd git
passwd git
su git cd /home/git/
mkdir -p projects/blog mkdir repos && cd repos
git init --bare blog.git cd blog.git/hooks
vi post-receive
|
1 2
| #!/bin/sh git --work-tree=/home/git/projects/blog --git-dir=/home/git/repos/blog.git checkout -f
|
添加完毕后修改权限
1 2 3 4 5
| chmod +x post-receive
exit
chown -R git:git /home/git/repos/blog.git
|
- 测试
git仓库
是否可用,另找空白文件夹
1
| git clone git@{server_ip}:/home/git/repos/blog.git
|
如果能把空仓库拉下来,就说明 git 仓库搭建成功了
- 建立
ssh
信任关系,在本地电脑
1 2 3
| ssh-copy-id -i C:/Users/{yourname}/.ssh/id_rsa.pub git@{server_ip}
ssh git@{server_ip}
|
注:此时的 ssh 登录 git 用户不需要密码!否则就有错,请仔细重复步骤 3-5
- 为了安全起见禁用 git 用户的 shell 登录权限,从而只能用 git clone,git push 等登录
1 2 3 4 5 6
| cat /etc/shells
which git-shell
vi /etc/shells
|
修改/etc/passwd
中的权限
1 2 3 4 5
| git:x:1001:1001::/home/git:/bin/bash
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
|
搭建nginx
服务器
- 下载并安装
nginx
1 2 3 4
| cd /usr/local/src wget http://nginx.org/download/nginx-1.15.2.tar.gz tar xzvf nginx-1.15.2.tar.gz cd nginx-1.15.2
|
Ubuntu系统需要的依赖库
1 2 3 4 5 6 7
| sudo apt-get update sudo apt-get install build-essential sudo apt-get install libtool sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install zlib1g-dev sudo apt-get install openssl sudo apt-get install libssl-dev
|
1 2 3 4 5 6
| ./configure ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module make && make install
alias nginx='/usr/local/nginx/sbin/nginx'
|
1
| alias nginx='/usr/local/nginx/sbin/nginx'
|
执行文件
- 配置
nginx
文件
先启动是否安装成功
修改配置文件
1 2 3 4 5 6
| nginx -s stop // 先停止nginx cd /usr/local/nginx/conf vi nginx.conf
nginx -s reload
|
http配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| user root; worker_processes 1;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#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 logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
server { listen 80; server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root /home/git/projects/blog; index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;
# location / { # root html; # index index.html index.htm; # } #}
# HTTPS server # #server { # listen 443 ssl; # server_name localhost;
# ssl_certificate cert.pem; # ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on;
# location / { # root html; # index index.html index.htm; # } #}
}
|
https配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| user root; worker_processes 1;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#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 logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
server { listen 80; server_name aliatry.com; rewrite ^/(.*) https://$host$1 permanent;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #root /home/git/projects/blog; index index.html index.htm; }
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;
# location / { # root html; # index index.html index.htm; # } #}
# HTTPS server # server { listen 443 ssl; server_name aliatry.com;
ssl_certificate cert.pem; ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location / { root /home/git/projects/blog; index index.html index.htm; } }
}
|
四、发布
至此我们就把本地和服务器的环境全部搭建完成,现在利用 hexo 配置文件进行链接
配置_config.yml
文件
- 编辑
_config.yml
的 deploy 属性
1 2 3 4 5
| deploy: type: git branch: master repository: server: git@server_ip:/home/git/repos/blog.git
|
- 在 package.json 中添加 npm 脚本
1 2 3 4
| "scripts": { "deploy": "hexo clean && hexo g -d", "start": "hexo clean && hexo g && hexo s" },
|
- 链接!这下在本地调试就用
npm start
,调试好了就上传到服务器,美滋滋~快通过你的服务器 ip 访问吧