In recent years, we have built several applications using the Laravel framework. Although we are really happy with the framework, hosting of those applications has always been a bit of a problem. We are programmers, not system administrators and we don’t have enough experience to setup and maintain our own server. Therefore, we always used a complete package offered by some local hosting company. Those packages work well for simple websites and in the beginning of our Laravel journey, they were fine as well. As webapplications are growing and our workflow improved, our demands for the hosting also growed and we ran into its limitations.
This was the moment we dived into Digital Ocean, Laravel Forge and Envoyer. As a first step, we watched the Forge Laracast and Envoyer Laracast series. They are both excellent introductions for these tools. However, since we had little knowledge about this subject before we started our journey, we needed a little bit more information on some points. We did some additional research and moved one of our applications to a Digitial Ocean server maintained by Forge. So far, it has been a very good experience and we plan to move all Laravel applications. As a reminder for ourselves, we wrote down the steps we took to make it work.
Laravel Forge supports several cloud servers out-of-the-box: Amazon webservices, Digital Ocean and Linode. Although all three options are suitable, we chose the cloud servers of Digital Ocean. It’s quick, cheap and has a lot of documentation on how to arrange things on your server for situations that are not managed by Forge.
Setting up a server will be done via Forge, but you need to have an account at Digital Ocean first. This is easy, just go to the website and sign up.
Laravel Forge is the tool to manage your servers. An extensive explanation how to work with Forge can be found in the corrensponding Laracasts series. The first step is to create an account. Next, we connect to Bitbucket or Github where our repository is hosted and we create a server via the Forge interface.
Now we have to wait a while because Forge is performing several tasks in the background. Many actions performed by Forge are authorized via ssh keys. To setup your ssh keys, go to the server you just created and switch to the tab ‘ssh keys’. Here you have to add the public key of the ssh key.
Now we have to add a new site. Switch to the tab ‘sites’ and fill in the domain. This can be something like example.com, or subdomain.example.com and choose the root directory of the project. For most Laravel applications, this will be ‘public’.
Domainnames are not managed by Laravel Forge. You can just buy a domainname somewhere online and adjust the DNS record by pointing the domainname to the ip of the newly created server.
When using Laravel Forge, a free ssl certificate can be added to the website using the LetsEncrypt option in the ‘SSL certificate tab’ of the site details page.
SSH and SFTP access
Although changes in your application should never be done directly on your webserver, you sometimes need access to the server.
SSH is a common way to login to the server. When using Forge, the root user is automatically renamed to ‘forge’. Logging in to the server is achieved using the following command:
When this is not successfull, add your ssh key to the setting page of your Digital Ocean account. The ip of your server can be found in Forge. The dashboard lists your active servers and their ip addresses, or your can go to your server detailpage where it’s displayed in the blue top bar
In rare situations you may want to use an application like Filezilla to access your server. A manual can be found here.
Keep in mind that you have to add a private key here. Shortly, the following steps should be taken:
Go to Filezilla -> settings and choose SFTP. Now add a private key by pressing the following keyboard combination:
command + shilft + .
The private key that is generated is not in the correct format for filezilla. Filezilla may convert this key. Fill in a passphrase and save the key with a descriptive name. Don’t forget to push the ‘OK’ button.
Now go to the site manager and add a new entry.
- In the field ‘host’, enter the ip of the server
- Choose SFTP as protocol
- For Logon type, choose the ‘interactive’ option
- In the user field, fill in ‘forge’
- Leave the password field empty
You should be able to connect to the webserver now.
We were used to work with phpmyadmin, but this not installed by default. Luckily, there are other and better alternatives. We chose SequelPro, which is able to connect to local databases as well as remote ones.
When creating a new server with Forge, one database is created by default. Its credentials are, together with some other details of your server, send to you by email.
Setting up Sequel Pro
Sequel Pro has 3 types of connections. The socket can be used for your local projects. The SSH option should be used to connect to a remote database.
In the name field, give your connection a descriptive name. Add the ip address of your server to the MySQL host field and fill in the username ‘forge’. The password you need is supplied in the ‘credentials email’ your received when setting up a new server.
The ‘database’ and ‘port’ fields can be left empty. Now you have to fill in the SSH details. The SSH host is again the ip address of the server, the SSH user is ‘forge’. The SSH key should be added by clicking the ‘key’ icon and navigating to the corresponding file on your computer. Use port 22 as SSH port.
Now you should be able to connect to your remote database.
Although Forge can be used for deployment of your code, Envoyer is more suitable for this job since it has a zero-downtime setup and the ability to deploy to multiple servers. How to set up Envoyer in combination with Forge, will be discussed in our next post.
As a very quick reminder, a list of tools and what they are used for:
- Digital Ocean: here we rent a server
- Laravel Forge: tool to manage the server
- Domainnames: buy them online at a company which allow you to manage the DNS records
- SSL: Freely available via Laravel Forge (Let’s Encrypt)
- SequelPro: tool to manage the database
- Deployment: Envoyer (next post)