问题
I have been using the official Strapi tutorial on how to deploy strapi to heroku with postgres and after following all the instructions, my heroku app is showing an error. However when I check the build logs, there are no errors and they show the build successful message.
build logs
2020-08-17T15:48:19.744258+00:00 app[web.1]: npm ERR!
2020-08-17T15:48:19.744486+00:00 app[web.1]: npm ERR! Failed at the radio-darya-backend@0.1.0 start script.
2020-08-17T15:48:19.744753+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-08-17T15:48:19.756754+00:00 app[web.1]:
2020-08-17T15:48:19.757071+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-08-17T15:48:19.757252+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-08-17T15_48_19_747Z-debug.log
2020-08-17T15:48:19.825573+00:00 heroku[web.1]: Process exited with status 1
2020-08-17T15:48:19.869487+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-17T15:48:32.221633+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=radio-darya-backend.herokuapp.com request_id=1bceee5d-4452-4b2a-9638-d5f242b4337c fwd="213.162.246.193" dyno= connect= service= status=503 bytes= protocol=https
2020-08-17T15:48:32.751425+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=radio-darya-backend.herokuapp.com request_id=95d4de1a-5f17-49e3-bed2-b459bce9259e fwd="213.162.246.193" dyno= connect= service= status=503 bytes= protocol=https
package.json dependencies
"devDependencies": {},
"dependencies": {
"knex": "<0.20.0",
"pg": "^8.3.0",
"sqlite3": "latest",
"strapi": "3.1.4",
"strapi-admin": "3.1.4",
"strapi-connector-bookshelf": "3.1.4",
"strapi-plugin-content-manager": "3.1.4",
"strapi-plugin-content-type-builder": "3.1.4",
"strapi-plugin-email": "3.1.4",
"strapi-plugin-upload": "3.1.4",
"strapi-plugin-users-permissions": "3.1.4",
"strapi-utils": "3.1.4"
},
config database.js
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
"client":"postgres",
"host":"${process.env.DATABASE_HOST}",
"port": "${process.env.DATABASE_PORT}",
"database": "${process.env.DATABASE_NAME}",
"username": "${process.env.DATABASE_USERNAME}",
"password": "${process.env.DATABASE_PASSWORD}",
"ssl": { "rejectUnauthorized": false }
},
options: {
},
},
},
});
her
回答1:
You didn’t give the error message you’re having?
I did deploy my strapi to heroku a few weeks ago and there was no problems.
You are sure you followed all the steps from strapi documentation?
Only thing I could think to go wrong is database connection.
First you have to install postgress addon in Heroku, then get the config-info and last add environment variables in Heroku (settings/config vars) and also modify strapi config-files to get the database information from environment variables.
Strapi documentation: https://strapi.io/documentation/3.0.0-beta.x/deployment/heroku.html.
EDIT:
Strapi documentation isn’t correct at the moment, database.json file and location has been changed. See:
strapi database.js / multiple database configs
https://www.youtube.com/watch?v=xNE0TrI5OKk
回答2:
ive just completed that tutorial a day ago... and i also had problems for a complete beginner to strapi and postgres and heroku... heres my experience.
follow along here to get postgres and the database setup: https://tute.io/install-configure-strapi-postgresql
then complete your setup with the missing pieces from here: https://strapi.io/documentation/v3.x/deployment/heroku.html
basically: have postgres installed locally on your system, create the db and user and grant permissions. install strapi without the quickstart flag and use the details that was used above. use the heroku cli to set the configs derived from the database_url configvar. commit and push and all should be well.
EDIT: In Heroku under the app in question:
- click setting and scroll down to Environment variable and click Reveal Config Vars
- Ensure that you have a DATABASE_URL config var
- Ensure that your postgres specific config vars match up to the DATABASE_URL
eg. DATABASE_URL = postgres://ebitxebvixeeqd:dc59b16dedb3a1eef84d4999sb4baf@ec2-50-37-231-192.compute-2.amazonaws.com: 5432/d516fp1u21ph7b
- It's read like so: postgres:// USERNAME : PASSWORD @ HOST : PORT : DATABASE_NAME
Also in your ./config/server.js file make sure your host is 0.0.0.0
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
admin: {
auth: {
secret: env('ADMIN_JWT_SECRET', '***********************'),
},
},
});
Also change your database.js config to be:
settings: {
client: 'postgres',
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'strapi'),
username: env('DATABASE_USERNAME', 'postgres'),
password: env('DATABASE_PASSWORD', ''),
ssl: env.bool('DATABASE_SSL', false),
},
options: {}
Theres no much to go on from your question or the logs, but the above is basically some common issues ive experienced.
回答3:
This is working for me for v3.x strapi.
// Path: ./config/env/production/database.js
const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
host: config.host,
port: config.port,
database: config.database,
username: config.user,
password: config.password,
},
options: {
ssl: false,
},
},
},
});
We also need to set the NODE_ENV variable on Heroku to production to ensure this new database configuration file is used.
heroku config:set NODE_ENV=production
see https://strapi.io/documentation/v3.x/deployment/heroku.html
Note here is v3.x instead of beta version. Google "strapi heroku postgres" for the moment still give legacy beta version.
回答4:
copy this code directly in the config/database.js
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client:'postgres',
host:`${process.env.DATABASE_HOST}`,
port: `${process.env.DATABASE_PORT}`,
database: `${process.env.DATABASE_NAME}`,
username: `${process.env.DATABASE_USERNAME}`,
password: `${process.env.DATABASE_PASSWORD}`,
ssl: { "rejectUnauthorized": false }
},
options: {
},
},
},
});
来源:https://stackoverflow.com/questions/63441231/how-to-deploy-strapi-backend-to-heroku-with-postgres-addon