Skip to content

Latest commit

 

History

History
85 lines (61 loc) · 3.74 KB

File metadata and controls

85 lines (61 loc) · 3.74 KB

Deploy Laravel with Nginx FPM

Initial Deployment - Non Container

This is a brief instruction how to setup web server for Laravel framework using:

  • PHP (include php-fpm)
  • Nginx
  • Postgres
  • Redis (optional)

Warning

You must run these steps as a root user or user with sudo access.

  1. Create a droplet in Digital Ocean with latest Ubuntu LTS. You may choose droplet with $6 or $12.
$6 $12
1GB vCPU 1GB vCPU
1GB RAM 2GB RAM
25GB SSD 50GB SSD
1000 GiB transfer 2000 GiB transfer
  1. Once droplet created, create A record by using the IP public of droplet into DNS. I'm using Cloudflare as DNS. We will use the Name of domain as a hostname in the deploy.php file.
Type Name Content Proxy Status TTL
A web.app droplet-ip-public Proxied  

Only one record, kept simple. Using Proxied, real droplet IP hidden behind Cloudflare's proxy IPs — visitors and attackers see only Cloudflare's IP, not yours. Bonus: CDN caching, DDoS protection, free SSL termination, no direct exposure of origin server.

Use web.app as the hostname in deploy.php and SSH_KNOWN_HOSTS.

  1. Put setup.sh file into root directory in the droplet and make it executable. This executable file do:
  • Install PHP and PHP extensions
  • Install Nginx
  • Install Composer
  • Create /var/www directory
  • Install PostgreSQL
  • Configure PostgreSQL database
  • Setup daily DB backup to Cloudflare R2 (optional)
  • Install Redis
  • Install Certbot
  • Install ACL
  • Create deployer user
  • Configure deployer sudo
  • Create SSH key pair
  • Display SSH info
chmod +x setup.sh
./setup.sh --db-name=yourdbname --db-user=yourdbuser --db-pass=yourdbpassword

Tip

If you want to install redis and/or mariadb, it will be like this: ./setup.sh --db-name=yourdbname --db-user=yourdbuser --db-pass=yourdbpassword --with-redis --db-engine=mariadb.

Tip

To enable daily DB backups to Cloudflare R2, add --with-backup plus the R2 credentials: ./setup.sh --db-name=yourdbname --db-user=yourdbuser --db-pass=yourdbpassword --with-backup --r2-endpoint=https://xxxx.r2.cloudflarestorage.com --r2-bucket=my-backups --r2-access-key=xxx --r2-secret-key=xxx. This installs the AWS CLI, drops a backup script at /usr/local/bin/{mariadb,postgresql}-backup.sh, and schedules it via cron at 00:00 daily.

After setup.sh executed, it will generate SSH_PRIVATE_KEY and SSH_KNOWN_HOSTS from deployer user. These are used for GitLab CI/CD. Store it into GitLab CI/CD variables. So, save it!

Important

Replace the public IP address value in SSH_KNOWN_HOSTS with your actual hostname. This will make the deployment smoother by just looking the actual hostname/domain. You must mapped the public IP address into DNS record first. Otherwise, you will get error message in CI/CD: Host key verification failed in the future.

  1. Prepare a Laravel project. Then, install the deployer tool and create initial deploy.php file.
composer require deployer/deployer --dev
vendor/bin/dep init -n

If you're using Windows (not WSL) + Laragon, then use this command to create initial deploy.php file.

.vendor\bin\dep.bat init -n
  1. Replace the content of initial deploy.php with the specific Laravel deployment setup deploy.php in this repository.

Important

Replace the laravel.senku.stream with your actual hostname.

  1. Create .gitlab-ci.yml file and use the content of .gitlab-ci.yml in this repository. Then hit deploy!