Getting Started with PHP Laravel Web App
A live example is available at https://laravel.nullstone.dev.
This quickstart launches a PHP Laravel Web App to AWS via Nullstone. It also configures a local development environment using Docker that works identical to production, but with debugging enabled.
This quickstart contains a walkthrough for generating a Laravel app. A working example is available to fork at nullstone-io/laravel-quickstart.
TIP
This quickstart is based off the official Your First Laravel Project guide
Create Laravel app
Generate app
In this example, we are going to create a Laravel web app in the current directory. In your repository root, run this command.
composer create-project laravel/laravel .
This will create the following files and directories:
.
├── .env
├── .env.example
├── .gitattributes
├── .styleci.yml
├── app
├── artisan
├── bootstrap
├── composer.json
├── composer.lock
├── config
├── database
├── lang
├── package.json
├── phpunit.xml
├── public
├── resources
├── routes
├── storage
├── tests
├── vendor
└── webpack.mix.js
Configure docker locally
TIP
Nullstone provides a docker image nullstone/laravel:local
that is configured for local development. The source for the docker image is on GitHub at nullstone-io/docker-laravel.
Now, create a docker-compose.yml
to run locally using Docker with an attached postgres Docker container.
version: "3.8"
services:
app:
image: nullstone/laravel:local
volumes:
- .:/app
- vendor:/app/vendor
ports:
- "9000:9000"
environment:
- NULLSTONE_ENV=local
- APP_KEY=${APP_KEY}
- POSTGRES_URL=postgres://acme:acme@db:5432/acme
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_USER=acme
- POSTGRES_PASSWORD=acme
- POSTGRES_DB=acme
volumes:
vendor: {}
Let's start our app locally.
docker compose up
Visit http://localhost:9000.
TIP
Even though this is running in docker, debugging and hot-reloading is enabled similar to your local environment. When you make changes to code, there is no need to rebuild/restart your docker container.
Update dependencies
As you add dependencies to your application, ensure that there is an entry for the dependency in composer.json
. Then, restart your docker container with docker compose up
or docker compose restart
. The local docker image will install dependencies on boot.
Prepare for Production
Before deploying your Laravel app to production, we need to dockerize the app. In this section, we will create a simple Dockerfile
.
Create Dockerfile
# syntax=docker/dockerfile:1
FROM nullstone/laravel
# Copy code, install dependencies
COPY --chown=www-data:www-data . .
RUN composer install --optimize-autoloader --no-dev
RUN chown -R www-data:www-data /app/storage
RUN chmod -R 755 /app/storage
Launch to Nullstone
Create app
When launching to Nullstone, we're going to create an app in the Nullstone UI and attach capabilities that automatically configure our app. Follow these steps in the Nullstone UI.
- Create an application.
- Name: In this example, we're naming our app
laravel-quickstart
- Framework:
laravel
- App Type:
Container
- Name: In this example, we're naming our app
- From the Domains tab for the application, add a subdomain. (This will automatically attach a load balancer capability)
- From the Capabilities tab for the application, add a capability named
APP_KEY for Laravel
. - From the Capabilities tab for the application, add a capability named
Nginx Sidecar for Fargate Service
.
Create postgresql datastore
- Create a datastore -
RDS Postgres Cluster
- Visit your application created in the previous step.
- From the Datastores tab, add the datastore you just created.
Provision
Our application is ready to launch. Click "Launch" through the UI or issue up
through the CLI.
nullstone up --wait --app=laravel-quickstart --env=dev
Build
Once your application is provisioned, you may build and deploy your app.
You can name your image whatever you want, just remember this image name for the deploy step. In this example, we are using an image name of laravel-app
.
docker build -t laravel-app .
Deploy
Now, issue launch
to push your docker image and deploy the service with a new version.
nullstone launch --source=laravel-app --app=laravel-quickstart --env=dev
Troubleshooting
Auto-versioning
When pushing your image, Nullstone performs auto-versioning if you are in a git-tracked directory. Nullstone selects the short commit SHA (a unique 8-character token) from the git repository to tag the docker image.
To use a manual version, issue launch with --version
(this example uses 1.0.0
).
nullstone launch --source=laravel-app --app=laravel-quickstart --env=dev --version=1.0.0
Version conflicts
Nullstone enforces version/image tag immutability for security reasons.
If you repeatedly push a new docker image without committing anything to git, you will receive an error message like this:
error pushing artifact: error pushing image: tag invalid: The image tag 'c3c7cd83' already exists in the 'periwinkle-louse-fkslv' repository and cannot be overwritten because the repository is immutable.
The easiest way to resolve this is to launch with an indexed version. The following uses the same commit sha, but with a -2
suffix to distinguish the image tag.
nullstone launch --source=laravel-app --app=laravel-quickstart --env=dev --version=c3c7cd83-2