DEV Community

Lê Vũ Huy
Lê Vũ Huy

Posted on

Deploy Strapi trên Centos 7 sử dụng Nginx, PM2

1. Clone repo to VPS

2. Tạo file ecosystem.config.js trong project

module.exports = {
  apps: [
    {
      name: "strapi-app",
      script: "npm",
      args: "run develop",
      exp_backoff_restart_delay: 100,
    },
  ],
};
Enter fullscreen mode Exit fullscreen mode

Config domain cho project strapi: ./config/server.ts

export default ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'http://admin.yourdomain.com',
  app: {
    keys: env.array('APP_KEYS'),
  },
  webhooks: {
    populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
  },
});
Enter fullscreen mode Exit fullscreen mode

3. Chạy pm2 service

npm i -g pm2
pm2 start ecosystem.config.js
Enter fullscreen mode Exit fullscreen mode

4. Cài đặt Nginx

Hướng dẫn

5. Tạo file config nginx:

Tạo file /etc/nginx/con.f/loyalty-strapi.conf (thường sẽ đặt tên file theo tên app)

upstream strapi {
    server 127.0.0.1:1337;
}

server {
    # Listen HTTP
    listen 80;
    listen [::]:80;
    server_name admin.yourdomain.com; 

        client_max_body_size 100M;

location / {
        proxy_pass http://strapi;

        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }
}
Enter fullscreen mode Exit fullscreen mode

Bạn cần thay thế các thông tin domain của bạn vào field server_name

6. Chạy nginx dưới user root:

mở file /etc/nginx/nginx.conf

//change this
user nginx;

//to this
user root;
Enter fullscreen mode Exit fullscreen mode

7. Restart nginx:

sudo systemctl restart nginx

8. Troubleshoot

1. Permission denied

 connect() to 127.0.0.1:1337 failed (13: Permission denied) while connecting to upstream, client: 42.114.89.25, server: admin.yourdomain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:1337/favicon.ico", host: "103.185.184.216", referrer: "http://103.185.184.216/"
Enter fullscreen mode Exit fullscreen mode

→ chạy lệnh

setsebool -P httpd_can_network_connect 1

Top comments (0)