It is recommended to do an initial installation on hosted server without all dev packages and tools. The best approach is to prepare a local docker installation where it is possible to run composer and NPM. For this purpose, we can use an intermediate server for clean code generation in PHP and css/js. The server is also used for final testing of the application before a transfer of cleaned code to the live server.

This is an image

In order to have the closest possible conditions for running the application on the deployment server, the Apache web server is used here instead of the nginx development server.

Here are basic tasks necessary for preparation of deployment server:

  • run on development server webpack.mix.js to generate production clean code for CSS /js files in custom /deploy/resources folder (needs adjustment of webpack.mix.js)
  • fast copy of application code to the deployment server. On Windows/WSL2 is the fastest file transfer achieved with copy inside virtual Ubuntu distribution. EXAMPLE of copy_to_deploy.sh from application root on dev server /projects/laravel-blog to deployment server located in /projects/apache folder.
#!/usr/bin/env bash  
cd /projects/laravel-blog  
cp -R laravel/app ../apache/web  
cp -R laravel/bootstrap ../apache/web  
cp -R laravel/config ../apache/web  
cp -R laravel/database ../apache/web  
cp -R laravel/resources ../apache/web  
cp -R laravel/routes ../apache/web  
cp -R laravel/storage ../apache/web  
cp -R laravel/themes ../apache/web  
cp -R laravel/vendor ../apache/web
cp laravel/.env ../$target_dir_laravel/.env
cp laravel/artisan ../$target_dir_laravel/artisan
cp laravel/composer.json ../$target_dir_laravel/composer.json
cp laravel/composer.lock ../$target_dir_laravel/composer.lock

Don't omit composer.lock because it contains important information on package-versions that should be used.

  • composer.json needs to be customized for production - remove post-installation scripts.
  • in .env file, it is necessary to set:
APP_ENV=production    
APP_DEBUG=false

When performing a production deploy, you can pass the --no-dev flag to Composer:

composer.phar install --optimize-autoloader --no-dev

The composer.lock file might contain information about dev-packages. This doesn't matter. The --no-dev flag will make sure those dev-packages are not installed.

For correct presentation of images on deployment server, it is necessary to create a symbolic link with artisan: php artisan storage:link