Laravel sail

Laravel sail

Sail with Apache server

Laravel Sail is a great choice for Dockerizing your Laravel app during development, making things easy. But using it on a real server requires caution. It's still in development mode, so it's not recommended for production environments.

After installing Laravel sail as mentioned in the documentation you will find a docker-compose.yml file and the following is my sample example:

services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.3
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '81:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            IGNITION_LOCAL_SITES_PATH: '${PWD}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3307}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    phpmyadmin:
        image: 'phpmyadmin:latest'
        ports:
            - 8090:80
        networks:
            - sail
        environment:
            - PMA_ARBITRARY=1
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

In the example you will find that:

  1. Laravel app itself running on port 81

  2. mysql running on port 3307

  3. phpmyadmin running on port 8090

So your website will run as yoursite.com:81, And sub pages will be like yoursite.com:81/sub-page. This will be annoying and not good for SEO, So we need to handle it on server configurations using proxy.

I will give an example to handle it using Apache.

On server go to Apache conf path usually it will be on /etc/apache2/sites-available

Here you will find my conf example.

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName yoursite.com

    SSLEngine on
    ProxyPreserveHost On
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://yoursite.com:81/
    ProxyPassReverse / http://yoursite.com:81/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
SSLCertificateFile /etc/letsencrypt/live/yoursite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yoursite.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

This example is using proxypass so now your site will run Laravel sail without displaying the port

Note that this Apache conf file is related to SSL conf file only. Conf file without SSL conf as follow:

<VirtualHost *:80>
    ServerName yoursite.com

    ProxyPreserveHost On
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://yoursite.com:81/ retry=1 acquire=3000 timeout=600 Keepalive=On
    ProxyPassReverse / http://yoursite.com:81/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
RewriteEngine on
RewriteCond %{SERVER_NAME} =yoursite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Last thing go to your project path then run

./vendor/bin/sail up -d

And open your link that's it.