How to Host Laravel 10 on Digital Ocean
Hosting Laravel 10 on Digital Ocean is easy to set up and possible for only 4$ a month. This will be enough for any hobby project and the server can always be upgraded to handle production environments.
To follow along make sure that you have an account on Digital Ocean. If you do not have an account yet make sure you sign up using this link. This will give you free $200 credit for 60 days, enough to test it out.
1. Signing up for Digital Ocean
If you are using the signup link make sure that you click on the green notification bar.

This will make sure that you get the $200 free credit.

2. Creating your server
Now that you have an account, make sure you are logged in and start by creating a new project in your dashboard.

Once your project is created we can start creating a server. Servers in Digital Ocean are called Droplets. Let's create a Droplet by clicking either on Droplets in the menu or the Droplet image.

On the creation screen, you will be able to select a region. Click on the region that is either the closest to you or a region where most of your users will be.
For the image, we want to go to Marketplace, search for Laravel
and select the Laravel 10.0.0 on Ubuntu 22.04
image. As of writing, it was only possible for me to find a Laravel 10 image.

For the size, we want to have a Basic
Droplet type with a Regular
CPU and the cheapest option of $4
a month.
Remember this can always be upgraded, downgrading is not always possible. So even if you are not sure if it will be enough, start low and upgrade until it suits your needs.

For the Authentication method, I recommend you use SSH Key
. This is the most secure way, I recommend using the guide from Digital Ocean to set it up. If it is just a hobby project going with a password is fine.
Do not forget to enable Monitoring and change the hostname to something easily recognizable. After everything is set you can click Create.

Wait until the Droplet is created.

After the creation of the Droplet is finished we can copy the IP address.

If you paste this IP address into the browser you will see your example Laravel application hosted on Digital Ocean. This is accessible to everyone.

3. Accessing and configuring your server
Now that our Droplet is up and running let's configure our application. Before we do this we have to access our Droplet. The easiest way is to click on your project and then on the Droplet.

After opening the Droplet, you can click access, and then you will see the Launch Droplet Console button.

Click on the Launch Droplet Console button, if everything is configured correctly you will see the following console. If this is not the case you can use the Launch Recovery Console.

Inside the console, we see the path of our Laravel Application. Other than that you can see that console is prompting us to set up our domain name. We will not be setting up a domain name so press CTRL+C
twice to close the setup.
Now we can navigate to our application by typing cd /var/www/laravel
.
After that, you can type ll
to see the contents of your application.

Now you have 2 options you can either continue with this Laravel application or copy your own Laravel application into it. Remember that this Droplet is set up for Laravel 10 applications so make sure that the application supports it or else you might run into issues.
You can follow the upgrade guide from Laravel to upgrade your application. If you are keeping the current Laravel application continue to 3.2.
3.1 Copy your own application
In order to copy your own application we need to remove the current application.
cd ../
rm -r laravel

Now that the Laravel application is removed we can clone our application onto the server.
git clone https://[email protected]/yourproject.git laravel
git clone [email protected]:gitlab-tests/sample-project.git laravel
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY laravel
Depending on your git provider choose one of the above cloning options and change it accordingly. Make sure you add laravel
at the end because we want to have the same folder structure.

Now that we copied our application we need to navigate back into our application and copy our environment variables.
cd laravel
cp .env.example .env

3.2 Composer install
In the /var/www/laravel
folder we can execute our Laravel related commands. Let's start by executing composer install
to make sure that everything is installed.

When we execute composer install
you will see that we end up with a warning message. If it is a hobby project feel free to ignore it and enter yes
instead of no
. If you have entered yes
you can continue to 3.2.1 otherwise continue to 3.2.2.
3.2.1 Execute with root
If you have entered yes
you will see that composer install
ran successfully.

Continue to 3.3.
3.2.2 Execute with new user
If you have entered no
then we have to create a new user first. We can do this by executing sudo adduser tijn
, where tijn is the name of your new user.
You will be prompted for the password and additional information. Make sure you remember the password, the other information does not have to be filled in. Once everything is correct type y
and hit enter
.

Now we have to open a new console with your new user. Do not close the existing console with the root user. For this, you can go back to your Droplet and change Log in as
to your new user and launch the console.

In the new console, we want to navigate to our application again by executing cd /var/www/laravel
and execute composer install
. You can see that we are getting an Permission denied
error.

This means that we have to give our new user permission to the application. For this, we need to switch to our root console
(the first console we opened up) and execute the following commands.
cd ../
sudo chown -R tijn laravel
(change tijn to the name of your user)cd laravel/

Now if we execute composer install
again in our new user console, you will see that it will succeed now.

3.3 Configuring the database
Now that everything is installed we can continue with the database. This can be done in our root console.
If we execute php artisan migrate
to migrate the database you can see that we currently do not have access.

Let's fix this and execute nano .env
to open up our environment variables. Inside our environment variables we want to change the following:
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
to
DB_DATABASE=test_database
(your database name)DB_USERNAME=user
(your user)DB_PASSWORD=password
(your password)

You can navigate using the arrow keys, go down toDB_DATABASE=
, then press eitherend
or navigate to the right using the arrow keys to end up after the=
symbol. Type the name of your database and use thedel
key to remove the old name. Do the same for the username and password. Make sure you use a secure password. I usedpassword
just for demonstration purposes. When you have made the changes pressCTRL
+x
followed byy
andenter
to save and exitnano
.
Now we need to configure MySQL
mysql -u root
CREATE USER "user"@"localhost" IDENTIFIED BY "password";
(change user and password to your user and password and pay attention to the double quotes"
).GRANT ALL PRIVILEGES ON *.* TO "user"@"localhost";
(again change user to your user)FLUSH PRIVILEGES;
CREATE DATABASE test_database;
(change test_database to your preferred database name)exit
service mysql restart

Now if we execute php artisan migrate
again you will see that it works.

Now your application should be configured and ready unless you decided to clone over your application. Then you should continue with 3.4.
3.4 Finishing cloned application
To finish up your cloned application we need to execute the following commands:
php artisan key:generate
php artisan storage:link
And we have to add permissions to certain files
chmod -R 775 storage bootstrap/cache
sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
Or else we end up with these exceptions:

If you now visit your IP address again you will see your application.
It might be possible that you still have to seed your database, in that case, you can execute php artisan migrate:fresh --seed
.
Also if this is a production application make sure that you disable debug.nano .env
and putAPP_DEBUG
to false.
4. Conclusion
In this post, I showed you in detail how to set up a new or existing Laravel application in Digital Ocean. It might seem like a lot of work, but once you know what to do it can be done within 5 minutes. I hope this helped you set up your Laravel application. If you have any trouble feel free to reach out to me preferably on Linkedin.