Skip to content

Commit

Permalink
add v2 docs
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Oct 10, 2023
1 parent 16976e7 commit ed34315
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pages/deploy/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sudo mv composer.phar /usr/local/bin/composer

### 安装 MySQL

这里安装的是 MySQL 8, 使用 5.7 也是可以的,而且 5.7 安装会比较省事一点。
这里安装的是 MySQL 8,使用 5.7 也是可以的,而且 5.7 安装会比较省事一点。

```shell
sudo apt-get install mysql-server
Expand Down
3 changes: 2 additions & 1 deletion pages/deploy/meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"default": "分开部署方案",
"docker": "Docker 部署方案"
"docker": "Docker 部署方案",
"v2": "V2 分开部署方案"
}
290 changes: 290 additions & 0 deletions pages/deploy/v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
# V2 分开部署方案

Ubuntu

## 安装 PHP

```shell
sudo apt update
sudo apt install php8.1 php8.1-fpm php8.1-common php8.1-cli php8.1-curl php8.1-zip php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-redis -y
```

### 安装 Composer

可以参考官网 [Download Composer](https://getcomposer.org/download/),或者这样安装:

```shell
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
```
### 安装 MySQL

这里安装的是 MySQL 8,使用 5.7 也是可以的,而且 5.7 安装会比较省事一点。

```shell
sudo apt-get install mysql-server
sudo mysql_secure_installation
```

然后会有一系列问题,这里可以参考我的回应:

```shell
* Press y|Y for Yes, any other key for No: Y
* Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 // 密码强度
* Please set the password for root here.
New password:
Re-enter new password:
* Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
* Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N
* Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
* Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
```

为 root 设置密码(下面 password 为新密码,要求最高强度):

```shell
sudo mysql
```

```sql
SELECT user,authentication_string,plugin,host FROM mysql.user;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
```

```
FLUSH PRIVILEGES;
```

添加新用户(下面 newuser 是新用户,password 为新密码,要求最高强度):

```sql
ALTER USER 'newuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password’;
```
### 删除 Apache
如果你需要的话
```shell
sudo service apache2 stop
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
sudo apt-get autoremove —purge
whereis apache2
sudo rm -Rf 多个文件路径
```
## API 项目
### 安装
```shell
git clone https://github.com/cashwarden/api-v2.git cashwarden-apiv2
cd cashwarden-apiv2
cp .env.example .env
```
修改 `.env` 的 MySQL 密码,以及其他环境变量,然后
```
composer install
php artisan key:generate
php artisan migrate
php artisan passport:install
```
### 配置 Nginx
```shell
sudo apt install nginx -y
sudo vim /etc/nginx/conf.d/api.cashwarden.com.conf
```
代码:
```conf
server {
charset utf-8;
client_max_body_size 128M; ## listen for ipv4
server_name youname.com;
index index.html index.htm index.php;
root /root/www/cashwarden-apiv2/web;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php?$args;
}


location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}

location ~ .*\.(js|css)?$ {
expires 1h;
}
}
```

`server_name``root` 的值需要根据情况修改。

然后重启 nginx

```shell
sudo service nginx reload
```

### 配置定时任务

配置定时记账,部署注意路径,参考代码:

```shell
* * * * * /usr/bin/php /root/cashwarden-apiv2/artisan schedule:run >> /tmp/prod-schedule.log 2>&1
```


## WEB 项目

### 安装

**方式一:使用已编译好的代码(推荐)**

WEB 项目部署可以直接拿 build 好的代码,自己 build 非常耗时,而且需要至少 2 G 的内存:

```shell
git clone --depth=1 https://github.com/cashwarden/web-v2-pages cashwarden-webv2
```

**方式二:自己编译**

```shell
git clone https://github.com/cashwarden/web-v2.git cashwarden-webv2
```

本地确保有 [nodejs](https://nodejs.org/) 14. X 以上环境

```shell
npm ci
npm install -g @angular/cli
ng build --prod
```

`dist` 目录就是编译好的代码,是拿来部署的代码。

### 配置 Nginx

```shell
vim /etc/nginx/conf.d/cashwarden.com.conf
```

代码:

```conf
server {
charset utf-8;
client_max_body_size 128M; ## listen for ipv4
server_name www.cashwarden.com cashwarden.com;
index index.html index.htm index.php;
root /root/www/cashwarden-webv2;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss
text/javascript;
location /api {
rewrite /api/(.*) /v1/$1 break;
proxy_pass https://api.cashwarden.com;
}
location / {
try_files $uri $uri/ /index.html;
}
}
```

`server_name``root` 的值需要根据情况修改。


## 支持 HTTPS

使用 [Certbot](https://certbot.eff.org/lets-encrypt/ubuntufocal-nginx)

```shell
sudo apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
```

自动安装 SSL 证书

```shell
sudo certbot --nginx
```

配置系统定时任务:

```shell
sudo crontab -e
```

添加

```shell
1 0 1 * * sudo certbot renew --dry-run
```

## Telegram 支持

### 申请 Telegram Bot

### 初始化 telegram

在 API 项目根目录执行

```shell
php artisan telegram:init
```

## 定时任务

使用 supervisor 管理队列进程

```shell
sudo apt-get install supervisor
cd /etc/supervisor/conf.d/
sudo vim cashwarden-api-v2.conf
```

参考代码:

```conf
[program:cashwarden-api-v2]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /home/www/cashwarden-api-v2/artisan queue:work --queue=high,default,low,later --sleep=3 --tries=3
user = www-data
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
numprocs=5
redirect_stderr=true
stdout_logfile=/tmp/supervisor_cashwarden_api_v2_worker.log
stopwaitsecs=3600
```

0 comments on commit ed34315

Please sign in to comment.