8

使用 hugo + nginx 服务器搭建个人博客

 2 years ago
source link: https://atticuslab.com/2020/06/02/3-nginx-hugo-blog/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

blog搭建小记。

基础环境搭建

最初由于使用 root 账号+简单密码 ssh 服务器,导致第二天收到tx云报警,该服务器已被暴力破解,最终无奈重装系统。

不要使用 root 用户 ssh 服务器,创建一个全新的用户,且加大密码复杂度。(关于构建一套个人密码管理系统,有望后续撰写一篇blog,此处留坑。)

sudo su //切换root用户获取创建用户的权限
2
3
useradd hello
4
passwd world
5
6
usermod -s /bin/bash hello   //为用户指定shell
7
usermod -d /home/hello hello //为用户指定用户目录
8
9
cat /etc/passwd //查看用户属性
su hello        //切换用户

go语言环境搭建

go语言官网下载对应安装包。

wget https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz

解压到/usr/local目录下,进入该目录下的go目录里,检查是否能正常执行显示go版本。

tar -C /usr/local -zxf go1.14.4.linux-amd64.tar.gz
2
3
cd /usr/local/go
4
5
bin/go version
6
go version go1.14.4linux/amd64

配置环境变量

使用vim /etc/profile命令打开profile文件,在文件末尾处添加环境变量。

export GOROOT=/usr/local/go
2
export PATH=$PATH:$GOROOT/bin
3
4
source /etc/profile

在对应用户目录下配置go语言工作环境,go nev查看配置。

mkdir go
2
export GOROOT=/usr/local/go
3
export GOPATH=$home/go
4
export PATH=$PATH:$GOROOT/bin:$GOPATH
5
export GOPROXY=https://goproxy.cn //添加国内代理
6
source .profile
7
8
go env

Nginx搭建及基本使用

安装依赖库

sudo apt-get update
2
sudo apt-get install build-essential
3
sudo apt-get install libtool
4
sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev openssl

安装nginx

wget http://nginx.org/download/nginx-1.14.2.tar.gz
2
3
tar -zxvf nginx-1.14.2.tar.gz
4
5
cd nginx-1.14.2
6
7
./configure --prefix=/usr/local/nginx
8
9
make && make install

nginx启动与停止

cd /usr/local/nginx/sbin
2
./nginx
3
ps -aux |grep nginx
4
netstat -nltp
5
./nginx -s stop
6
./nginx -s quit
7
kill nginx 主进程pid
8
killall nginx

创建软连接后在任意目录下使用nginx

echo $PATH
2
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

Hugo从入门到会用

下载 Hugo (建议带extended的安装包)对应安装包。
解压拷贝至/usr/local/bin目录下

tar -zxvf hugo_extended_0.72.0_linux-64bit.tar.gz
2
cp hugo /usr/local
3
hugo version //验证是否安装成功
hugo new site myblog

添加一个主题

cd myblog
2
git clone https://github.com/olOwOlo/hugo-theme-even themes/even

编辑主题配置文件config.toml,在主题的exampleSite里有主题相关的配置,可以拷贝出来,自定义修改。
开始写作,文件开头一般有draft: true表示,该文件当前为草稿,如需发布,需要将true改为false即可。

hugo new post/1-first.md

一般情况下执行hugo server -D开启内置服务器,即可在http://localhost:1313/开启了。由于我们是在云服务器上,使用hugo server --baseURL=http://atticuslab.com --bind= -w该命令动态查看网站效果。

设置nginx访问配置文件

hugo执行hugo命令后,博客相关的文件都保存在对应的/public/目录下,我们需要配置 nginx 可访问 public 目录即可。修改/usr/local/nginx/conf/nginx.conf配置文件,如果是root权限启动,需要添加user root;,对/xxxx/myblog/public文件添加可访问权限chmod -R 777 /*,重启nginxnginx -s reload,在浏览器里访问你的blog吧。

user root;
2
...
3
http {
4
	...
5
	server{
6
		listen 80;
7
		server_name xxxx;
8
		rewrite ^(.*) https://$server_name$1 permanent;
9
	}
	server{
	  listen 443;
		server_name xxxx;
		charset utf-8;
		ssl_certificate xxx.crt;
		ssl_certificate_key xxxx.key;
		ssl_session_timeout 5m;
20
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
21
22
		ssl_ciphers xxxxxxxxxxxxxxx;
23
		ssl_prefer_server_ciphers on;
24
25
		location / {
26
		  root /xxx/xxx;
27
			index index.html index.htm;
28
		}
29
	}
30
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK