search
HomeBackend DevelopmentPHP TutorialPrepare your Laravel app for the cloud

The recent announcements at LaraconUS have sparked a renewed interest in cloud-based deployments within the Laravel community. As the debate continues on how to deploy your apps, one thing is clear: the cloud is becoming a more viable option for Laravel users.

In this article, we'll explore how to prepare your Laravel app for deployment in a cloud environment using FrankenPHP, Caddy, Dockerfiles, and finally deploying it to Sevalla.

So where do we start? In the local environment of course! ?

#Local development environment

For the sake of simplicity, we'll assume you have a fresh Laravel app installed on your local machine, which connects to a PostgreSQL database to read/write some data.

Before we move on, make sure you have a .env file in your project root with the following content:

.env:

<!-- Syntax highlighted by torchlight.dev -->...
DB_CONNECTION=pgsql
...

Once that's verified, we can start building. ? ☕️

It's always a good idea to have a local development environment that closely resembles your production environment. This way, you can catch any issues early on and avoid surprises when you deploy your app in production.

To mimic the production setup we're going to use Docker and Docker Compose. If you don't have Docker installed on your machine, you can download it from the official website.

#Running Laravel without the database

First off, create a new file called compose.yml in the root of your Laravel project and add the following content:

compose.yml:

<!-- Syntax highlighted by torchlight.dev -->services:
  php:
    image: dunglas/frankenphp:php8.3-bookworm
    environment:
      SERVER_NAME: ":8080"
    ports:
      - 8080:8080
    volumes:
      - .:/app

This configuration file defines a service called php that uses the dunglas/frankenphp:php8.3-bookworm image, which is a FrankenPHP image that includes necessary extensions to run a Laravel app. The SERVER_NAME environment variable configures Caddy to listen on port 8080. We also expose port 8080 to access the app from the host machine.

To test your configuration, try running the following command in your terminal:

<!-- Syntax highlighted by torchlight.dev -->docker compose up [-d]

You should see a Laravel error page explaining the connection was not established to the database because of a missing driver when you navigate to http://localhost:8080 in your browser. This is expected as we haven't connected our Laravel app to a database yet.

Awesome, so far we've configured our Laravel app to be served by a FrankenPHP server.

Next, let's connect our local app with a PostgreSQL database!

#Running Laravel with the database

To connect your Laravel app to a PostgreSQL database, we'll need to do a couple of things.

First, we need to set the following environment variables in your .env file:

.env:

<!-- Syntax highlighted by torchlight.dev -->...
DB_CONNECTION=pgsql
...

Following that, you'll need to add new services to your compose.yml file, and create a custom Dockerfile for your dev environment. Create and update the files with the following content:

Dockerfile.dev:

<!-- Syntax highlighted by torchlight.dev -->services:
  php:
    image: dunglas/frankenphp:php8.3-bookworm
    environment:
      SERVER_NAME: ":8080"
    ports:
      - 8080:8080
    volumes:
      - .:/app

Dockerfile.dev is only meant to be used by your local/development environment, and it extends the dunglas/frankenphp:php8.3-bookworm image to include the pdo_pgsql extension, which is required to connect to a PostgreSQL database.

compose.yml:

<!-- Syntax highlighted by torchlight.dev -->docker compose up [-d]

There is a lot going on here, let's take a look at what has changed and why:

  1. We've updated the php service to use a custom Dockerfile called Dockerfile.dev to build a new image that includes the necessary extensions to connect to a PostgreSQL database.
  2. We've added a new service called db that uses the postgres:16.4-alpine image to run a PostgreSQL database. We've also defined some environment variables to set up the database user, password, and database name.
  3. We've created a new volume called db_data to persist the data in the database on your machine, and Docker could reuse it when you restart the services.
  4. A new service called init was also added that reuses Dockerfile.dev. This image is used to run the php artisan migrate command to run your database migrations. The depends_on key ensures that the db service is up and running before the migrations are run.
  5. The php service now depends on the init service to ensure that the database migrations are run before the Laravel app starts.
  6. We've added a health check to the db service to ensure that the PostgreSQL database is up and running before the init service runs the migrations.

To test your configuration, run the following command in your terminal:

<!-- Syntax highlighted by torchlight.dev -->...
DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432 # default PostgreSQL port
DB_DATABASE=main
DB_USERNAME=admin
DB_PASSWORD=password

Your application should now be connecting to your PostgreSQL database, and your database migrations are always run. ?

Your local envnironment is now ready to mimic your production environment. You can now develop your app locally and test a really similar setup you'll use in production.

#Preparing for production

It's time to make the necessary changes for your production environment.

The first step is to tell Docker which directories it can safely ignore when building the production image. Create a new file called .dockerignore in the root of your Laravel project and add the following content:

.dockerignore:

<!-- Syntax highlighted by torchlight.dev -->FROM dunglas/frankenphp:php8.3-bookworm

RUN install-php-extensions pdo_pgsql

This file tells Docker to ignore the vendor, bootstrap/cache, and tests directories.

Then, create a Dockerfile that will be used to build your production image:

Dockerfile:

<!-- Syntax highlighted by torchlight.dev -->...
DB_CONNECTION=pgsql
...

This Dockerfile is similar to the Dockerfile.dev we created earlier, but it includes a few additional steps:

  1. As the FrankenPHP image uses Caddy as the default web server, we've set the SERVER_NAME environment variable to :8080 to instruct Caddy to listen on port 8080.
  2. We install the @composer PHP extension to install Composer in the image.
  3. composer install command is run to install the dependencies of your Laravel app.
  4. We've set the working directory to /app and copied the contents of your Laravel app to the image.

To test your changes in your local environment, you'll need to produce a production build of your app. Run the following command in your terminal:

<!-- Syntax highlighted by torchlight.dev -->services:
  php:
    image: dunglas/frankenphp:php8.3-bookworm
    environment:
      SERVER_NAME: ":8080"
    ports:
      - 8080:8080
    volumes:
      - .:/app

This command builds a new Docker image called my-laravel-app based on the Dockerfile in the current directory.

To test your newly built production image, use the following command:

<!-- Syntax highlighted by torchlight.dev -->docker compose up [-d]

Replace <your-app-key></your-app-key> with the value of the APP_KEY environment variable in your .env file or grab a key from here.

Visit localhost:8080 in your browser, and your app should start in production mode. It may error due to the lack of a database connection, but that's expected.

#Deploying to the cloud

Now that you have a production-ready Docker image, you can deploy it to a cloud provider. ?

In this tutorial, we'll use Sevalla, a new cloud provider that offers a simple way to deploy Dockerfile-based deployments.

As your app depends on a PostgreSQL database, you're better off provisioning a new PostgreSQL database on Sevalla first. Once you're logged in the Sevalla dashboard,

  1. Navigate to the Create database modal
  2. Select PostgreSQL database
  3. Confirm the settings and create the database

Once your database is ready, you can create your Laravel app on Sevalla.

  1. Navigate to the Create app modal
  2. Select your app's repository from your preferred Git provider
  3. Make sure to select the same data-center your database is in
  4. Set the APP_KEY environment variable required by Laravel
  5. Select Dockerfile as build type
  6. Confirm the rest of the settings and hit "Deploy later" button

If your app is ready, you can now connect it with your PostgreSQL database.

  1. Navigate to the app's page
  2. Go to the "Network" tab
  3. Click on the "Add connection" button and select your PostgreSQL database
  4. Confirm the settings and hit "Connect"

Then, set the following environment variables in the "Environment variables" tab with your database's connection details:

  • DB_CONNECTION
  • DB_HOST
  • DB_PORT
  • DB_DATABASE
  • DB_USERNAME
  • DB_PASSWORD

We recommned using the internal network address of your database as the DB_HOST value. This way, your app can connect to the database over a private network.

The last step is to set up a Job process for your application to run the database mirgations before starting the app.

  1. Navigate to the "Processes" tab
  2. Click on the "Create process" button and select "Job"
  3. Set the command to php artisan migrate --force
  4. Set the start policy to "Before deployment"
  5. Confirm the settings and hit "Create"

If this is also done, you can now initiate a manual deployment of your app in the Deployments tab. ?

If all went well, congrats! You've successfully prepared your Laravel app for the cloud. ?

#Conclusion

In this article, we've explored:

  • How to setup your local environment to mimic your production environment using Docker and docker compose.
  • How to prepare your Laravel app for deployment in a cloud environment using Docker, FrankenPHP, and Caddy.
  • We've also covered how to deploy your app to a cloud provider like Sevalla.

By following these steps, you can take your Laravel app to new heights and enjoy the benefits of cloud-based deployments. ?

#UPDATE: September 8, 2024

We received valuable feedback from the community through a discussion on X.

The first point highlighted that including the php artisan serve command in the compose.yml file was unnecessary, as it bypassed the FrankenPHP server. We've corrected this by updating the relevant sections of the compose.yml file.

The second point, shared by Kévin Dunglas (creator of FrankenPHP), recommended using a Debian-based image (bookworm) instead of Alpine (alpine), as Debian offers better compatibility with PHP and better performance with FrankenPHP. As a result, we updated both the Dockerfile.dev and Dockerfile files.

We're grateful for the community's support and feedback. ? ❤️

The above is the detailed content of Prepare your Laravel app for the cloud. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),