Deploy Nginx and Hexo on Ubuntu Server with SSL
Looking at this article, you must have a technical basis to build your own blog website. So, in this article, basic content will be omitted, and I’ll focus on some relatively complex problems I’ve met. This article may not be entirely suitable for totally new readers.
Virtual Private Server (VPS)
VPS does not need too many introductions. The funny thing is that I used to think AlibabaCloud is quite suitable for building websites. But now I find out that the CPU frequency of its starter server, which is 2.4GHz, is too slow to have an excellent performance. On the other side, Vultr, on which I used to look down, is really impressive. It’s High-Frequency Server is equipped with CPU with 3.8GHz. So it is. I choose to build my blog on Vultr.
- BTW, you can get $100 on your Vultr account if you register and make a payment by clicking my referral link. my referral link
- Clarification: I can get $25 on my account if you do this.
Node.js
For Node.js, I recommend you to install new versions. You may want to check this page for help. https://github.com/nodesource/distributions
Hexo
Demo
Official website of Hexo
- Install Hexo
npm install hexo-cli -g
- Switch to your desired directory and init
hexo init <Name>
- Switch into new directory and install dependencies
npm install
- You can run the demo now
hexo s
,ORhexo s -p 80
to use particular port - You can use screen to keep it running
Screen -R hexo
Worth a word: Hexo, based on Node.js, is basically a web program. So its package management, running, and migration have no difference to regular Node.js applications.
Configuration
Configure your blog based on the official tutorial. https://hexo.io/docs/configuration
Theme
The default theme is way simple and crude, so let’s select a good-looking theme! https://hexo.io/themes/
My blog uses Icarus. The tutorial can be found in its demo site.
Some key points
Here are some difficulties I met.
If you cannot find SVG images, you can go to Unsplash or something to download your favorite photos. And then convert them to SVG format on some websites like SVGCreator. For the avatar image, you can compress it on TinyPNG.
ShareThis module. You can go to register on ShareThis, turn on the
Image Share Buttons
, click theGet The Code
and then paste the link withinsrc
to the configuration file.
Nginx
The command Hexo s
mentioned before uses Node.js in-build web-server to run your blog. It’s hard to enable Https and some other special features by using Node.js web-server. So we usually don’t do this way.
Instead, we usually use Nginx to dispatch or reserve proxy requests.
You can simply use apt install Nginx
on Ubuntu. The configuration files are in the /etc/nginx
. The main file is /etc/nginx/nginx.conf
and the sites related configuration file is located in sites-enabled
directory. Usually, we only need to edit the last one, that is /etc/nginx/sites-enabled/default
sites-enabled
actually contains the symbol links of files insites-available
Nginx to be the Server
Because Hexo is a static blog, we only need to set the root in /etc/nginx/sites-enabled/default
to be where your blog directory is placed. This method can sharply reduce the average loading time of your blog, which I highly recommend.
1 | server { |
You can visit your blog after restarting Nginx. service nginx restart
Nginx Reverse proxy Node.js
This method needs both Nginx and Node.js running, which takes up more than 100 MB of memory and costs much more time to load.
1 | server { |
You can visit your blog after restarting Nginx. service nginx restart
SSL
It’s 2020 now; almost all websites support Https, should your blog.
To enable Https, you need an SSL certificate. You can get a free SSL certificate, or you can buy a more stable one. So in terms of your blog, I recommend you buy a cheap one. That’s enough.
This blog uses positive SSL certificate of NameCheap, which is cheap and good.
Now, you need three more steps to activate Https finally.
- Generate a pair of keys on your server
- Verify your domain on where you buy the certificate
- Add configure on Nginx
Generating Keys
Switch to your blog directory and paste codes below. Files server.key
and server.csr
are the output files.
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr
Some points
- Common Name: fill with your domain for verification
- A challenge password: you can set a easy-remembering password
Verifying your domain
Paste the content of .csr
to the given text box and choose one of these three methods.
- Email. If you have domain email, this is the recommended method
- DNS. Add a particular record of your domain.
- Http. Upload a given file to your website and make sure they can visit it. If you don’t have domain email and activation time of new DNS records, this might be the best method.
Some points
If you choose method one, directly using Nginx as the server, you need to upload the file to the given path and then change the content of /etc/nginx/sites-enabled/default
to:
1 | server { |
If you choose method two, you need to upload the file to the given path and then change the content of /etc/nginx/sites-enabled/default
to::
1 | server { |
加载SSL
Now you can download your certificate and then upload .crt
file to your blog directory。After that we can add configuration of Nginx.
If you choose method one, you need to change the content of /etc/nginx/sites-enabled/default
to:
1 | server { |
If you choose method two, you need to change the content of /etc/nginx/sites-enabled/default
to:
1 | server { |
Redirect all requests to Https is highly recommended
You can find the green lock when visiting your blog after restart Nginx: service nginx restart
Other
Main content is over. Let’s talk about some other things.
Migration
I migrated my demo blog from AlibabaCloud to Vultr and found it really simple, just like normal Node.js programs.
- Install Node.js and Hexo
- Pack up and transfer you blog directory to new VPS
- Install Nginx and edit configuration
And success! Thanks to Node.js!