13

I've been struggling for hours trying to install Strapi on my Ubuntu server.

Ubuntu: 20.04
nodejs: v14.19.0
npm: 6.14.16
strapi: 4

npx create-strapi-app@latest my-project --quickinstall

All the installation process goes seamlessly but when I go to http://mydomain:1337/admin in order to create a first user I get this warning:

an error occurred while requesting the API.

I know this problem has been encountered several times but none of the suggested solutions have helped me so far. I also found this error in the Chrome console but I'm not not sure it's related to the my problem:

main.815f1087.js:2 Refused to connect to 'http://localhost:1337/admin/project-type' because it violates the following Content Security Policy directive: "connect-src 'self' https:".

Any idea ?

Duddy67
  • 836
  • 2
  • 10
  • 26

10 Answers10

31

You need to build the app. Read this github issue

Run: npm run build or yarn build or strapi build depending on what you are using

Kholdarbekov
  • 973
  • 1
  • 10
  • 28
5

I had this error after upgrading to latest Strapi and all dependencies, what helped me, is to rebuild the Strapi admin interface:

yarn build

or

npm run build

then start develop

yarn develop

or

npm run develop
atazmin
  • 4,757
  • 1
  • 32
  • 23
1

First Build your Application by using npm run build then run npm start.

1

What I did is to change the host address from 0.0.0.0 to 127.0.0.1 in server.js and then ran npm run build and that fixed my issue. I know it sounds strange but it worked.

Thanks.

jtvdw
  • 79
  • 2
  • 7
1

If you are running strapi CMS on docker, and using docker composer to build, then maintain the default port 1337 when binding the container port to your system port.

Do this: -1337:1337

Not this: -8000:13337

0

Got the same problem, a simple restart of the server did it for me.

Dastan
  • 1,929
  • 2
  • 10
  • 21
0

First of all, need to Build the admin panel and then Start the application.

To build the admin panel,

npm run build
# or
yarn build

To start the application with auto-reload,

npm run develop
# or
yarn develop
Mr. Kabir
  • 695
  • 8
  • 9
0

when you change the port of your strapi app, you must run npm run build or yarn build or strapi build. because strapi admin templates use default port 1337 and for using on another port you must build the project again.

sajjad pld
  • 61
  • 3
0

Running strapi on a server (& behind a reverse-proxy) - you need to set the url:

# path: config/server.js


module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'https://my.example.com',
});
  • rebuild the Admin Panel: npm run build
  • start the dev server: npm run develop

Also useful - run a production build: NODE_ENV=production npm run build

Stuart Cardall
  • 2,099
  • 24
  • 18
0

Well, first thing to do is add url in server.js at "./config/server.js". Also add proxy: true eg:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  app: {
    keys: env.array('APP_KEYS'),
  },
  webhooks: {
    populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
  },
  proxy: true,
  url: "https://yoursite.com",
});

Now change admin.js at "./config/admin.js" eg:

module.exports = ({ env }) => ({
  auth: {
    secret: env('ADMIN_JWT_SECRET'),
  },
  apiToken: {
    salt: env('API_TOKEN_SALT'),
  },
  transfer: {
    token: {
      salt: env('TRANSFER_TOKEN_SALT'),
    },
  },
  port: 1337,
  url: "https://yoursite.com/admin"
});

Note: https is required else you will get error in console something like:

Refused to connect to 'http://yourwebsite/admin/project-type' because it violates the following Content Security Policy directive: "connect-src 'self' https:".

Refused to connect to 'http://yourwebsite/admin/init' because it violates the following Content Security Policy directive: "connect-src 'self' https:".

If you do not have https you might have to change middleware, please see example below security section in strapi documentation: https://docs.strapi.io/dev-docs/configurations/middlewares#security

Very Imp: Lastly you must build strapi app other wise above configuration change will not apply. Make sure you do this:

NODE_ENV=production npm run build
or
NODE_ENV=production yarn run build
rosnk
  • 1,068
  • 1
  • 14
  • 36