介紹
原先我的博客是掛載在github pages上面的,但是因?yàn)間ithub pages的服務(wù)器在國外,國內(nèi)訪問速度就會(huì)有點(diǎn)感人,于是就想把我的博客掛載在自己阿里云的服務(wù)器上面。
環(huán)境
- 一臺(tái)有獨(dú)立ip的服務(wù)器(centos 7)
- 在本地搭建好了hexo框架
實(shí)現(xiàn)
因?yàn)槲业牟┛褪莌exo寫的,眾所周知這是一個(gè)快速生成靜態(tài)博客的框架,既然是靜態(tài)網(wǎng)頁,web服務(wù)器方面當(dāng)然是選擇nginx了,為什么選擇nginx不選擇apache呢,因?yàn)閚ginx輕量級,同樣起web服務(wù),比apache 占用更少的內(nèi)存及資源、抗并發(fā),nginx 處理請求是異步非阻塞的,而apache 則是阻塞型的,在高并發(fā)下nginx 能保持低資源低消耗高性能、高度模塊化的設(shè)計(jì),編寫模塊相對簡單、社區(qū)活躍。等等的優(yōu)點(diǎn)。
既然選擇了nginx,那選擇如何搭建了,普通安裝或者編譯nginx,多多少少會(huì)有各種殘留,這對一個(gè)系統(tǒng)潔癖的人來說,卻是非常痛苦的,所以我選擇了使用docker安裝nginx。
獲取nginx鏡像
-
拉取鏡像:
docker search nginx
pull第一個(gè)nginx官方的鏡像
docker pull nginx
查看獲取到的鏡像
? docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 06144b287844 2 weeks ago 109MB
可以看到鏡像已經(jīng)pull下來了
安裝docker
? curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
然后進(jìn)入阿里云docker庫首頁https://dev.aliyun.com/
登入
然后進(jìn)入管理中心
點(diǎn)擊加速器,復(fù)制鏈接
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["粘貼那個(gè)鏈接"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
安裝docker-compose
下載shell腳本
? sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
修改權(quán)限
? sudo chmod +x /usr/local/bin/docker-compose
測試
? docker-compose --version
docker-compose version 1.22.0, build 1719ceb
通過docker-compose管理生成容器
? cd /home/
? mkdir blog && cd blog //創(chuàng)建一個(gè)blog的目錄
? vi docker-compose.yml //創(chuàng)建一個(gè)名叫docker-compose.yml的文件
#寫入
version: '3'
services:
nginx:
restart: 'always'
image: 'nginx'
ports:
- '80:80'
- '443:443'
volumes:
- '/home/blog/www/public:/usr/share/nginx/html' #這里的/home/blog/www/public是主機(jī)的目錄,意思是把這個(gè)目錄掛載到容器的/usr/share/nginx/html目錄中,該目錄防止了網(wǎng)頁的靜態(tài)文件
- '/home/blog/log:/var/log/nginx/' #同理,log文件夾里是nginx的日志文件
- '/home/blog/conf:/etc/nginx/conf.d' #同理,配置文件
#:wq保存退出
創(chuàng)建掛載目錄
在/home/blog/下創(chuàng)建/www、/log、/conf三個(gè)目錄
? mkdir {www,log,conf}
? cd conf
? vi default.conf
添加以下內(nèi)容
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
## 下面三行是添加的。
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
上傳靜態(tài)文件
在本地hexo的博客目錄下
運(yùn)行hexo g生成靜態(tài)文件
他會(huì)在該目錄下生成一個(gè)public的一個(gè)文件,里面是關(guān)于你博客的所有靜態(tài)文件,然后這個(gè)文件scp到服務(wù)器的/home/blog/www/下
? scp /home/kevin/blog/public root@119.23.241.xxxx:/home/blog/www
# /home/kevin/blog/public是你本地的public目錄地址
# 119.23.24.xxxx表示你遠(yuǎn)程服務(wù)器的地址
# /home/blog/www/public表示上傳到遠(yuǎn)程服務(wù)器的位置
然后進(jìn)入服務(wù)器端
? cd /home/blog
? docker-compose up -d
# -d表示后臺(tái)運(yùn)行的意思
然后在瀏覽器輸入你服務(wù)器的ip
發(fā)現(xiàn)博客已經(jīng)掛在成功了,
因?yàn)槲业姆?wù)器是阿里云的,所以還有加一步,去阿里的控制臺(tái)配置防火墻入口規(guī)則,添加80端口和443端口。