部署
V2 分开部署方案

V2 分开部署方案

Ubuntu

安装 PHP

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 php8.1-intl -y

安装 Composer

可以参考官网 Download Composer (opens in a new tab),或者这样安装:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

安装 MySQL

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

sudo apt-get install mysql-server
sudo mysql_secure_installation

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

* 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

进入 MySQL

sudo mysql

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

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

刷新

FLUSH PRIVILEGES;

【可选操作】添加新用户(下面 newuser 是新用户,password 为新密码,要求最高强度):

ALTER USER 'newuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

删除 Apache

如果你需要的话

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 项目

安装

cd /srv
git clone -b prod 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

sudo apt install nginx -y
sudo vim /etc/nginx/conf.d/api.cashwarden.com.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 /srv/cashwarden-apiv2/public;

    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_nameroot 的值需要根据情况修改。

然后重启 nginx

sudo service nginx reload

安装 Redis

sudo apt install redis

配置定时任务

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

crontab -e

添加代码:

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

配置文件说明

.env 文件,默认是本地开发环境

APP_ENV=local
APP_DEBUG=true

如果是生产环境,需要修改为:

APP_ENV=prod
APP_DEBUG=false

默认注册用户要走验证邮箱流程,请修改:

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=no-reply@cashwarden.com
MAIL_FROM_NAME="${APP_NAME}"

注册用户不想邮箱验证流程,可以修改为:

MUST_VERIFY_EMAIL=false

WEB 项目

安装

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

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

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

方式二:自己编译

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

本地确保有 nodejs (opens in a new tab) 16. X 以上环境

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

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

配置 Nginx

sudo vim /etc/nginx/conf.d/cashwarden.com.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 /srv/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/(.*) /api/$1  break;
        proxy_pass https://api.cashwarden.com;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }
}

server_nameroot 还有 proxy_pass 的值需要根据情况修改。

测试配置文件是否正确

sudo service nginx reload
sudo nginx -t

然后去配置域名的 DNS 解析,添加 A 记录,指向服务器 IP 地址。我这里需要给三个解析记录,分别是:

你可以根据自己的情况,添加相应的解析记录。

支持 HTTPS

使用 Certbot (opens in a new tab)

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 证书

sudo certbot --nginx

输入一个邮箱,然后输入 2 次 Y,就可以了。

配置系统定时任务:

sudo crontab -e

添加

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

测试

访问 https://api.cashwarden.com/api/site/config (opens in a new tab) 看看是否正常返回数据。

Telegram 支持

申请 Telegram Bot

初始化 telegram

在 API 项目修改 .env

TELEGRAM_BOT_NAME=TestCashWardenBot
TELEGRAM_BOT_TOKEN=xx

然后执行

php artisan telegram:init

定时任务

使用 supervisor 管理队列进程

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

参考代码:

[program:cashwarden-api-v2]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /srv/cashwarden-apiv2/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_apiv2_worker.log
stopwaitsecs=3600

常见问题

Nginx 500 错误

查看 nginx 错误日志路径

sudo vim /etc/nginx/nginx.conf

error_log 配置,一般都是 /var/log/nginx/error.log;