3月 26, 2021 Nginx
> Source: https://serverfault.com/questions/980571/can-i-delete-access-log-files-in-nginx-will-it-cause-an-issue

You can remove access.log as root user, or using sudo.

```sh
sudo rm /var/log/nginx/access.log
```

However after deleting it, the file will not be re-created until nginx is re-loaded or re-started. To reload nginx do:

```sh
sudo service nginx reload
```

To re-size the file to 0 bytes, without having to re-load or re-start nginx, use the truncate command:

```sh
sudo truncate --size 0 /var/log/nginx/access.log
```
3月 02, 2021 NodeJS
> Source: https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color

Below you can find colors reference of text to command when running node.js application:

```js
console.log('\x1b[36m%s\x1b[0m', 'I am cyan');  //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow);  //yellow
```

Note `%s` is where in the string (the second argument) gets injected. `\x1b[0m` resets the terminal color so it doesn't continue to be the chosen color anymore after this point.

### Colors reference

```js
Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
```
2月 22, 2021 19 Things
好耐都無入來寫文章了,最近生活都有好多事情要忙,有時打開了 19Site 都沒有進來打文章。
12月 22, 2020 Nginx SSL
> Source: https://www.vultr.com/docs/setup-letsencrypt-on-linux

### Introduction

Let’s Encrypt is an automated, open certificate authority that offers free TLS/SSL certificates for the public’s benefit. The service is provided by the Internet Security Research Group (ISRG). This tutorial shows how to install a Let's Encrypt SSL certificate on One-Click LAMP & LEMP apps using the certbot installation wizard. After completing this tutorial, the server will have a valid certificate and redirect all HTTP requests to HTTPS.

### Prerequisites

This tutorial assumes that you have deployed a Vultr One-Click LAMP (Apache) or One-Click LEMP (Nginx) VPS, have a domain name pointing to your server IP address, and you are logged in as root.

### 1. Install certbot

Install certbot with `apt`.

- One-Click LAMP (Apache)

```sh
# apt update && apt install certbot python-certbot-apache -y
```

- One-Click LEMP (Nginx)

```sh
# apt update && apt install certbot python-certbot-nginx -y
```

### 2. Install Certificate

Run `certbot` to install the certificate. Full examples are below, here are descriptions of the command line options:

- --apache
- - Use the Apache web server
- --nginx
- - Use the nginx web server
- --redirect
- - Redirect all HTTP requests to HTTPS.
- -d example.com -d www.example.com
- - Install a multiple domain (SAN) certificate. You may use up to 100 -d domain entries.
- -m admin@example.com
- - The notification email address for this certificate.
- --agree-tos
- - Agree to the terms of service.

Use `certbot --help` for more information.

### Example: One-Click LAMP (Apache)

Run certbot for Apache.

```sh
# certbot --apache --redirect -d example.com -d www.example.com -m admin@example.com --agree-tos
```

### Example: One-Click LEMP (Nginx)

1. Before running certbot, make sure server_name is set properly. Edit your Nginx configuration:

```sh
# nano /etc/nginx/conf.d/default.conf
```

2. Update server_name to include your domain name.

```sh
server {
    server_name  example.com www.example.com;
```

3. Save and exit the file.

4. Run certbot for Nginx.

```sh
# certbot --nginx --redirect -d example.com -d www.example.com -m admin@example.com --agree-tos
```

### 3. Verify Automatic Renewal

Let's Encrypt certificates are valid for 90 days. The certbot wizard updates the systemd timers and crontab to automatically renew your certificate.

1. Verify the timer is active.

```sh
# systemctl list-timers | grep 'certbot\|ACTIVATES'
```

2. Verify the crontab entry exists.

```sh
# ls -l /etc/cron.d/certbot
```

3. Verify the renewal process works with a dry run.

```sh
# certbot renew --dry-run
```

### Summary

Installing a free Let's Encrypt certificate is simple with certbot. For more information, see the official certbot installation documentation.
11月 12, 2020 Ubuntu
> Source: https://www.vultr.com/docs/update-ubuntu-server-best-practices

### Introduction

It is a best practice to update your server on a regular schedule for security and stability. Use this guide to keep your Ubuntu server updated.

Supported Versions
This guide applies to:

- Ubuntu 20.04 LTS
- Ubuntu 19.10
- Ubuntu 18.04 LTS
- Ubuntu 16.04 LTS

### Make a Backup

Always make a backup before updating your system.

### 1. Update the Package Lists

This command updates the package lists from the enabled repositories.

```sh
$ sudo apt update
```

### 2. List the Upgradable Packages

This step is optional. To view the upgradable packages before performing the upgrade, use the apt list command.

```sh
$ sudo apt list --upgradable
```

### 3. Upgrade Packages

This command will upgrade all the upgradeable packages.

```sh
$ sudo apt upgrade
```

### 4. Restart the Server

```sh
$ sudo reboot
```

### One Line Upgrade

If you want to accept all the defaults and perform the upgrade without intervention, use this command:

```sh
$ sudo apt update && sudo apt upgrade -y
```

### Optional - Autoremove

Use apt to remove old packages and dependencies automatically.

```sh
$ sudo apt autoremove
```
11月 11, 2020 Ubuntu
> Source: https://www.vultr.com/docs/create-a-sudo-user-on-ubuntu-best-practices

### Introduction

Performing server administration as a non-root user is a best practice for security. After launching your Vultr VPS, your first task as root should be to set up a non-root user with sudo access.

This guide applies to the following versions:

- Ubuntu 20.04 LTS
- Ubuntu 19.10
- Ubuntu 18.04 LTS
- Ubuntu 16.04 LTS

### 1. Add a New User Account

Create a new user account with the `adduser` command. Use a strong password for the new user. You can enter values for the user information, or press `ENTER` to leave those fields blank.

```sh
# adduser example_user
```

### 2. Add the User to the Sudo Group

Add the new user to the sudo group with usermod.

```sh
# usermod -aG sudo example_user
```

### 3. Test

Switch to the new user.

```sh
# su - example_user
```

Verify you are the new user with whoami, then test sudo access with sudo whoami, which should return root.

```sh
$ whoami
example_user
$ sudo whoami
[sudo] password for example_user:
root
```
10月 21, 2020 React
### Bundle

在使用 React 時是一定會使用到 Webpack 等等工具來把 JSX、CSS、IMAGES... 等等進行打包。如果你的 APP 已經建立得非常大時,產生的 Bundle 檔案也會非常巨大。使用者在下載時就會花上很多時間了喔 !

### 解決方法

我們可以通過使用 React 內建的 Lazy loading 來解決一次過下載巨大 Bundle 檔案的問題。它的原理是把單一的 Bundle 檔案依不同的 package 來分割開,然後在使用者實際使用時才動態載入。

### 內裏是 Promise

我們有以下的 module ,作用把兩個傳入的數字相加。

```js
// math.js
export default sum(a, b) => a + b;
```

在傳統的使用方法,我們是以下這樣 :

```js
import sum from './math.js';

console.log(sum(1, 2)); // 3
```

使用 Lazy Loading 的話,載入的 object 就會變成了 Promise 物件:

```js
import('./math.js')
	.then(sum => console.log(sum(1, 2))); // 3
```

### 實際使用

在實際使用上,我們可以通過使用 `React.lazy` 方法來簡單化整個載入的流程。

```js
// 載入 SomeComponent
const SomeComponent = React.lazy(() => import('./SomeComponent.jsx'));
```

然可以作為一般的 Component 來使用,但是需要放在 React.Suspense 元件內。

```js
<React.Suspense fallback={<div>Loading...</div>}>
	<SomeComponent />
</React.Suspense>
```

React.Suspense 是處理其子 Component 如果有 Lazy Loading 動作時,就會劃出 fallback 的元件出來,等待 Lazy Load 完成後,就會顯示元件。

### 筆者推介使用方法

因為在 React 內得多元件都可以重用,但是如果有使用 React Router 的話,不同 URL 之間的元件是一定不會重用的,所以我們可以簡單把 React.Suspense 包住整個 Router。然後把不同 Route 內的 Component 都轉成使用 Lazy Load,這樣就已經可以簡單快速地分割出合適的 Bundle 檔案了。

### 官方文件

官方文件 : https://zh-hant.reactjs.org/docs/code-splitting.html
過去文章
2025 (9)
4 (5)
3 (1)
2 (3)
2024 (25)
11 (3)
10 (3)
9 (1)
3 (18)
2022 (6)
10 (1)
6 (2)
5 (1)
3 (1)
1 (1)
2021 (21)
11 (7)
7 (1)
6 (2)
5 (2)
4 (6)
3 (2)
2 (1)
2020 (92)
12 (1)
11 (2)
10 (4)
9 (10)
8 (5)
7 (1)
6 (3)
5 (1)
4 (4)
3 (25)
2 (7)
1 (29)
2019 (57)
12 (25)
11 (7)
9 (25)