从 0 开始配置 Amazon Linux

305 阅读3分钟

这两天正在搞 AIGC,想要部署一些程序到服务器,方便多个项目通过 API 直接调用 AI 能力。但是因为众所周知的原因,国内网络环境无法直接连接国外的 AI 服务,所以我开始寻找低成本的方式,在国外部署我的 AI 服务。

我的目标瞄准到了亚马逊 AWS,于是就开始了我的配置之路。

我有想过,将一些配置过程写为脚本,方便下次直接使用,但是在我使用过阿里云、联通云、亚马逊云后,我发觉各个云服务器厂商提供的系统环境都不尽相同,所以我决定还是记录下来即可,毕竟我不是专门的运维。并不会天天和服务器打交道。

我之前写了一篇《从 0 开始配置 CentOS 7.9》,于是参考着这篇的路径,我开始了我的配置之路。过程远比我想象的简单得多。对此我虽然有心理准备,因为至少不会遇到各种网络问题,但是整个过程配置下来,顺利程度还是出乎了我的意料。

我的主要目标是为了在服务器部署 firecrawl 服务,所以有些步骤是专门为了 firecrawl 配置的,根据自己的需要跳过即可。

步骤

nginx

  1. yum install -y nginx
  2. systemctl start nginx && systemctl enable nginx

很多参数我没有研究,有些参数往往都是之前遇到问题后加上的,时间久了就忘了是为了解决什么问题。如果比较懂 nginx,不用参考这些配置。

nginx.conf

user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

events {
  worker_connections 1024;
}

http {
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile            on;
  tcp_nopush          on;
  keepalive_timeout   120;
  types_hash_max_size 4096;

  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 10000m;
  client_body_buffer_size 2048k;

  fastcgi_connect_timeout 1800s;
  fastcgi_send_timeout 1800s;
  fastcgi_read_timeout 1800s;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 256k;

  sendfile_max_chunk 512k;
  tcp_nodelay on;

  proxy_connect_timeout 1800s;
  proxy_send_timeout 1800s;
  proxy_read_timeout 1800s;
  server_tokens off;

  proxy_hide_header X-Powered-By;
  proxy_hide_header Server;

  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  include conf.d/gzip.conf;

  upstream firecrawl {
    server 127.0.0.1:9999;
  }

  server {
    listen       80 default_server;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html;

    location /firecrawl/api/ {
      proxy_pass http://firecrawl/;
      include /etc/nginx/conf.d/proxy.conf;
    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
  }
}

conf.d/gzip.conf

gzip            on;
gzip_types      text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_buffers    4 16k;
gzip_comp_level 4;
gzip_vary       on;
gzip_min_length 5k;
gzip_disable    "MSIE [1-6]\.";

conf.d/proxy.conf

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

conf.d/ws_proxy.conf

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 20s;
proxy_read_timeout 600s;
proxy_send_timeout 60s;

nvm & node.js & pnpm

  • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
  • nvm install 20.10.0
  • npm install -g pnpm

conda

  • cd ~
  • curl -LO [https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh](https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh)
  • bash Miniconda3-latest-Linux-x86_64.sh
  • 注意在安装成功最后的提问:Do you wish the installer to initialize Anaconda3 by running conda init? [yes|no],这里一定要回答 yes,否则自己去设置环境变量会很麻烦。

docker & docker-compose

docker

  • sudo yum update -y
  • sudo yum install -y docker
  • sudo systemctl start docker && sudo systemctl enable docker
  • 如果是 Amazon EC2,可以接着运行:sudo usermod -a -G docker ec2-user

参考:Installing Docker to use with the AWS SAM CLI - AWS Serverless Application Model (amazon.com)

docker-compose

  • curl -SL [https://github.com/docker/compose/releases/download/v2.29.6/docker-compose-linux-x86_64](https://github.com/docker/compose/releases/download/v2.29.6/docker-compose-linux-x86_64) -o /usr/local/bin/docker-compose
  • chmod +x /usr/local/bin/docker-compose

参考:Install Compose standalone | Docker Docs

glances

我个人的喜好,习惯使用 glances 监控系统状态。

  • yum install python-pip python-devel
  • pip install glances

参考:Install — Glances 4.1.2 documentation

firecrawl

一些 sudo 权限问题

我希望将 firecrawl 装在 /usr/local/src 目录下,但是会遇到总是要使用 sudo 的情况,为了方便使用:

  • 更改权限(ec2-user 是我的用户名):sudo setfacl -R -m u:ec2-user:rwx /usr/local/src
  • 如果还不成功,可能还需要更改:sudo chmod -R 775 /usr/local/src

git & degit

AI 启动

  • 拉取官方仓库:cd /usr/local/src && degit git@github.com:mendableai/firecrawl.git firecrawl
  • 使用 docker 启动,直接参考官方文档:Self-hosting | Firecrawl
  • docker-compose build && docker-compose up -d

参考