2024-08-20
Use ssh to connect to AWS EC2 instance

1: Create a new key pair

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation pane, choose Key Pairs.
  3. Choose Create key pair.
  4. For Key pair name, enter a name for the new key pair, and then choose Create key pair.
  5. Your browser will download the private key file automatically. The private key file is automatically downloaded by your browser. The base file name is the name you specified as the name of your key pair, and the file name extension is .pem. Save the private key file in a safe place.
  6. Choose Close.
  7. Important: You can create a key pair only once. Be sure to save the private key file to your computer. You’ll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance.

2: Connect to your instance

  1. Open a terminal window.
  2. Use the cd command to navigate to the directory where your private key file is located.
  3. Use the following command to set the permissions of your private key file so that only you can read it:
1
chmod 400 /path/my-key-pair.pem
  1. Use the following command to connect to your instance. Replace ec2-user with the appropriate user name for your AMI.
  • For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.
  • For a CentOS AMI, the user name is centos.
  • For a Debian AMI, the user name is admin.
  • For a Fedora AMI, the user name is ec2-user or fedora.
  • For a RHEL AMI, the user name is ec2-user or root.
  • For a SUSE AMI, the user name is ec2-user or root.
  • For an Ubuntu AMI, the user name is ubuntu. Here my OS is Ubuntu.
  • Otherwise, if ec2-user and root don’t work, check with your AMI provider.
1
ssh -i /path/my-key-pair.pem user_name@ip_address

3: (Optional) Connect to your instance using a password

  1. Open a terminal window.
  2. Use the following command to connect to your instance.
    1
    ssh user_name@my-instance-ip

I meet Permission denied (publickey) error.

To fix it.

Step1: Firstly I need to use the previous command to connect to my instance.
1
ssh -i /path/my-key-pair.pem user_name@ip_address
Step2: Set up a password for the user using passwd command along with the username.
1
sudo passwd ubuntu
Step 3: Edit sshd_config file.
1
sudo vim /etc/ssh/sshd_config

Find the Line containing PasswordAuthentication parameter and change its value from no to yes.

1
PasswordAuthentication yes

If you want to set up root login, find PermitRootLogin parameter and change its value from prohibit-password to yes

1
PermitRootLogin yes

After this changes save file and exit.

Step 4: Restart the SSH service.
1
2
3
service ssh restart ## for ubuntu

service sshd restart ## for centos
Step 5: Add the .pem file to Local ssh.
1
2
chmod 400 /path/my-key-pair.pem
ssh-add -k /path/my-key-pair.pem
Step 6: Now you can connect to your instance using a password.
1
ssh user_name@my-instance-ip
Read More

2024-08-18
Useful Commands

Git

When you are using Git, you may encounter some problems. Here are some useful commands to help you solve them.

  1. Set the postBuffer size to 500MB
    When meeting:
1
2
3
4
5
6
error: RPC failed; HTTP 400 curl 22 The requested URL returned error: 400
send-pack: unexpected disconnect while reading sideband packet
Writing objects: 100% (635/635), 2.07 MiB | 2.53 MiB/s, done.
Total 635 (delta 81), reused 0 (delta 0), pack-reused 0
fatal: the remote end hung up unexpectedly
Everything up-to-date

You can set the postBuffer size to 500MB:

1
git config http.postBuffer 524288000
Read More

2024-08-16
Add Dark Mode to Your Website

Add Dark Mode to Your Website

Add dark mode to your website using DarkReader. This is a simple way to add dark mode to your website.

  • GitHub: https://github.com/darkreader/darkreader
  • Add code below to _includes/head.html in your Jekyll project.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {% if jekyll.environment == 'production' -%}
    <!-- dark theme using DarkReader -->
    <script src="//unpkg.com/darkreader@4.9.40/darkreader.js"></script>
    <script type="text/javascript">
    DarkReader.setFetchMethod(window.fetch); // Fix to remedy CORS errors in chrome console
    //DarkReader.enable();
    DarkReader.auto( {brightness: 100, contrast: 90, sepia: 10} );
    </script>
    {%- endif %}


Reference:
reference the implementation from link

Read More

2024-08-05
Add comments in blog posts on GitHub Pages websites

  • This, https://alanzeng.com/, is my Github Pages static website and blog, powered by the Jekyll static site generator.
  • I’ve added GitHub-Issue-based comments to the bottom of this website using the plugin called utterances,
    which is totally awesome and makes it trivial to add a beautiful and user-friendly commenting system to your static website or blog.

utteranc config

Visit Utterances main page & installation info.: https://utteranc.es/
image-20240806185908366


repo:设置存放评论的仓库

Utterances 使用 Github Issues 存储评论,所以需要一个仓库。你可以新建一个公开仓库专门用来放评论,也可以使用原有的仓库。要设置存放评论的仓库只需要将 repo=”username/reponame” 这一行中的 username 改为你的 GitHub 用户名,reponame 改为你的仓库名,其它不变。

仓库需满足以下条件:

  • 仓库必须为公开仓库,私有仓库访客无法查看对应 Issues 上的评论。
  • 确保在仓库中安装了 Utterances 的 GitHub App,或是你自己注册的 GitHub App(自托管),否则用户将无法发表评论。
  • 如果你的仓库是派生 (fork) 出的,请在仓库的 Settings 选项确认 Features 区 Issues 已勾选。

issue-term:博客文章和 Issue 映射

Utterances 使用以下几种规则建立博客文章和 GitHub Issues 的映射:

  • Issue 标题包含页面路径名(issue-term=”pathname”)
  • Issue 标题包含页面 URL(issue-term=”url”)
  • Issue 标题包含页面标题(issue-term=”title”)
  • Issue 标题包含页面 og:title(issue-term=”og:title”)
  • 特定的 issue 编号(issue-number=”具体数字”)
  • Issue 标题包含特定项(issue-term=”你设置的特定内容”)

具体细节参考上图


label:Issue 标签

如果你使用原有的仓库,但是担心 Issues 页面评论和问题混杂在一起,Utterances 支持设置标签(Label)来区分它们。设置 label=”你的标签内容”,Utterances 将在创建 issue 时使用你设置的标签。

  • 标签名区分大小写。
  • 标签必须存在于你的仓库中(须提前在 GitHub Issues 页面创建好,不能使用不存在的标签)。
  • 标签名支持 Emoji。例如:label=”💬”

theme:主题

Utterances 有多种主题,其中包括多款夜间模式主题。

  • GitHub Light:theme=”github-light”
  • GitHub Dark:theme=”github-dark”
  • GitHub Dark Orange:theme=”github-dark-orange”
  • Icy Dark:theme=”icy-dark”
  • Dark Blue:theme=”dark-blue”
  • Photon Dark:theme=”photon-dark”

配置

1
2
3
4
5
6
7
<script src="https://utteranc.es/client.js"
repo="[ENTER REPO HERE]"
issue-term="pathname"
theme="github-light"
crossorigin="anonymous"
async>
</script>

将这部分代码粘贴进_layout/post.html中, 大功告成!



> referenced: > - [link](https://gabrielstaples.com/github-pages-comments/#gsc.tab=0) > - [link](https://blog.njilc.com/post/self-hosted-utterances-tutorial)
Read More

2024-08-02
Common Docker Operations and Commands

Common Docker Operations and Commands

Create Docker Image for Python Projects

  1. Configure Dependencies
1
2
3
pip freeze > requirements.txt
touch Dockerfile
vim Dockerfile
  1. Create and Edit Dockerfile
1
2
3
4
5
6
FROM python:3.10.12
ADD . /code
WORKDIR /code
# Install Dependencies
RUN pip install -r requirements.txt
CMD ["python", "/hello.py"]
  1. Build Docker Image & run
1
2
3
docker build -t demo .
docker run -it demo
docker run -itd --name demo -p 5000:5000 demo

Pull or Push Docker Images

Docker Hub

login to Docker Hub;

1
2
3
4
5
6
7
8
9
[root@localhost ~]
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username:
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Edit Tag

  • Push Command docker push docker_username/REPOSITORY:TAG
  • We need to edit the tag and repository name:
1
2
3
4
5
6
7
8
9
[root@localhost ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
demo latest 1e5f6711d527 3 days ago 178MB
[root@localhost ~]# docker tag demo:latest ***/demo:v1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo latest 1e5f6711d527 3 days ago 178MB
***/demo v1 1e5f6711d527 3 days ago 178MB
[root@localhost ~]#

Push

1
2
3
4
5
6
7
8
[root@localhost ~]# docker push ***/demo:v1
The push refers to repository [docker.io/***/demo:v1]
833a0t6a6ff9: Pushed
10bfe4y2500e: Pushed
d43sfdd7d594: Mounted from library/nginx
c2adabsadfed: Mounted from library/nginx
v1: digest: sha256:67dcdae5578c0374019bdc899731543cfd7c48fe5780e84233a258f2bf7d2ceda size: 1155
[root@localhost ~]#

Pull

1
2
3
4
5
[root@localhost ~]# docker pull ***/demo:v1
v1: Pulling from ***/demo:v1
Digest: sha256:67dcdae5578c0374019bdc899731543cfd7c48fe5780e84233a258f2bf7d2ceda
Status: Downloaded newer image for ***/demo:v1
docker.io/llxxyy/nginx-io:v1
Read More

2024-07-23
RAG

Read More

2024-07-23
Jekyll 网站添加访问量统计分析

Google Analysis

谷歌分析是谷歌提供的免费网络分析服务,用于跟踪和报告网站流量。将谷歌分析添加到 Jekyll 网站十分简单。登录  谷歌分析  并新建一个媒体资源,以获取网站的跟踪 ID。可在管理 > 媒体资源 > 跟踪信息 > 跟踪代码下找到跟踪 ID。

在 Jekyll 网站上部署谷歌分析,首先在_includes文件夹新建名为google-analytics.html的文件,并写入以下代码:

1
2
3
4
5
6
7
8
<script async src="https://www.googletagmanager.com/gtag/js?id=***"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '***');
</script>

然后在_config.yml中添加跟踪 ID:

1
2
# Google Analytics
google_analytics: ***

最后添加google-analytics.html到网页,谷歌建议把跟踪代码放在每个页面的<head>中,以确保正确跟踪所有访问。

{%- if jekyll.environment == 'production' -%}
      {%- include google-analytics.html -%}
    {%- endif -%}
    

Reference:link

Read More