Directus在Ubuntu正式生产环境的安装和部署

因为外网对nodejs生产环境的OS推荐使用ubuntu所以也把digitalocean买的droplet也弄了个Ubuntu的系统,首先确认系统安装好node和npm

sudo apt update
sudo apt install nodejs

输入node -v检查安装情况

v12.22.9

最后安装npm

sudo apt install npm

接下来正式进入主题

一 Directus项目创建

直接使用Directusnpm的包,非常简单

npm init directus-project my-project

项目的启动

npx directus start

输出信息

root@ubuntu-s-1vcpu-1gb-intel-fra1-01:/mnt/projects/saucouncil/backend# npx directus start
11:26:24 ??  PUBLIC_URL should be a full URL
11:26:24 ??  Spatialite isn't installed. Geometry type support will be limited.
11:26:24 ? Server started at http://0.0.0.0:8055

这时候访问IP地址加端口我们就可以正常访问directus的登陆页面了

directus可以访问了

端口的修改

官方的这篇文档有很详细的说明,但是最简单的方法是修改.env文件,找到PORT=8055修改为自己想要的端口即可。

注意:部署多应用一定要修改成不同的端口。

二 nodejs守护进程

接下来我们需要让node服务在后台运行,而不是每次都使用npx directus start命令来启动,早在18年我就写过,在这里依旧推荐使用pm2来管理后台程序进程,所以我们实现这一功能需要安装pm2

sudo npm install pm2@latest -g

安装完毕后找到directus项目根目录下的package.json文件,在scripts区域中加入参数"start": "directus start"

如下所示

{
  "name": "...",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "directus start"
  },
  ...
  }
}

保存后使用pm2 start npm -- start来启动项目。

如果服务器上有很多node应用,在这这里也可以使用 pm2 start npm --name "app name" -- start 可以指定应用名称方便管理

PM2启动成功

最后使用pm2 save命令来保存当前配置

pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2

设置随系统自启动

pm2 startup systemd

输出信息

pm2 startup systemd
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

pm2常用管理命令

保存 PM2 进程列表和相应的环境

pm2 save

使用systemctl启动服务,注意后面的root需要改为自己的用户名

systemctl start pm2-root

查看服务状态

systemctl status pm2-root

停止应用服务进程

pm2 stop app_name_or_id

重启服务

pm2 restart app_name_or_id

查看当前所有的服务进程列表

pm2 list

查看某应用的服务信息

pm2 info app_name

监控应用程序状态、CPU 和内存使用情况

pm2 monit

三 将 Nginx 设置为反向代理服务器

安装nginx

sudo apt update
sudo apt install nginx

设置防火墙通行规则

sudo ufw app list

输出

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

给nginx放行

sudo ufw allow 'Nginx HTTP'

查看防火墙状态

sudo ufw status

检查nginx服务状态

运行

systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-07-11 13:43:17 UTC; 2min 17s ago
       Docs: man:nginx(8)
   Main PID: 2122 (nginx)
      Tasks: 2 (limit: 1131)
     Memory: 6.2M
     CGroup: /system.slice/nginx.service
             ├─2122 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─2123 nginx: worker process

这时候如果访问你的IP地址应该能看到nginx的默认页

NGINX默认页

Nginx的常用管理命令

设置随系统自启动

sudo systemctl enable nginx

关闭自启动

sudo systemctl disable nginx

停止服务

sudo systemctl stop nginx

开启服务

sudo systemctl start nginx

重启服务

sudo systemctl reload nginx

设置虚拟主机

sudo vi /etc/nginx/sites-available/yourdomain.com

location区域输入以下内容

小提示:这里可以建立多个server让多个app生效。

server {
        listen 80;
        listen [::]:80;

        server_name yourdomain.com;

        location / {
            proxy_pass http://localhost:8055;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
}

建立软连接

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

可选操作:为了避免server names引起的内存错误问题需要打开文件/etc/nginx/nginx.conf,找到server_names_hash_bucket_size 参数,将前面的注释取消

...
http {
    ...
    server_names_hash_bucket_size 64;
    ...
}
...

测试nginx语法并重启服务

sudo nginx -t
sudo systemctl restart nginx

最后别忘了解析域名到服务器IP,然后生效后就可以用域名访问了。

最后关于SSL的处理,如果需要配置HTTPS可以参考下文:

Post Comment