服务器安装Hexo博客

基础库

1
2
3
sudo apt-get install git-core
sudo apt-get install g++
sudo apt-get install cURL

安装Node.js

hexo官方推荐的安装方法是使用nvm,这里我们也使用nvm进行安装,当然你也可以使用直接安装,不过貌似nvm安装之后会直接在个人目录下产生.nvm目录并且通过.bashrc或者.bash_profile进行开机加载,然后其他的nvm操作都会保存在>.nvm目录,这样方便以后升级或者重装系统,相当于绿色软件了。

1
2
3
4
wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
reboot
nvm install 4
nvm ls

会输出:

1
2
3
4
v4.2.6
node -> stable (-> v4.2.6) (default)
stable -> 4.2 (-> v4.2.6) (default)
iojs -> N/A (default)

然后:

1
2
nvm use 4.2.6
nvm alias default 4.2.6

为了避免重启后找不到hexo指令的错误,在/etc/rc.local中添加:

1
nvm use 4.2.6

安装,配置Hexo

1
2
3
4
5
6
7
8
9
10
11
12
13
npm install -g hexo-cli

#在home新建一个文件夹,用于本地存放博客
$mkdir Blogs
$cd Blogs

#用hexo命令在Blogs目录中初始化环境
$hexo init
$npm install

#本地服务开启
hexo server
INFO Hexo is running at http://0.0.0.0:4000/. Press Ctrl+C to stop.

可以通过ip:4000查看到默认页面。

安装Dropbox

32-Bit:

1
wget -O - "https://www.dropbox.com/download?plat=lnx.x86" | tar xzf -

64-Bit:

1
wget -O - "https://www.dropbox.com/download?plat=lnx.x86_64" | tar xzf -

接着,从新建的 .dropbox-dist 文件夹运行 Dropbox 守护程序。

1
~/.dropbox-dist/dropboxd

Next we will download the command line management script. This script requires Python, so make sure you have it installed. You can see if Python is installed with python -V

1
2
3
4
5
wget https://www.dropbox.com/download?dl=packages/dropbox.py -O dropbox_manager.py

chmod +x dropbox_manager.py

./dropbox_manager.py

如果想要不同步某些文件夹,就可以:

1
./dropbox_manager.py exclude add ~/Dropbox/Photos/

Running Dropbox on system startup

Here's a sample init.d script for Debian/Ubuntu and the event.d sample.

1
sudo nano /etc/init.d/dropbox

输入:

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
#!/bin/bash
# dropbox service
DAEMON=.dropbox-dist/dropboxd

start() {
echo "Starting dropbox..."
if [ -x /root/$DAEMON ]; then
HOME="/root" start-stop-daemon -b -o -c root -S -u root -x /root/$DAEMON
fi
}

stop() {
echo "Stopping dropbox..."
if [ -x /root/$DAEMON ]; then
start-stop-daemon -o -c root -K -u root -x /root/$DAEMON
fi
}

status() {
dbpid=`pgrep -u root dropbox`
if [ -z $dbpid ]; then
echo "dropboxd not running."
else
echo "dropboxd running (pid $dbpid)"
fi
}


case "$1" in
start)
start
;;

stop)
stop
;;

restart|reload|force-reload)
stop
start
;;

status)
status
;;

*)
echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}"
exit 1

esac

exit 0

Set it to start automatically on system boot:

1
2
sudo chmod +x /etc/init.d/dropbox
sudo update-rc.d dropbox defaults

To remove the service, run

1
sudo update-rc.d -f dropbox remove

To start the service, run

1
sudo /etc/init.d/dropbox start

To stop it, run

1
sudo /etc/init.d/dropbox stop

To restart it, run

1
sudo /etc/init.d/dropbox restart

You can also check running status in the Ubuntu/Debian and Gentoo versions with:

1
sudo /etc/init.d/dropbox status

部署Nginx

1
2
3
sudo apt-get install nginx

sudo vim /etc/nginx/conf.d/blog.conf

写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name example.com; # 自己的域名

root /home/xxx/public; # 刚才说的路径
access_log /var/log/nginx/blog_access.log;
error_log /var/log/nginx/blog_error.log;

# 这里是针对静态资源文件做个缓存
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 1d;
add_header Pragma public;
add_header Cache-Control "public";
}

# 这里就是把请求转给我们的静态文件了
location / {
try_files $uri $uri/ /index.html;
}
}

如果使用了hexo-pdf插件,并且将文件放到服务器上,则还需添加:

1
2
3
location ~* \.(pdf)$ {
add_header "Access-Control-Allow-Origin" "http://nagland.github.io";
}

由于nginx的运行用户没有权限访问网站所在的目录,检查1

1
/etc/nginx/nginx.conf

可以将user的值改为当前用户名。

重启:

1
service nginx restart

  1. http://www.leyar.me/hexo-nginx-403-forbidden/↩︎