laravel5.5 heroku上线部署
程序员文章站
2022-05-20 21:54:04
...
laravel5.5 heroku上线部署
1.登录你的 Heroku 账号
heroku login
2.添加 SSH Key 到 Heroku
heroku keys:add
3.Laravel 项目通过配置 Procfile 文件来告诉 Heroku 应当使用什么命令来启动 Web 服务器。
echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
4.在 Heroku 上创建一个新应用
heroku create appname
5.用 buildpack 声明应用是用 PHP 写的。
heroku buildpacks:set heroku/php
6.使用 Laravel 自带的 artisan 命令来生成 App Key
php artisan key:generate
7.heroku设置 APP key
heroku config:set APP_KEY=<your_app_key>
8.heroku添加postgresql
heroku addons:add heroku-postgresql:hobby-dev
heroku config:set IS_IN_HEROKU=true
bootstrap/helpers.php
<?php
function get_db_config()
{
if (getenv('IS_IN_HEROKU')) {
$url = parse_url(getenv("DATABASE_URL"));
return $db_config = [
'connection' => 'pgsql',
'host' => $url["host"],
'database' => substr($url["path"], 1),
'username' => $url["user"],
'password' => $url["pass"],
];
} else {
return $db_config = [
'connection' => env('DB_CONNECTION', 'mysql'),
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
];
}
}
bootstrap/app.php
<?php
require __DIR__.'/helpers.php';
.
.
.
config/database.php
<?php
$db_config = get_db_config();
return [
'default' => $db_config['connection'],
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => $db_config['host'],
'port' => env('DB_PORT', '5432'),
'database' => $db_config['database'],
'username' => $db_config['username'],
'password' => $db_config['password'],
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];
9.部署上线
git push heroku master
10.数据迁移
heroku run php artisan migrate
heroku run php artisan db:seed
问题
1.Class ‘Faker\Factory’ not found
composer.json中的dev中添加fzaninotto/faker
然后
composer update
最后重新部署上线
git push heroku master
上一篇: laravel5.5集成声网SDK