avatar

目录
阿里云ECS搭建hexo记录(带踩坑)

前言

之前因为coding的便捷,把个人博客部署在coding page上,最近收到来自coding官方的短信,表示coding静态网站已经升级了,旧版即将在5月30日下线。新版的coding与腾讯云合并,部署page需要收费,想着反正也是要花钱,不如多花点心思和时间上手一个云服务器,选择自己部署网站,于是选择了阿里云。

搭建过程

首先,我们需要在阿里云购买服务器,这里的话,我选择的是阿里云的ECS共享性 n4,1核2G。性能无约束,对于我们这种简单的个人博客。完全足够,关键一年的费用也不算太贵,省下几顿饭的钱,选择用云服务器搭建个人网站,成功的那一刻也是很有成就感的。
话不多说,我讲一下我搭建的过程,我采用的是阿里云Centos下nginx+hexo方式搭建。
关于如何去配置云服务器,网上有相应的教程给到大家,找到最新的几篇文章,大家可以参照着去研究一下。
在使用之前,不要忘了先去阿里云控制台把你服务器的80端口打开,不懂可以百度,下面最基本的几个过程我讲一下:

一、建立博客安放的目录

举个例子,你打算把博客放到/home/www/blog

nginx
1
2
3
cd /home
mkdir www
mkdir /blog

二、安装nginx

输入yum install -y nginx
然后依次输入:

nginx
1
2
systemctl start nginx
systemctl enable nginx

之后你就可以用你的公网ip查看默认的网页了,阿里云的nginx默认页面可能是centos,这个没有关系,因为只要你能看见nginx默认页面或者阿里云centos欢迎页面基本上就表示的nginx安装成功,可以访问服务器内的网站了。

三、配置

到这一步,我发现网上有一些教程可能不是很可靠,会导致最后服务器访问不到你的hexo网页,只能访问nginx默认欢迎页,这个是一个大大的坑,说不定你就会踩到它,这个在后半部分我会介绍我的解决办法,先看大多数网友的操作:

(一)nginx默认网址重定向

①先在/etc/nginx/目录下创建一个文件夹 叫 vhost

nginx
1
2
3
4
cd /etc/nginx 
mkdir vhost
cd vhost
vim blog.conf

②编辑 blog.conf配置文件,在里面增加下面的内容:

nginx
1
2
3
4
5
6
7
server{ 
​ listen 80;
​ root /home/www/blog; #这里填博客目录存放的地址
​ server_name xxx.xxx.xxx.xxx;#这里填域名,暂时没有域名就填阿里云的公网ip
location /{
​ }
}

保存退出,按esc键,然后输入:wq
③打开/etc/nginx/目录下的nginx.conf文件
vim /etc/nginx/nginx.conf, 增加一行代码

nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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;
include /etc/nginx/vhost/*.conf; # 增加这一行代码
server {
​ listen 80 default_server;
​ listen [::]:80 default_server;
​ server_name _;
​ root /usr/share/nginx/html
​​ # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

保存,并退出

(二)安装node.js以及git

安装node.js

nginx
1
2
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs

安装完成后执行node -vnpm -v如果打印版本号则安装成功
安装git:
yum install git
配置git用户:adduser git
修改用户权限:chmod 740 /etc/sudoers
对sudoers文件作出修改:vim /etc/sudoers
然后增加一行git ALL=(ALL) ALL

nginx
1
2
3
4
5
## Allow root to run any    commands anywhere
root ALL=(ALL) ALL
git ALL=(ALL) ALL #增加这一行
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.

保存并退出
设置git用户的密码:sudo passwd git
本地建立SSH信任关系
目前每次对git仓库进行操作都需要输入密码,不太方便。但是我们已经配置了SSH,就可以通过建立SSH信任关系来免去输入密码的步骤(没有配置ssh的话先在本地电脑里输入ssh-keygen -t rsa,生成公钥文件):
切换到git用户,然后在~目录下创建.ssh文件夹

nginx
1
2
3
4
5
su git
cd ~
mkdir .ssh
cd .ssh
touch authorized_key

修改它的权限

nginx
1
2
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

回到电脑桌面,打开git bash,输入以下代码:

cmd
1
2
ssh-copy-id -i ~/.ssh/id_rsa.pub git@server_ip # 建立信任关系
ssh git@server_ip # 试一下能不能登录

如果不能登录或者还是要输入密码,就说明前面的操作有误,再检查一下吧。

现在我们要创建一个git的仓库,并且新建一个post-receive文件,操作如下:

nginx
1
2
3
cd ~
git init --bare blog.git
vim ~/blog.git/hooks/post-receive

对post-receive文件添加如下内容:
git --work-tree=/home/www/blog --git-dir=/home/git/blog.git checkout -f
保存并退出,授予该文件可执行权限
chmod +x ~/blog.git/hooks/post-receive
至此我们就完成了所有关于服务器端的配置。

四、上传博客文件,完成部署

hexo博客搭建步骤这省略了,可以到网上参照教程去搭建。
在hexo主目录下(注意:不是主题目录),找到并打开_config.yml:
找到deploy做出如下修改:

yml
1
2
3
4
5
6
deploy:
type: git
repo:
github: https://github.com/yourname/yourname.github.io.git
aliyun: git@这里改为服务器公网IP:/home/git/blog.git
branch: master

保存并退出
在主目录下,打开git bash,输入:

cmd
1
2
hexo clean
hexo d -g

到此结束

开始踩坑

部署到这一步,按理讲应该可以访问我们的博客了,最后发现,还是只能访问nginx的欢迎页,我的解决办法是回到配置的第3步:
root /usr/share/nginx/html #改成你博客的地址

nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 server { 
​ listen 80 default_server;
​ listen [::]:80 default_server;
​ server_name _;
​ root /home/www/blog; #改成你博客的地址
​ # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

如果配置好后网页打开提示403,这是权限不够的问题,如果你的博客放在/home/www/blog那么你就要 在/home目录下 使用chmod -R 777 ./www
检查配置文件/etc/nginx/nginx.conf,将第五行的user,做修改:

nginx
1
2
3
4
5
6
7
8
9
10
11
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
#nginx
user root;#把“user nginx”改为“user root”
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

然后重启nginx:systemctl restart nginx,再次刷新网页,问题解决。
如果可以访问了,恭喜你成功了!

文章作者: J.M.
文章链接: https://www.masj.top/post/1e9357b.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Jason的小世界