Writing

Deploy laravel with deployer

10/02/2024

10 mins to read

Share article

How to deploy a Laravel project with deployer on a remote server.

Install deployer (local)

## on local terminal

composer require deployer/deployer --dev
vendor/bin/dep init
# answer the questions:
# php
# laravel
# mysite.com
# open deploy.php and edit it
# if you dont have a db connection yet you can disable the migrations with
# task('artisan:migrate')->disable();

Create new user and setup ssh authorized_keys (remote)


## on remote server

sudo adduser --disabled-password deploy
cd /home/deploy
sudo -u deploy mkdir .ssh
sudo -u deploy nano .ssh/authorized_keys

# find your public ssh key from your pc (or generate a new one with 'ssh-keygen -t rsa -b 4096 -C "your.email@gmail.com or keyname"') and paste it in the authorized_keys file

Setup server with nginx and prepare folders (remote)

## on remote server

cd /var/www
sudo mkdir mysite.com
sudo chown -R deploy:deploy mysite.com
cd mysite.com
sudo mkdir logs
touch logs/access.log && touch logs/error.log
sudo chown -R www-data:www-data ./logs/
cd /etc/nginx/sites-available
sudo nano mysite.com

Nginx configuration

server {
  listen 80;
  listen [::]:80;

  server_name mysite.com www.mysite.com;

  # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
  return 302 https://$host$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/ssl/mysite.com/cert.pem;
  ssl_certificate_key /etc/ssl/mysite.com/key.pem;

  server_name mysite.com www.mysite.com;

  root /var/www/mysite.com/current/public;
  index index.php;
  access_log /var/www/mysite.com/logs/access.log;
  error_log /var/www/mysite.com/logs/error.log;

  add_header Strict-Transport-Security max-age=15768000;
  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-Content-Type-Options "nosniff";

  charset utf-8;

  location / {
      try_files $uri $uri/ /index.php?$query_string;
  }

  location = /favicon.ico { access_log off; log_not_found off; }
  location = /robots.txt  { access_log off; log_not_found off; }

  error_page 404 /index.php;

  location ~ \.php$ {
      fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      include fastcgi_params;
  }

  location ~ /\.(?!well-known).* {
      deny all;
  }
}
# enable the site (create a symlink)
sudo ln -s ../sites-available/mysite.com /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

Prepare laravel project (remote)


## on local terminal

vendor/bin/dep deploy

## on remote server

cd /var/www/mysite.com/current
sudo -u deploy cp /var/www/mysite.com/current/.env.example /var/www/mysite.com/shared/.env
sudo -u deploy php artisan config:clear && sudo -u deploy php artisan config:cache && sudo -u deploy php artisan key:generate

Deploy (local)

## on local terminal

#every time you push a change to your repository you can deploy with on your local terminal
vendor/bin/dep deploy
© 2025, Nikodem Kedzierski - All rights reserved.