概念 步骤 1.准备Linux服务器 这里以Ucloud为例,其他云服务商都是一样的
我们需要登录到主机的终端
2.安装 docker 与 docker-compose
如果您想在 CentOS 系统上使用 Docker Compose,可以按照以下步骤操作:
安装 Docker 和 Docker Compose:在 CentOS 上安装 Docker 和 Docker Compose 非常简单。您可以通过运行以下命令来安装它们:
sudo yum install docker docker-compose
启动 Docker 服务:安装完成后,您需要启动 Docker 服务。您可以使用以下命令启动 Docker 服务:
sudo systemctl start docker
3.编写docker-compse文件 一共有两个服务:
nginx提供反向代理的能力
hexo提供博客服务
下面的docker-file可以自己修改
hexo-docker-file
1 2 3 4 5 6 7 8 9 FROM node:18.12 .1 LABEL maintainer=warms RUN npm config set registry https://registry.npm.taobao.org RUN npm config set disturl https://npm.taobao.org/dist RUN npm install -g hexo-cli RUN hexo init blog RUN cd blog RUN git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly WORKDIR /blog
nginx-docker-file
1 2 3 FROM nginx:1.23 LABEL maintainer=warms CMD nginx
docker-compose.yaml
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 34 35 36 37 38 39 version: "3.9" services: nginx: build: context: ./dockerfile dockerfile: nginx-docker-file hostname: nginx-warms ports: - "80:80" - "443:443" volumes: - "./nginx:/etc/nginx" networks: dockerbr0: ipv4_address: "172.127.0.100" privileged: true command: nginx -g "daemon off;" hexo: build: context: ./dockerfile dockerfile: hexo-docker-file hostname: hexo-warms ports: - "4000:4000" volumes: - "./hexo/blog:/blog" networks: dockerbr0: ipv4_address: "172.127.0.111" privileged: true command: hexo s networks: dockerbr0: driver: bridge ipam: driver: default config: - subnet: "172.127.0.0/24" gateway: "172.127.0.1"
编写了这么多文件,我们需要放在下面这个目录结构下
文件树
1 2 3 4 5 6 7 8 -compose |--docker-compose.yaml |--dockerfile | |--hexo-docker-file | |--nginx-docker-file |--hexo | |--blog |--nginx
4.购买域名&申请证书 域名可以在各个服务商购买,购买之后可以申请证书,这里以腾讯云为例
5.配置nginx配置文件 我们进入第三步文件树中的compose文件夹下的nginx文件夹中新建几个文件nginx.conf、mime.types、scgi_params、uwsgi_params、fastcgi_params,文件内容如下
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 34 35 36 37 38 39 40 41 user nginx;worker_processes auto;error_log /var/log/nginx/error .log notice ;pid /var/run/nginx.pid;events { worker_connections 1024 ; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local ] "$request " ' '$status $body_bytes_sent "$http_referer " ' '"$http_user_agent " "$http_x_forwarded_for "' ; access_log /var/log/nginx/access.log main; sendfile off ; keepalive_timeout 65 ; gzip on ; gzip_types *; gzip_comp_level 6 ; gzip_min_length 1024 ; gzip_buffers 4 16K ; gzip_http_version 1 .1 ; gzip_vary on ; gzip_disable "MSIE [1-6]\." ; gzip_proxied any; error_page 404 https://www.wzfry.com/404.html; include /etc/nginx/conf.d/*.conf ; }
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 types { text/html html htm shtml; text/css css; text/xml xml; image/gif gif; image/jpeg jpeg jpg; application/javascript js; application/atom+xml atom; application/rss+xml rss; text/mathml mml; text/plain txt; text/vnd.sun.j2me.app-descriptor jad; text/vnd.wap.wml wml; text/x-component htc; image/avif avif; image/png png; image/svg+xml svg svgz; image/tiff tif tiff; image/vnd.wap.wbmp wbmp; image/webp webp; image/x-icon ico; image/x-jng jng; image/x-ms-bmp bmp; font/woff woff; font/woff2 woff2; application/java-archive jar war ear; application/json json; application/mac-binhex40 hqx; application/msword doc; application/pdf pdf; application/postscript ps eps ai; application/rtf rtf; application/vnd.apple.mpegurl m3u8; application/vnd.google-earth.kml+xml kml; application/vnd.google-earth.kmz kmz; application/vnd.ms-excel xls; application/vnd.ms-fontobject eot; application/vnd.ms-powerpoint ppt; application/vnd.oasis.opendocument.graphics odg; application/vnd.oasis.opendocument.presentation odp; application/vnd.oasis.opendocument.spreadsheet ods; application/vnd.oasis.opendocument.text odt; application/vnd.openxmlformats-officedocument.presentationml.presentation pptx; application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx; application/vnd.openxmlformats-officedocument.wordprocessingml.document docx; application/vnd.wap.wmlc wmlc; application/wasm wasm; application/x-7z-compressed 7z; application/x-cocoa cco; application/x-java-archive-diff jardiff; application/x-java-jnlp-file jnlp; application/x-makeself run; application/x-perl pl pm; application/x-pilot prc pdb; application/x-rar-compressed rar; application/x-redhat-package-manager rpm; application/x-sea sea; application/x-shockwave-flash swf; application/x-stuffit sit; application/x-tcl tcl tk; application/x-x509-ca-cert der pem crt; application/x-xpinstall xpi; application/xhtml+xml xhtml; application/xspf+xml xspf; application/zip zip; application/octet-stream bin exe dll; application/octet-stream deb; application/octet-stream dmg; application/octet-stream iso img; application/octet-stream msi msp msm; audio/midi mid midi kar; audio/mpeg mp3; audio/ogg ogg; audio/x-m4a m4a; audio/x-realaudio ra; video/3gpp 3gpp 3gp; video/mp2t ts; video/mp4 mp4; video/mpeg mpeg mpg; video/quicktime mov; video/webm webm; video/x-flv flv; video/x-m4v m4v; video/x-mng mng; video/x-ms-asf asx asf; video/x-ms-wmv wmv; video/x-msvideo avi; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 scgi_param REQUEST_METHOD $request_method; scgi_param REQUEST_URI $request_uri; scgi_param QUERY_STRING $query_string; scgi_param CONTENT_TYPE $content_type; scgi_param DOCUMENT_URI $document_uri; scgi_param DOCUMENT_ROOT $document_root; scgi_param SCGI 1; scgi_param SERVER_PROTOCOL $server_protocol; scgi_param REQUEST_SCHEME $scheme; scgi_param HTTPS $https if_not_empty; scgi_param REMOTE_ADDR $remote_addr; scgi_param REMOTE_PORT $remote_port; scgi_param SERVER_PORT $server_port; scgi_param SERVER_NAME $server_name;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
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 fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
接下来我们需要在compose\nginx文件夹下创建conf.d文件夹,并在其中添加配置文件default.conf
1 2 3 4 5 server { listen 80; server_name wzfry.com www.wzfry.com; return 301 https://$host$request_uri; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html> <html> <head> <title>Error</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>An error occurred. hhhhhhh</h1> <p>Sorry, the page you are looking for is currently unavailable.<br/> Please try again later.</p> <p>If you are the system administrator of this resource then you should check the error log for details.</p> <p><em>Faithfully yours, nginx.</em></p> </body> </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 server { listen 443 ssl; server_name wzfry.com www.wzfry.com; ssl_certificate /etc/nginx/cert/xx/xx.com_bundle.pem; # 这里xx/xx.com_bundle.pem 替换为填写compose\nginx\cert目录下的证书pem文件路径 ssl_certificate_key /etc/nginx/cert/xxx/xxx.com.key; # 这里xxx/xxx.com.key 替换为compose\nginx\cert目录下的证书key文件路径 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; error_page 404 https://www.wzfry.com/404.html; # 保留404错误页面配置 error_page 500 502 503 504 /50x.html; # 保留500系列错误页面配置 location = /50x.html { root /usr/share/nginx/html; # 保留错误页面所在目录 } location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://hexo-warms:4000; } }
将我们下载的证书文件内容复制粘贴到compose\nginx\cert文件夹下
6.将hexo生成的博客数据放入compose\hexo\blog文件夹下
如果熟悉docker,可以直接进入docker容器安装一系列插件
7.将我们上面配置的compose目录上传到服务器 这里可以使用electerm或者其他工具
上传到服务器之后,我们通过cd 命令进入 compose文件夹,通过docker-compose 命令启动任务,命令如下:
1 docker-compose -f ./docker-compose.yaml up -d
命令执行结束后,可以通过 docker ps 查看已经启动的容器
8.服务器安全组开放443端口
9.配置域名解析记录 以腾讯云为例,进入云解析,点击添加解析记录
9.恭喜到这一步我们的hexo博客服务器就正常部署了,下面是将一下如何进入docker容器更新hexo 在我们的服务器上输入docker ps 命令就可以查看到所有运行当中的容器
1 2 3 4 5 [root@VM-4-11-centos \\x7e]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 24a9b8d765ae compose_hexo "docker-entrypoint.s…" 2 months ago Up 2 months 0.0.0.0:4000->4000/tcp compose_hexo_1 f6820104512e pengzhile/pandora-next:latest "/opt/app/entrypoint…" 11 months ago Up 9 months 0.0.0.0:8018->8018/tcp, 8181/tcp compose_pandora-next_1 2b9bd3320fe6 2f0da49af4e2 "/docker-entrypoint.…" 11 months ago Up 11 months 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp compose_nginx_1
我们找到names为compose_hexo_1的容器,其id为24a9b8d765ae,我们输入以下命令即可进入容器,24a为可以唯一区分出容器的id前缀
1 docker exec -it 24a /bin/bash
进入该容器中我们就可以执行一系列的hexo命令了