不知道大家平时是怎么部署自己的应用的呢?我自己折腾过UCloud、阿里云、腾讯云(之前活动特价买了好几年)来部署应用。最近接触到了Heroku,由于 Heroku 的部署上线流程异常简单,对新手也很友好,我在这里强烈推荐大家使用它。

本文主要记录一下,我是如何在 Mac 下使用 Heroku部署Laravel应用的。

什么是 Heroku ?

Heroku 是一个支持多种编程语言的 PaaS (Platform-as-a-Service)。在 2010 年被 Salesforce收购。Heroku 作为最开始的云平台之一,从 2007 年 6 月起开始开发,当时它仅支持 Ruby,后来增加了对Java、Node.js、Scala、Clojure、Python 以及 PHP 和 Perl 的支持。

至于如何安装 Heroku,你可以点安装教程,在此之前你必须注册一个 Heroku 账户。

安装完成之后,就可以使用以下命令来登录你的 Heroku 账户了。

1
heroku login

如下图所示:

heroku-login.png

添加 SSH Key 到 Heroku 上:

1
heroku keys:add

如下图所示:

heroku-add-ssh.png

到这还没完,我们要将应用部署到 Heroku 上,我们还需要在项目下新建一个 Procfile 文件,通过配置该文件来告诉 Heroku 应该使用什么命令来启动 Web 服务器,输入以下4条命令即可。

1
2
3
4
cd ~/Code/Laravel ---切换到你的应用目录
echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
git add -A
git commit -m "Procfile for Heroku"

在前面我们已经注册了 Heroku 账户,但是我们还没有创建应用,我们可以使用 heroku create 命令在 Heroku 上创建一个新应用,输入以下命令即可。

1
2
3
heroku create
Creating app... done, ⬢ polar-river-12432
https://polar-river-12432.herokuapp.com/ | https://git.heroku.com/polar-river-12432.git

如下图所示:

heroku-create.png

https://polar-river-12432.herokuapp.com/ 就是Heroku生成的线上访问地址了,其中 “polar-river-12432”是 Heroku随机生成的默认名称,如果你想改也是可以的,但需要保证未被其他人占用,输入下面的命令即可。

1
heroku rename your-app-name

然后我们可以打开Heroku的Web控制台,也可以看到我们已经创建的应用,如下图所示:

heroku-console.png

Heroku 平台支持多种语言,所以在进行应用部署时,Heroku 会自动检查应用的代码是用什么语言写的,因此我们需要对应用的 buildpack 进行声明我们的应用是PHP编写的。声明命令如下:

1
heroku buildpacks:set heroku/php

heroku-buildpack.png

因为Laravel 使用 App Key 来完成对用户会话的加密操作,因此我们也需要将 App Key也加入到Heroku中,使用artisan 命令生成 App Key即可。

1
php artisan key:generate

然后将生成的 App Key 替换掉,输入下面命令的 your_app_key即可,如下图所示:

1
heroku config:set APP_KEY=your_app_key

heroku-artisan-ssh.png

终于来到最后一步,部署应用上线,使用以下命令即可

1
git push heroku master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Counting objects: 113, done.
Compressing objects: 100% (95/95), done.
Writing objects: 100% (113/113), 168.85 KiB | 771.00 KiB/s, done.
Total 113 (delta 10), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote: - php (7.2.10)
remote: - ext-mbstring (bundled with php)
remote: - nginx (1.8.1)
remote: - apache (2.4.34)
remote: -----> Installing dependencies...
remote: Composer version 1.7.2 2018-08-16 16:57:12
remote: Loading composer repositories with package information
remote: Installing dependencies from lock file
remote: Package operations: 38 installs, 0 updates, 0 removals
remote: - Installing doctrine/inflector (v1.3.0): Downloading (100%)
remote: - Installing doctrine/lexer (v1.0.1): Downloading (100%)
remote: - Installing erusev/parsedown (1.7.1): Downloading (100%)
remote: - Installing vlucas/phpdotenv (v2.5.1): Downloading (100%)
remote: - Installing symfony/css-selector (v4.1.4): Downloading (100%)
remote: - Installing tijsverkoyen/css-to-inline-styles (2.2.1): Downloading (100%)
remote: - Installing symfony/polyfill-mbstring (v1.9.0): Downloading (100%)
remote: - Installing symfony/var-dumper (v3.4.15): Downloading (100%)
remote: - Installing symfony/routing (v3.4.15): Downloading (100%)
remote: - Installing symfony/process (v3.4.15): Downloading (100%)
remote: - Installing symfony/polyfill-ctype (v1.9.0): Downloading (100%)
remote: - Installing paragonie/random_compat (v9.99.99): Downloading (100%)
remote: - Installing symfony/polyfill-php70 (v1.9.0): Downloading (100%)
remote: - Installing symfony/http-foundation (v3.4.15): Downloading (100%)
remote: - Installing symfony/event-dispatcher (v4.1.4): Downloading (100%)
remote: - Installing psr/log (1.0.2): Downloading (100%)
remote: - Installing symfony/debug (v3.4.15): Downloading (100%)
remote: - Installing symfony/http-kernel (v3.4.15): Downloading (100%)
remote: - Installing symfony/finder (v3.4.15): Downloading (100%)
remote: - Installing symfony/console (v3.4.15): Downloading (100%)
remote: - Installing egulias/email-validator (2.1.5): Downloading (100%)
remote: - Installing swiftmailer/swiftmailer (v6.1.3): Downloading (100%)
remote: - Installing ramsey/uuid (3.8.0): Downloading (100%)
remote: - Installing psr/simple-cache (1.0.1): Downloading (100%)
remote: - Installing psr/container (1.0.0): Downloading (100%)
remote: - Installing symfony/translation (v4.1.4): Downloading (100%)
remote: - Installing nesbot/carbon (1.33.0): Downloading (100%)
remote: - Installing mtdowling/cron-expression (v1.2.1): Downloading (100%)
remote: - Installing monolog/monolog (1.23.0): Downloading (100%)
remote: - Installing league/flysystem (1.0.47): Downloading (100%)
remote: - Installing laravel/framework (v5.5.43): Downloading (100%)
remote: - Installing fideloper/proxy (3.3.4): Downloading (100%)
remote: - Installing jakub-onderka/php-console-color (0.1): Downloading (100%)
remote: - Installing nikic/php-parser (v4.0.3): Downloading (100%)
remote: - Installing jakub-onderka/php-console-highlighter (v0.3.2): Downloading (100%)
remote: - Installing dnoegel/php-xdg-base-dir (0.1): Downloading (100%)
remote: - Installing psy/psysh (v0.9.8): Downloading (100%)
remote: - Installing laravel/tinker (v1.0.7): Downloading (100%)
remote: Generating optimized autoload files
remote: > Illuminate\Foundation\ComposerScripts::postAutoloadDump
remote: > @php artisan package:discover
remote: Discovered Package: fideloper/proxy
remote: Discovered Package: laravel/tinker
remote: Discovered Package: nesbot/carbon
remote: Package manifest generated successfully.
remote: -----> Preparing runtime environment...
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 17.7M
remote: -----> Launching...
remote: Released v4
remote: https://polar-river-12432.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/polar-river-12432.git
* [new branch] master -> master

是不是迫不及待想看看效果啦?使用下面的命令就可以啦,如果无法在浏览器顺利打开,可根据命令行输出提示中给出的链接直接访问。

1
heroku open

至此,我们使用Heroku完成了 Laravel 应用的部署啦。