Deploy a Laravel App on a Laravel Forge Server using a Digital Ocean Droplet and a domain name
The accounts you need:
- Create a Laravel Forge account, add a credit card and choose which plan you want. If you choose the first plan, only one server can be created. Checkout my article about deploying multiple Laravel Apps on one Laravel Forge Server.
- Use this link https://m.do.co/c/1c27867c48f0 to create an account on Digital Ocean and get $100 in credit over 60 days!
Add Digital Ocean as Server Provider on Laravel Forge:
DIGITAL OCEAN Part:
- Go to Digital Ocean
- Under the tab Account select API
- Under Tokens/Keys click “Generate New Token”
- Fill in the name of your Token eg. <Forge server App 01>
- Read and Write permissions are already select by default
- Go ahead and click “Generate Token”.
- Next you’ll see a token, copy it.
LARAVEL FORGE Part:
- Go to Account (Click on your name on the right)
- Go to Server Providers
- Choose “Digital Ocean” as provider
- Fill in the email address you used for your digital ocean account
- Paste the Token you copied.
- Click ADD CREDENTIAL
- Digital Ocean is now added to the Active providers.
Creating the Server on Laravel Forge:
- Click on “FORGE” in the header
- Click on the square with “Digital Ocean”
- Most of the credentials are already filled in:
- check your email address
- check the name you want to give your server, leave it if you like it, it should be unique
- check your region
- server size: I start with the first 1GB option, that’s the 5 dollar Droplet, you can always choose a bigger Droplet when you need to
- choose your PHP version
- Post Provision can be ‘none’
- choose your mysql version
- give a NAME to your Database
- leave the Load Balancer unchecked
- leave the Weekly Backups unchecked (unless you want to pay for it)
4. Click CREATE SERVER
5. Copy and save the Sudo Password and the Database Password to a secure place, with the name of your server ( especially if you are planning on adding multiple servers)
6. After a while (+-10 min) your server will be ready, and the status will change from ‘building’ to ‘provisioning’ to ‘connected’.
Once your server is connected:
- Click on the name of your server or click on Servers and select your server.
- Go to Sites > Active Sites and select the ‘default’ site
- Under Site Details, select Meta
- Fill in your domain name and click UPDATE
- You’ll see your domain name, between the name of your server and your ip address
On Digital Ocean, connect your (purchased) domain name to your newly created site/server (refresh Digital Ocean, if you were still logged in, then you can find your Droplet with the name of your newly created server under Projects > resources)
- Go to Networking > Domains
- Enter your purchased domain and select your Project (if you have multiple otherwise leave the default)
- Click Add Domain
Next, we’ll redirect our purchased domain to use the DigitalOcean nameservers , there are three.
Add the first nameserver:
- Under Domains, click on your newly added domain
- Under Create new record select the option “NS”
- Add an “@“ or your domain name in the HOSTNAME field
- In the WILL REDIRECT FIELD add “ns1.digitalocean.com.”
- Leave the TTL default
Add the second nameserver:
- Click on Create Record
- Under Create new record select the option “NS”
- Add an “@“ or your domain name in the HOSTNAME field
- In the WILL REDIRECT FIELD add “ns2.digitalocean.com.”
- Leave the TTL default
Add the third nameserver:
- Click on Create Record
- Under Create new record select the option “NS”
- Add an “@“ or your domain name in the HOSTNAME field
- In the WILL REDIRECT FIELD add “ns3.digitalocean.com.”
- Leave the TTL default
- Click on Create Record
Next, we’ll add an A record to redirect our domain name to our server/site:
- Under Create new record select the option “A”
- Add an “@“ or your domain name in the HOSTNAME field
- In the WILL REDIRECT FIELD select your Server/Droplet from the list
- Leave the TTL default
- Click on Create Record
Then we’ll add a CNAME record to redirect www.yourdomain.com to our server/site:
- Under Create new record select the option “CNAME”
- Add “www” or <www.yourdomainname.com> in the HOSTNAME field
- In the IS AN ALIAS OF field enter an @ or <yourdomainname.com>
- Leave the TTL default
- Click on Create Record
Now head over to Laravel Forge, we are ready to add our Laravel App to our Site.
- Under Sites, select the name of your Site and head over to the Apps tab.
- Click on Git Repository ( Go to Account > Source Control > CONNECT TO GITHUB and follow the steps to connect your GitHub account to Laravel Forge)
- Install Repository
- Select Github
- Fill in your repository details
- Choose your default branch, which is “main” on Github since a few weeks.
- Choose to Install Composer Dependencies
- Click Install Repository
- Now wait until all is installed
Head over to ENVIRONMENT under Site Details.
Fill in the details about your Laravel app in the .env file.
- database, database user and password (you can create a new database under Servers > Database)
- email server
- APP_URL -> http://yourdomainname.com
- + all relevant .env settings for your app
Click SAVE and go to Deployments and click Deploy Now and once the status is Finished click on the green arrow on the right site (at the top) to open up your App.
Optional: Create an SSL certificate with Let’s Encrypt.
- Go to SSL under Site Details
- Click on the box with LetsEncrypt
- Fill in your domain
- Click OBTAIN CERTIFICATE
Sometimes it takes some after the site setup before you can create a certificate!
If the status is installed and active is active, go back to your website and refresh, you’ll now see the lock in your browser.
Thanks for reading, please leave a comment and happy coding!
Kind regards,
Lotje Kinable