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.
- 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 |
- Once droplet created, create
A recordby using the IP public of droplet into DNS. I'm using Cloudflare as DNS. We will use theNameof domain as a hostname in thedeploy.phpfile.
| 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.
- Put
setup.shfile 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/wwwdirectory - 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=yourdbpasswordTip
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.
- Prepare a Laravel project. Then, install the deployer tool and create initial
deploy.phpfile.
composer require deployer/deployer --dev
vendor/bin/dep init -nIf you're using Windows (not WSL) + Laragon, then use this command to create initial deploy.php file.
.vendor\bin\dep.bat init -n- Replace the content of initial
deploy.phpwith the specific Laravel deployment setup deploy.php in this repository.
Important
Replace the laravel.senku.stream with your actual hostname.
- Create
.gitlab-ci.ymlfile and use the content of .gitlab-ci.yml in this repository. Then hit deploy!