Prep DigitalOcean for Laravel
With your Laravel application up and running locally, you now need to deploy it on your production server.
Before deploying, though, you need to do a few updates on your DigitalOcean server so it's set up with the necessary components that Laravel needs to run.
In these notes, we'll do the following:
- Enable a swap file for more memory
- Install Composer
- Install some necessary modules and enable
Tip: This note set includes some complex command line commands, each prefixed with a brief explanation of what the command does. To break down any one particular command to learn more, paste the command into https://explainshell.com.
All of the following commands are to be run on your DigitalOcean server, so the first thing you want to do is SSH into your DigitalOcean server (
ssh email@example.com), then proceed...
Enable a swap file for more memory
Composer can be memory intensive, so we'll want to configure our low-memory DigitalOcean droplets to use a swap file.
"Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM." -ref
To configure a swap file, run through the following commands.
First, create the swap file:
$ sudo fallocate -l 4G /swapfile
Next, adjust permissions on the resulting swap file so it isn't readable by anyone besides root:
$ sudo chmod 600 /swapfile
Next, tell the system to set up the swap space:
$ sudo mkswap /swapfile
Next, enable the swap space:
$ sudo swapon /swapfile
Finally, we want to make it so the server always enables this swap space, even after a reboot. To do this, open
/etc/fstab with nano:
$ sudo nano /etc/fstab
...and paste in the following line at the end:
/swapfile none swap sw 0 0
Save your changes (
y, then Enter).
Confirm it worked: You can confirm your swap file with the following command:
$ sudo swapon -s
Filename Type Size Used Priority /swapfile file 4194300 0 -2
Move into your bin directory where you'll install Composer:
$ cd /usr/local/bin
Use cURL to download the Composer installer and run it with PHP:
$ curl -sS https://getcomposer.org/installer | sudo php
Rename the resulting
composer.phar executable to
composer so it's convenient to invoke:
$ sudo mv composer.phar composer
Test it's working:
See a list of Composer commands? Good, you're ready to move to the next step...
Install/update necessary modules
Next, there are a few modules we need to install on the server in order for Laravel to work.
To do this, we'll use
apt-get, a command line utility for managing packages on Ubuntu systems (which our Droplets run on).
Run the following commands, one at a time. Follow the instructions to hit
Y (yes) when prompted.
First, specify a new repository
apt-get can download software packages from. The repository we're adding is ppa:ondrej/php, the primary source for PHP-related packages.
$ sudo add-apt-repository ppa:ondrej/php
apt-get to get the latest packages from your apt-get repositories (including your newly added one,
$ sudo apt-get update
Now that everything is updated, we'll install the necessary modules:
$ sudo apt-get install php7.2-mbstring zip unzip
Next, Laravel requires Apache's
mod_rewrite for URL routing (a topic we'll cover in future lectures). To enable this module, run the following command:
$ sudo a2enmod rewrite
Finally, to make all these changes take effect, restart Apache:
$ service apache2 restart
Server setup complete!
At this point, you DigitalOcean Droplet has everything it needs to run a Laravel app. You're ready to move on to the next steps of deploying.