Deploy Laravel app to DigitalOcean
The following steps outline the procedure for deploying a new Laravel application to our DigitalOcean Droplets:
- On your server, clone your Laravel app from Github
- Build the
composer install --no-dev
- Set permissions
- Set up
- Configure subdomain
Clone your Laravel app
While SSH'd into your Droplet, navigate into your document root at
$ cd /var/www/html
Now use Git to clone your project from Github. Edit as necessary with your username and app name.
$ git clone firstname.lastname@example.org:username/foobooks.git
Navigate into the resulting directory (in this case, it's
foobooks) and use a list command to confirm all your files are there.
Build vendor/ directory
If you compare the contents of your local application files to your production application files on your Droplet, you'll notice the Droplet version is missing a
This is because vendors are managed by Composer and are not version controlled. This is configured via
.gitignore which lists
vendor/ as a directory to ignore:
Given this, you need to have Composer build your vendor's directory with this command:
$ composer install --no-dev
- Running this command may take a few minutes
--no-devflag is added to indicate we only want packages intended for use on production; it will exclude packages used for local development/testing.
- Don't worry about the lines regarding suggested packages you should install; we can always add the suggested components later as needed.
Example output from the above command:
root@lamp-512mb-nyc3-01:/var/www/html/foobooks# composer install --no-dev Do not run Composer as root/super user! See https://getcomposer.org/root for details Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Package operations: 68 installs, 0 updates, 0 removals - Installing doctrine/inflector (v1.2.0): Downloading (100%) [...ADDITIONAL INSTALLING LINES REDACTED FOR BREVITY...] symfony/var-dumper suggests installing ext-symfony_debug () [...ADDITIONAL SUGGESTION LINES REDACTED FOR BREVITY...] Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Package manifest generated successfully.
When Composer is complete, if you list the contents of your project you should now see a
As discussed when we were setting up Laravel on your local server, Laravel/the Apache web server needs write access to the
This was a step we could skip locally because of how your local servers are configured, but it can't be skipped on your live servers. On your DigitalOcean server we need to make sure Apache has write access to these folders.
To do this, we first identified that on DigitalOcean servers, Apache runs under a user called
Given that, make the user
www-data own the
storage directory and everything in it (
$ chown -R www-data storage
And now make the same change for the
$ chown -R www-data bootstrap/cache
Set up .env file on production
vendors/ folder, the
.env file is also listed in
.gitignore so it's also excluded from version control. Because of this, you need to manually create a
.env file on your live server in order for your application to work there.
This can be done by copying the provided
.env.example file to
$ cp .env.example .env
Next, you need to generate a app key:
$ php artisan key:generate
(If you're curious, you can
cat .env to see the new key that was generated)
Later we'll discuss environments in full details and explain what exactly
.env is doing and how it works. For now, just know that we need that
.env file in order for Laravel to work.
To access your Laravel application from the web, you'll want to set up a subdomain that points to it. For this you will follow the same procedure you did to create
Continuing with the foobooks example, I set up
Summary of steps:
- Add a new VirtualHost block to
/etc/apache2/sites-enabled/000-default.conf(see notes below)
- Restart Apache with the command
service apache2 restart
Reminder when creating your VirtualHost block that the document root for your Laravel projects should point to the
public directory within your project.
<VirtualHost *:80> ServerName foobooks.dwa15.me DocumentRoot "/var/www/html/foobooks/public" <Directory "/var/www/html/foobooks/public"> AllowOverride All </Directory> </VirtualHost>
Test it out
After setting up your subdomain you should now be able to access your new app on your DigialOcean server:
The above steps were a one-time-process for deploying a new Laravel application.
Moving forward, your deployment process will look like this:
- From local
- SSH into your DigitalOcean droplet and navigate into your app folder, then run
git pull origin master.
- Also while SSH'd in to your app folder, run
composer install --no-dev.
It's important to always run
composer install --no-dev when deploying code changes. This command will make sure your code base has the latest versions of all dependencies and it will also refresh your application's classmap with the latest details.
Issue: Composer memory issue
You attempt to run a Composer command but get an error about
lack of memory or swap, or not having swap configured and/or
Cannot allocate memory.
To fix, follow the instructions for setting up a swap file in the note set Prep DigitalOcean for Laravel.
- Lack of a
- Forgetting to build the
- Forgetting to set the necessary write permissions on