- 1. Overall logic:
- 2. What do you need
- 3. Install Nodejs
- 4. Setup Dropbox
- 5. Start Dropbox Service
- 6. Install Hexo
- 7. Publication Automation
- 8. Install nginx
- 9. Setup the Rule
The major reference for this tutorial is 用Hexo+Vps搭建博客并用Dropbox同步自动发布.
Hexo suits all my needs, it is a great tool. Therefore it is worth all the pain to set up.
After some trial and fail, I finally figured out how to integrate my Digit Ocean CentOS VPS with Dropbox for Hexo to automate the publication process. With the help of this new setup, the Dropbox folder is the only thing I need to focus on. Whatever operation done there will be monitored by my VPS, publications will be automated by the script running on my server!
Run Dropbox on my VPS, set up a rule that monitors changes and trigger
hexo g command to automatically publish changes to my site.
What do you need
- A VPS. I am using Digit Ocean‘s 512MB 20GB VPS, which is $5/month.
- Dropbox account.
- A domain. I got mine from dot.tk.
We want to install Nodejs and have it run as a service from after boot.
To install it, according to my first reference, we need to install epel first. I do not know why we need epel for Nodejs, if you do, please let me know.
To check if you have epel installed already:
If you seee something like the following, it means you have it installed.
Otherwise you need to install epel.
Add epel key:
Install the package
Also, we need
yum repolist again to make sure you have epel installed.
Then wen can start installing nodejs.
We want to have dropbox running on our VPS so that files changed in Dropbox folder will show up on our VPS.
For safety reasons I chose to create a new user dbox in dbox group to run Dropbox.
For 32bit users:
For 64bit users:
After installation you will be given a link to verify your installation. You just need to copy and paste the link to a browser and do as instructed.
Then you can kill the dropbox using
Ctrl + C, we will choose what folder to synchronize using its official managment script.
Download offcial management script from the Internet.
Modify the permission:
Select folders to synchronize:
Start Dropbox Service
Here I mainly used this [Ubuntu]Dropbox命令行下安装与维护 as reference.
In the post, there is a script for Fedora/CentOS users. The 60th line should be changed from
Please pay attention to the backquote(`), it is not single quote(‘). The backquote(`) is the key locates at the left top corner of laptop keyboard, the one next to number key 1; not the one on next to return key.
Save the script as
/etc/init.d/dropbox and create file
The latter file should be empty before you add the following line:
Change file permission:
Set SElinux permission:
Add Dropboxo to autostart list:
Use the following command to check if the service is on:
Later when you need to do service check, here are some sueful commands:
You can check if Dropbox is going to start on boot now. Just reboot your system and run
service dropbox status. If your setup is right, you will see somehting like this.
Let’s move on to installing hexo now.
This part is very easy, just follow the official tutorial.
Here is the underlining logic:
- VPS runs a server. nginx for instance and set its root as
- Create a system service to monitor a Dropbox folder. _posts for instance.
- Once some file operation happens in _posts, run
hexo gautomatically. The command will generate files in
$hexo_root/blog/public( which happens to be the same position as the root of nginx server).
My choice is to use the LNMP package. It sets up Nodejs for me as user www in group www.
My default location for nginx configure file is
My conf file for nginx is (only the most important part):
Setup the Rule
For the nginx server, what really matters is the folder and files in the folder it is monitoring. Therefore, we need to automatically copy files into it.
Linux offers a kernel feature to monitor the files changes, it is called inotify. One can choose to use this feature directly like this post linux下使用inotify实时监控文件变更，做完整性检查, or we can use a command based on that feature. Incond is one of that command.
incond is a service works very much like cron. Cron periodically invokes some command( you get to choose what that command is). incond instead invokes command on file change event. For instance if you create, delete, modify files.
A note for Ubuntu, the hexo comand is by default not installed under
/usr/bin, therefore when you run incrond, you will not have
hexo command in the search path by default. You need to add something like this in your script
start incrond on boot
Now we can consider how to write synchronization rules.
For me I need to monitor the Dropbox/hexo folder so the rule will be like this:
IN_MODIFY, IN_ATRRIB, ... are for incrond. Other available flags can be found by
man 5 incrontab:
Also the content of the script is :
flock is used to acquire file lock, such that there could be only one bash script run at a time. This lock is used to avoid race condition.
From the answer from StackOverflow is a good reference to understand the
Also this link talks in general about how to create a library using flock: Elegant Bash Locking.