Skip to content

9. Deploy ​

The shop works on your machine. In this step you put it under version control, then deploy it the way a host does: a fresh copy of the code, production settings, only the packages production needs, and caches built once. You do it on your own machine first, where you can see every step work, and then put it online.

Run the commands in your second terminal, in the shop folder.

Commit your work ​

A host deploys from a Git repository, and the repository is also your project's history. Create it and commit everything:

sh
git init
git add -A
git commit -m "Build the shop"
text
[… (root-commit) …] Build the shop

The project's .gitignore files keep out what must not be shared or is rebuilt: the .env files with their secrets, the SQLite database, and vendor and node_modules.

Git asks who you are

Git records an author on each commit. Tell it once, then commit again:

sh
git config --global user.name "Your Name"
git config --global user.email you@example.com

Deploy a copy ​

A host starts from a fresh copy of your repository, as you do here with git clone, next to shop:

sh
cd ..
git clone shop shop-production
cd shop-production/api

The API ​

The production settings go in .env. Start from the example, and change three lines:

sh
cp .env.example .env
diff
 APP_NAME="My API"
-APP_ENV=local
+APP_ENV=production
 APP_KEY=
-APP_DEBUG=true
-APP_URL=http://localhost:8000
+APP_DEBUG=false
+APP_URL=http://localhost:8080

APP_DEBUG=false matters most: with it on, an error shows its stack trace and settings to whoever caused it. APP_URL is where the API is reached, which the API reference and pagination links use. Here it is port 8080, so it can run beside your development copy.

Now do what a host does on each deploy. Install the packages without the development tools, create the app's secret key and the database, and run the migrations:

sh
composer install --no-dev --optimize-autoloader
php artisan key:generate --force
touch database/database.sqlite
php artisan migrate --force

--force confirms you mean to change a production app. The key encrypts and signs data, so create it once per environment and keep it: changing it signs everyone out.

Last, build Laravel's caches of the configuration, routes and views, so production does not read them on every request:

sh
php artisan optimize
text
config … DONE
events … DONE
routes … DONE
views … DONE

Check the settings took:

sh
php artisan about --only=environment
text
Environment … production
Debug Mode … OFF
URL … localhost:8080

Start it. Open a third terminal, go to shop-production/api, and run:

sh
php artisan serve --port=8080

php artisan serve is fine for a rehearsal. In production, a web server such as nginx runs PHP instead, or Octane keeps the app in memory between requests.

Back in the second terminal, the production database is empty. Create a key, and keep it in a variable:

sh
php artisan api:key:create "Production check" --scope='*'
text
INFO Created API key 1. Copy it now: it is not stored and cannot be shown again.
fs_live_…
Pinned version … 2027-01-15

New keys are pinned to the latest version, as you saw in step 8, so this one speaks 2027-01-15.

sh
export PROD_KEY=fs_live_…

Add a team and an order:

sh
curl localhost:8080/v1/teams -H "Authorization: Bearer $PROD_KEY" -H "Content-Type: application/vnd.api+json" \
  -d '{"data":{"type":"teams","attributes":{"name":"Acme Ltd"}}}'
json
{
  "data": {
    "id": "1",
    "type": "teams",
    "attributes": { "name": "Acme Ltd", "plan": "free", "created_at": "2026-12-01T10:50:12.000000Z", "updated_at": "2026-12-01T10:50:12.000000Z" }
  }
}
sh
curl localhost:8080/v1/orders -H "Authorization: Bearer $PROD_KEY" -H "Content-Type: application/vnd.api+json" \
  -d '{"data":{"type":"orders","attributes":{"total":4999,"currency":"GBP"},"relationships":{"team":{"data":{"type":"teams","id":"1"}}}}}'
json
{
  "data": {
    "id": "1",
    "type": "orders",
    "attributes": { "status": "pending", "total": 4999, "currency": "GBP", "created_at": "2026-12-01T10:50:13.000000Z" }
  }
}

The web app ​

Go back to the project's root folder, install its packages exactly as package-lock.json lists them, and build it for production:

sh
cd ..
npm ci
API_URL=http://localhost:8080 npm run build
text
…Build complete …

API_URL is the API's address. Set it when you build: the proxy that passes the browser's /v1 requests to the API, and the server-side rendering, keep the address the build had.

The build is in web/.output, and needs nothing else to run. In the third terminal, or a fourth, in shop-production, start it:

sh
PORT=3080 node web/.output/server/index.mjs

The production web app renders the order from the production API:

sh
curl -s localhost:3080/orders -b "fusion_stack_token=$PROD_KEY" | grep -o 'data-test="team">[^<]*'
text
data-test="team">Acme Ltd

That is a deploy: the same commands, on a server, are your production app.

php artisan optimize fails

Run php artisan optimize:clear, fix the error it showed, and run php artisan optimize again. A common cause is reading env() outside the config folder: once the configuration is cached, .env is no longer read, so env() returns null there. Read settings with config() instead.

The web app shows Bad Gateway, or an error with no details

It cannot reach the API. Check the API is running, and that the web app was built with API_URL set to the API's address: build it again with the right one.

A request returns 500 with no details

That is APP_DEBUG=false at work. The details are in the API's storage/logs/laravel.log, under the request's request_id.

Put it online ​

Your own machine is not a server, but every host runs the same steps. You need:

  • A host for the API that runs PHP 8.4: Laravel Cloud, Laravel Forge with a server of your own, or any server with PHP. Point it at your repository's api folder.
  • A host for the web app that runs Node 22: the same server, or any Node host. Build with API_URL set to the API's public address, and run node web/.output/server/index.mjs.

On each deploy, the host runs the commands from this page: composer install --no-dev --optimize-autoloader, php artisan migrate --force and php artisan optimize for the API, and npm ci and the build for the web app. Before you go live:

  • HTTPS. Every host above provides it. Set APP_URL to the https:// address.
  • The settings, in the host's environment rather than a file in the repository: APP_ENV=production, APP_DEBUG=false, APP_URL and an APP_KEY generated once.
  • A database server, such as MySQL or PostgreSQL, for real traffic: set DB_CONNECTION and its DB_ settings. SQLite suits one small server.
  • A queue worker: php artisan queue:work, kept running by the host. Version usage tracking runs on the queue, and so do webhooks, if you add them.
  • Redis, ideally, as the cache: idempotency keys and rate limits use the cache, and Redis handles them best under load. Set CACHE_STORE=redis.

The project also has a CI workflow in .github/workflows/ci.yml. On GitHub, it runs on every push: your tests, style and type checks, and a check that openapi.json and the client match the API. A change that breaks the contract with your frontend is caught before it is deployed.

What you built ​

An API with orders and invoice emails, where every query is one you allowed, every endpoint says what it needs, retries are safe, and a breaking change broke no one. A web app that is typed against it, so a change to the API that would break the page fails the build instead. And a way to deploy both.

From here:

  • Core concepts explains the ideas behind each step in more depth.
  • Webhooks tell other systems when an order changes.
  • MCP server lets AI clients such as Claude use your API, with the same keys and scopes.

Released under the MIT License.