Rapid Deployment to DigitalOcean
Although you can deploy a Laravel application hundreds of different ways, I wanted to share a quick way to deploy a Laravel app in DigitalOcean to get you started.
- You have a Laravel application that you’re trying to deploy.
- You have a Digital Ocean account. If you don’t, it’s really simple to sign up.
- Your Laravel Application is on GitHub or Bitbucket.
Log into your Digital Ocean account and click on the Create -> Droplets button.
You will be brought to a screen that looks like the one below. The default Distributions tab will be selected, but we don’t want to spend that much time on optimizing our environment. We want something quick and already optimized for us.
Select the Marketplace tab and search for Laravel.
The latest version of Laravel will be displayed. Select it.
Choose the Basic plan.
Click on the Regular with SSD option and select the $5/mo option.
Skip the portion that states that you should add block storage.
Since I’m on the East Coast of the United States, I’ve selected the New York datacenter. Select the one that’s closest to you.
Nothing needs to be changed with the VPC Network.
Specify a password that you’re going to use to access your Droplet.
I encourage you to enable backups if you’re planning on keeping this application running.
You can leave all other settings alone and click on the Create Droplet button.
Be patient while the Droplet is created.
Once it’s finished, you can click on the droplet to enter the droplet screen. If you see the screen like the one below, you’re in.
Time to test your droplet. Click on the ipv4 address to copy the address. Paste it into your browser’s address bar. You should see the Laravel welcome screen.
Now that it’s working, it’s time to push your application to the droplet.
Pushing Your Laravel App to the Droplet
At this point, I’m assuming that your application has been pushed to GitHub or an alternative like Bitbucket.
On the top right of the droplet screen, you’ll see a link to the Console. Click on it to open it up.
Since this is your first time launching the application, you’ll need to enter the domain/subdomain credentials. We won’t go through that in this article. You can hit CTRL+C to exit the application or follow the prompts.
Your application is hosted in ../var/www/laravel/. If you want to explore that folder, you can cd into it: cd ../var/www/laravel/. We’re going to go a directory above and delete the laravel folder since we’re going to copy our Laravel application into it: cd ../var/www/.
Execute the command rm -r laravel/ to remove the laravel folder and all of its subfolders. You can verify that it’s removed by typing in ls. You should only see the html folder.
It’s time to clone the repository into your application. To clone the repo, go to your GitHub account, click on the Code dropdown button, and select the HTTPS option.
Go back to your console and type in:
git clone https://github.com/your-user-name/your-project.git laravel
Make sure to add the laravel folder at the end since we’re going to replicate the same structure as before.
If you go back to your Laravel application in your browser, you’ll see that it’s not working. That’s because we have to set it up.
Execute the following commands:
cp .env.example .env
This is where you might run into some issues if you’re not running the latest version of Laravel already. So you may have to start doing some upgrades. For my application, since it was on version 7, I had to do the following:
- sudo apt-get update
- sudo apt install php-xml
- sudo apt-get install php-mbstring
- composer update
- composer install
I hope that you don’t have to, but it’s just part of any deployment (unless you do it with Docker or you have the latest version of Laravel already running locally, but one article at a time).
php artisan key:generate
php artisan storage:link
php artisan migrate:fresh — seed
That should be it. You should be able to go to your application and see it working. But, it’s never that simple, and you are always running on a different version, aren’t you?
Potential error time
If you’re executing the migrate:fresh command, you might get the following error.
Illuminate\Database\QueryException could not find driver (SQL: SHOW FULL TABLES WHERE table_type = ‘BASE TABLE’)
To fix it, execute the following command:
- apt install php-mysql
You’ll most likely get a new error once you try to run the migrate:fresh command, such as:
Illuminate\Database\QueryException SQLSTATE[HY000]  Access denied for user ‘root’@’localhost’ (using password: YES) (SQL: SHOW FULL TABLES WHERE table_type = ‘BASE TABLE’)
It’s time to set a username/password in your .env file. In the console, type:
Find the DB USERNAME and PASSWORD fields. Make something up and remember them. I chose sail for username and password for password. Also choose the DATABASE name since we’ll most likely need that. Hit CTRL+X to Save and then exit. Let’s create a user with those credentials.
mysql -u root
CREATE USER ‘sail’@’localhost’ IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON *.* TO ‘sail’@’localhost’;
CREATE database your_database_name;
service mysql restart
Try the migrate:fresh command again. It should now work.
You may get an additional error if you go back to your application:
The stream or file “/var/www/laravel/storage/logs/laravel.log” could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file “/var/www/laravel/storage/logs/laravel.log”
To solve that error, execute the following commands:
- chmod -R 775 storage bootstrap/cache
- sudo chown -R $USER:www-data storage
- sudo chown -R $USER:www-data bootstrap/cache
That’s it. Refresh your page and hopefully you see the application that you created.
I hope that this will assist you in deploying your application quickly. In a later article, I’ll go over how to link your domain name to your application.