How to Host Laravel 10 on Digital Ocean

PHP Apr 2, 2023

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.

digital_ocean_start_page_using_referral_link

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

digital_ocean_signup

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.

digital_ocean_new_project_menu_item

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.

digital_ocean_droplets_menu_item

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.

digital_ocean_selecting_region_and_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.

digital_ocean_selecting_our_droplet_size

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.

digital_ocean_setting_up_authentication_method

Wait until the Droplet is created.

digital_ocean_progress_bar_of_our_droplet

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

digital_ocean_selecting_our_droplet_ip

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.

digital_ocean_opening_our_new_laravel_application

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.

digital_ocean_showing_name_of_the_droplet

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

digital_ocean_showing_launch_console_buttons

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.

digital_ocean_connecting_to_the_droplet_using_the_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.

digital_ocean_navigating_to_our_laravel_project

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.

  1. cd ../
  2. rm -r laravel
digital_ocean_removing_our_existing_application

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.

digital_ocean_cloning_an_existing_application_in_the_console

Now that we copied our application we need to navigate back into our application and copy our environment variables.

  1. cd laravel
  2. cp .env.example .env
digital_ocean_creating_our_env_file_for_our_laravel_project

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.

digital_ocean_laravel_composer_install

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.

digital_ocean_laravel_composer_install_result

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.

digital_ocean_laravel_creating_new_user

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.

digital_ocean_laravel_opening_our_droplet_console_with_new_user

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.

digital_ocean_laravel_composer_install_with_new_user

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.

  1. cd ../
  2. sudo chown -R tijn laravel (change tijn to the name of your user)
  3. cd laravel/
digital_ocean_giving_new_user_permission_to_our_application

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

digital_ocean_laravel_composer_install_with_new_user

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.

digital_ocean_laravel_php_artisan_migrate_error

Let's fix this and execute nano .env to open up our environment variables. Inside our environment variables we want to change the following:

  1. DB_DATABASE=laravel
  2. DB_USERNAME=root
  3. DB_PASSWORD=

to

  1. DB_DATABASE=test_database (your database name)
  2. DB_USERNAME=user (your user)
  3. DB_PASSWORD=password (your password)
digital_ocean_laravel_configuring_my_sql_connection
You can navigate using the arrow keys, go down to DB_DATABASE=, then press either end or navigate to the right using the arrow keys to end up after the = symbol. Type the name of your database and use the del key to remove the old name. Do the same for the username and password. Make sure you use a secure password. I used password just for demonstration purposes. When you have made the changes press CTRL + x followed by y and enter to save and exit nano.

Now we need to configure MySQL

  1. mysql -u root
  2. CREATE USER "user"@"localhost" IDENTIFIED BY "password";
    (change user and password to your user and password and pay attention to the double quotes ").
  3. GRANT ALL PRIVILEGES ON *.* TO "user"@"localhost";
    (again change user to your user)
  4. FLUSH PRIVILEGES;
  5. CREATE DATABASE test_database;
    (change test_database to your preferred database name)
  6. exit
  7. service mysql restart
digital_ocean_laravel_executing_mysql_commands

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

digital_ocean_laravel_php_artisan_migrate

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:

  1. php artisan key:generate
  2. php artisan storage:link

And we have to add permissions to certain files

  1. chmod -R 775 storage bootstrap/cache
  2. sudo chown -R $USER:www-data storage
  3. sudo chown -R $USER:www-data bootstrap/cache

Or else we end up with these exceptions:

digital_ocean_laravel_permission_denied_error

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 put APP_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.

Tags