前提条件:

  1. 需要有服务器,本教程采用的是阿里云的ESC服务器,系统为Centos
  2. 需要提前开放80端口,方便后续访问
  3. 本人的公共ip为:116.62.78.6

安装nginx

安装nginx需要相关的依赖库

安装gcc gcc-c++

1
yum install -y gcc gcc-c++

安装PCRE库

1
2
3
4
5
6
cd /usr/local/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
tar -xvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make && make install

查看版本号,出现版本号说明安装成功

1
pcre-config --version

安装 openssl 、zlib 、 gcc 依赖

1
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

安装nginx

安装nginx一定要在local文件夹下

1
2
3
4
5
6
cd /usr/local/
wget http://nginx.org/download/nginx-1.17.9.tar.gz
tar -xvf nginx-1.17.9.tar.gz
cd nginx-1.17.9
./configure
make && make install

编写启动脚本

在/etc/init.d/路径下添加脚本文件,名称为nginx

1
2
cd /etc/init.d/
vim nginx

写入以下内容

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
#!/bin/bash
#Startup script for the nginx Web Server
#chkconfig: 2345 85 15
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done."
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done."
;;
test)
$nginx -t -c $conf
echo "Success."
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done."
;;
restart)
$nginx -s reload
echo "reload done."
;;
*)
echo "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac

然后执行

1
chmod +x nginx

常用控制指令

  • 启动 service nginx start
  • 停止 service nginx stop
  • 重启 service nginx reload

启动后,访问自己的ip,例如116.62.78.6,就可以看到nginx 默认的配置网页

安装Git和Node.js

安装Node.js

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

查看版本号,是否安装成功

1
2
node -v
npm -v

安装Git及配置仓库

安装git及新建git用户

1
2
3
4
yum install git
adduser git
chmod 740 /etc/sudoers
vim /etc/sudoers

找到Allow root to run any commands anywhere这个下面,添加git ALL=(ALL) ALL这样一条指令,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
。。。。
##
## The COMMANDS section may have other options added to it.
##
## 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.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL

。。。。

执行以下指令更改文件夹权限

1
2
chmod 400 /etc/sudoers
sudo passwd git

切换git用户并且建立密钥

1
2
3
4
5
su git
cd ~
mkdir .ssh
cd .ssh
vim authorized_keys

然后在文件这里填入本地ssh秘钥,我的位置在本地C:\Users\user\.ssh下的id_rsa.pub文件里,用记事本打开。

如果本地没有秘钥或者不会创建秘钥请,参考本人技术博客中,在码云Gitee配置ssh公钥一文如何创建本地秘钥的教程

然后修改权限

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

创建git仓库

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

输入一下内容,保存退出

1
git --work-tree=/home/www/website --git-dir=/home/git/blog.git checkout -f

接着继续修改权限

1
chmod +x ~/blog.git/hooks/post-receive

以上指令都需要在su git 之后执行 如果中途断开重新连接过,需要重新执行 su git指令 进入git账户。

创建网站仓库文件夹

新建/home/www/website文件夹,在root用户下执行先su root切换为root账户,这里要输入之前设定的密码

1
su root

接着

1
2
3
4
cd /home
mkdir www
cd www
mkdir website

修改文件夹权限,这步很重要

1
2
chmod 777 /home/www/website
chmod 777 /home/www

本地测试

在本地电脑cmd命令输入

1
ssh -v git@服务器的公网ip

例如:ssh -v git@116.62.78.6,返回如下则成功,第一次的话可能需要输入一个yes

1
2
3
4
5
6
7
8
9
10
。。。
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Remote: /home/git/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Remote: /home/git/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding

Welcome to Alibaba Cloud Elastic Compute Service !

Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Apr 16 23:27:25 2020 from 183.213.146.215

修改配置文件server

这步的作用是使得nginx指向的网站,是我们上面设定的/home/www/website中的网站

1
2
cd /usr/local/nginx/conf
vim nginx.conf

将80 端口下的root项改为 /home/www/website

主要修改以下2处,一个是location下的root,原本应该填的是html,改为 /home/www/website ,同理在error_page这里也有一个,修改好后保存退出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
。。。        
location / {
root /home/www/website;
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 /home/www/website;
}

从本地hexo部署到服务器

修改本地配置文件,hexo下的config文件,将repo: git@这里改为服务器公网IP:/home/git/blog.git

1
2
3
4
5
6
7
# Deployment
# 部署的地址,必须指向自己的仓库
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@116.62.78.6:/home/git/blog.git
branch: master

重启nginx,激活网页

1
2
service nginx stop
service nginx start

本地执行部署命令

1
hexo clean && hexo g && hexo deploy

验证网页,登录自己的ip,例如116.62.78.6,就能看到自己网页了,如果不成功需要检查一下 /home/www/website 文件夹下是否有上传的内容