Featured image of post 博客部署至我的服务器的记录

博客部署至我的服务器的记录

部署hugo项目到我的服务器的全流程记录

安装nginx

首先给我的服务器安装上nginx

sudo apt install nginx

安装成功后测试一下nginx的运行状态,如果看到像我这样即为成功

deepblue@deepblueubuntu:~$  sudo systemctl is-active nginx
active

如果发现没启动使用sudo systemctl start nginx启用服务即可

默认nginx启用的是80端口,也就是直接访问IP地址就是等效于访问80端口,安装后访问IP地址出现欢迎界面即为成功

配置nginx

vim /etc/nginx/sites-available/default

你将见到下列内容,不过我的建议是最好修改前做好备份,一个要修改的内容就是其中listen的第一个值,修改成你想要的端口,如果很多IP默认端口都是80会引起重复,像我就目前改成的4448端口, 另一个是root,也就是基于什么目录的,我先把我的发布页的所有内容移到上面去,root后面跟上我的移动的地址,其中必须包含index.html入口HTML文件

 server {
       # listen 80 default_server; # 原始内容
       listen 4448 default_server; # 修改后的内容
       listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

       # root /var/www/html; # 原始内容
       root /home/deepblue/my_projects/my-blog/public; # 修改后的内容
       ......
       
}       

IP端口修改得重启后才生效,默认80端口是IP端口,也就是192.168.0.200即是192.168.0.200:80端口,两者等效

上传自动化

如果需要进一步的方便的话,得需要配置自动化了,首先sudo su的身份进入root账户,然后开始以下方法生成密钥,其中文件路径名(/root/.ssh/deepblue_rsa这是我的)最好提前先复制好,貌似修改文件名就会影响密钥判断?


root@deepblueubuntu:/home/deepblue# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /roo	^H^H^H^H^H^C
root@deepblueubuntu:/home/deepblue# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/deepblue_rsa
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/deepblue_rsa
Your public key has been saved in /root/.ssh/deepblue_rsa.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxx root@deepblueubuntu

生成完密钥之后使用下列命令:

cat /root/.ssh/deepblue_rsa.pub >> /root/.ssh/authorized_keys 

其中要把/root/.ssh/deepblue_rsa.pub替换成你使用的密钥名,这一步也就是把你生成的公钥加入认证密钥内 然后用vim编辑/etc/ssh/sshd_config文件

root@deepblueubuntu:/home/deepblue# vim /etc/ssh/sshd_config

取消以下内容的注释

PubkeyAuthentication yes # 允许公钥认证登录

PermitRootLogin prohibit-password # 禁止root用户通过密码登录

AuthorizedKeysFile      .ssh/authorized_keys  .ssh/authorized_keys2 # 指定公钥存放的位置

然后使用esc :wq!强制保存即可

然后在linux终端使用(Ubuntu环境)sz命令传回文件即可,详解参考《Ubuntu使用指南》中的 shell文件传输 板块即可

sz /root/.ssh/deepblue_rsa

拿到密钥后在你的上传bat内加入以下内容即可,-i参数后是密钥文件地址,后面是要登录的账户,最后跟上你想执行的命令,目前是重启nginx服务,以上,完成了所有自动化

ssh -i %userprofile%/.ssh\deepblue_rsa root@192.168.0.200 systemctl restart nginx
深蓝之亘的博客网站 https://adeepblue.github.io
使用 Hugo 构建
主题 StackJimmy 设计