1. 程式人生 > 其它 >CentOS7.8(阿里雲伺服器)使用Docker搭建Halo部落格

CentOS7.8(阿里雲伺服器)使用Docker搭建Halo部落格

一、安裝Docker

1.檢視核心版本

uname -a

Docker官方說至少3.8以上,建議3.10以上

2.把yum包更新到最新

溫馨提示:新環境或測試環境可隨意操作,生產環境酌情慎重更新

yum update

期間需要確認,輸入"y"即可

3.安裝需要到依賴包

yum install -y yum-utils device-mapper-persistent-data lvm2

4.設定yum源

下面兩個選擇一個即可

# 中央倉庫
yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo
# 阿里倉庫
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5.檢視倉庫中所有到Docker版本

yum list docker-ce --showduplicates | sort -r

6.安裝Docker

我選擇到是docker-ce-18.03.1.ce

yum install docker-ce-18.03.1.ce

期間需要確認,輸入"y"即可

7.啟動Docker並加入開機啟動

# 啟動Docker
systemctl start docker
# 加入開機自啟動
systemctl enable  docker

8.檢視Docker版本

docker version

一些常用Docker命令

二、使用Docker安裝Halo部落格

1.建立工作目錄

mkdir ~/.halo && cd ~/.halo

2.下載示例配置檔案到工作目錄

wget https://dl.halo.run/config/application-template.yaml -O ./application.yaml

如果沒有安裝wget,可使用命令 yum -y install wget 進行安裝

3.編輯配置檔案,配置資料庫或者埠等,如需配置請參考參考配置(一般預設即可)

vim application.yaml

4.拉取最新的 Halo 映象

docker pull halohub/halo

5.建立容器

docker run -it -d --name halo -p 8090:8090 -v ~/.halo:/root/.halo --restart=always halohub/halo

-it: 開啟輸入功能並連線偽終端
-d: 後臺執行容器
–name: 為容器指定一個名稱
-p: 埠對映,格式為 主機(宿主)埠:容器埠 ,可在 application.yaml 配置。
-v: 工作目錄對映。形式為:-v 宿主機路徑:/root/.halo,後者不能修改。
–restart: 建議設定為 always,在 Docker 啟動的時候自動啟動 Halo 容器。

6.開啟 http://ip:埠號

訪問後即可進入指導頁面(需開放伺服器埠,一般預設是8090)
參考文件

三、安裝nginx

1.下載安裝包(一般安裝在/usr/local下)

cd /usr/local
wget http://nginx.org/download/nginx-1.11.5.tar.gz

2.下載nginx依賴庫

可能會安裝失敗,具體可參考後面到錯誤提示資訊

yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib 
yum install zlib-devel
yum install openssl
yum install openssl-devel

3.解壓安裝包

nginx被解壓到了/usr/local/nginx-1.11.5 目錄下(不要把壓縮包解壓到/usr/local/nginx目錄下,或者將解壓後的目錄重新命名為nginx,因為nginx會預設安裝到/usr/local/nginx目錄下)

tar -zxvf nginx-1.11.5.tar.gz

4.安裝

# 切換至nginx-1.11.5/目錄
cd /usr/local/nginx-1.11.5/

執行

./configure --with-http_ssl_module
# --with-http_ssl_module需加上,否則後面配置SSL證書時會報錯  the "ssl" parameter requires ngx_http_ssl_module

該命令會檢測當前系統環境,以確保能成功安裝nginx,執行該操作後可能會出現以下幾種提示:

checking for OS
+ Linux 3.10.0-123.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found

如果出現以上錯誤提示資訊,執行yum install gcc-c++安裝gcc

 ./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

如果出現上面提示,表示缺少PCRE庫

 ./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

如果出現以上提示,表示缺少zlib庫

如果沒有出現./configure: error提示,表示當前環境可以安裝nginx,執行make和make install編譯nginx

make && make install

如果沒有報錯的話,表示nginx已經成功安裝完成,預設安裝位置為/usr/local/nginx

5.配置nginx開機啟動

cd /lib/systemd/system/
vim nginx.service

檔案內容如下

[Unit]
Description=nginx 
After=network.target 
   
[Service] 
Type=forking 
ExecStart= /usr/local/nginx/sbin/nginx
ExecReload= /usr/local/nginx/sbin/nginx  reload
ExecStop= /usr/local/nginx/sbin/nginx  quit
PrivateTmp= true 
   
[Install] 
WantedBy=multi-user.target

退出並儲存檔案,執行命令開機啟動

systemctl enable nginx.service

6.驗證nginx是否安裝成功

輸入http://伺服器IP/ 如果能看到nginx的介面,就表示安裝成功了

7.常用nginx命令

systemctl start nginx.service   # 啟動nginx
systemctl stop nginx.service   # 結束nginx
systemctl restart nginx.service  # 重啟nginx
nginx -v  # 檢視nginx版本

四、nginx安裝SSL證書並進行配置

在此之前需先購買域名並進行備案

1.進入阿里雲伺服器中購買證書購買SSL證書地址

選擇***DV單域名(免費試用)進行購買,購買後選擇nginx證書下載,下載後解壓會得到下面兩個檔案

2.配置nginx

執行下面命令在在Nginx安裝目錄(預設為/usr/local/nginx/conf)下建立一個用於存放證書的目錄(命名為cert)

cd /usr/local/nginx/conf  #進入Nginx預設安裝目錄。如果您修改過預設安裝目錄,請根據實際配置進行調整。
mkdir cert  #建立證書目錄,命名為cert。

使用putty、xshell等工具等檔案傳輸工具將你的證書傳到cert目錄下(/usr/local/nginx/conf/cert)
編輯neinx.conf

vim /usr/local/nginx/conf/nginx.conf

配置檔案中定位到HTTP協議程式碼片段(http{}),並在HTTP協議程式碼裡面新增以下server配置(如果server配置已存在,按照以下注釋內容修改相應配置即可)。

server {
        listen       443 ssl;
        server_name yourdomain.com www.yourdomain.com;
        #需要將yourdomain.com替換成證書繫結的域名。
        client_max_body_size 1024m;
        ssl_certificate      cert/cert-file-name.pem;
        # 需要將cert-file-name.pem替換成已上傳的證書檔案的名稱
        ssl_certificate_key  cert/cert-file-name.key;
        # 需要將cert-file-name.key替換成已上傳的證書金鑰檔案的名稱
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  HIGH:!aNULL:!MD5;

        location / {
            # 反向代理地址,注意 /
             proxy_pass http://127.0.0.1:8090/;

             proxy_redirect     off;
            # 代理請求頭
             proxy_set_header HOST $host;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

設定HTTP請求自動跳轉HTTPS(可選),配置如下

server {
        listen       80;
        server_name   yourdomain.com www.yourdomain.com;
        return 301 https://$server_name$request_uri;
       }

3.重啟Nginx服務

cd /usr/local/nginx/sbin  #進入Nginx服務的可執行目錄
./nginx -s reload  #重新載入配置檔案

4.驗證SSL證書是否安裝成功

證書安裝完成後,可通過訪問證書的繫結域名驗證該證書是否安裝成功

https://yourdomain.com   #需要將yourdomain.com替換成證書繫結的域名

注意事項,需將埠80和443新增至伺服器安全組,且需檢查系統防火牆firewall是否添加了443埠,是否在監聽,命令可參考文件

本部落格主要參考文件如下:
阿里雲文件
Halo文件

本文來自部落格園,作者:胡圖人,轉載請註明原文連結:https://www.cnblogs.com/huturen/p/15466172.html