使用docker搭建nginx掛載hexo博客

介紹

原先我的博客是掛載在github pages上面的,但是因?yàn)間ithub pages的服務(wù)器在國外,國內(nèi)訪問速度就會(huì)有點(diǎn)感人,于是就想把我的博客掛載在自己阿里云的服務(wù)器上面。

環(huán)境

  1. 一臺(tái)有獨(dú)立ip的服務(wù)器(centos 7)
  2. 在本地搭建好了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鏡像

  1. 拉取鏡像:

    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端口。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • Docker — 云時(shí)代的程序分發(fā)方式 要說最近一年云計(jì)算業(yè)界有什么大事件?Google Compute Engi...
    ahohoho閱讀 15,850評論 15 147
  • Pam 又一個(gè)不走尋常路 將食物與藝術(shù)相結(jié)合的厲害人物 拿餅干當(dāng)起了畫布 不只是畫 還有雕刻 嚇?biāo)纻€(gè)人 — END...
    藝伙閱讀 243評論 0 1
  • 久等了,我來了。 我的第一本書《只有時(shí)間知道》今日上線預(yù)售了。由于我的懶惰慢吞吞地拖了兩年之久,也正是因?yàn)檫@樣,有...
    飛蔦集民宿閱讀 957評論 0 1

友情鏈接更多精彩內(nèi)容