Deploy Laravel Project in Digital Ocean Droplet
Are you ready to take your Laravel project to the next level? Deploying it on a Digital Ocean droplet could be the perfect solution. Digital Ocean is a cloud infrastructure provider that offers fast, reliable, and scalable virtual machines known as droplets. Whether you're launching a personal portfolio, a business website, or a full-fledged web application, hosting your Laravel project on a droplet will give you the flexibility and control you need.
In this step-by-step guide, we'll walk you through the entire process of deploying your Laravel project on a Digital Ocean droplet. From setting up your droplet and configuring the server environment to deploying your code and securing the application, we've got you covered. Even if you're new to server management and deployment, this guide will simplify the process and help you get your project up and running smoothly.
Create a Droplet.
- Choose Image / Use Marketplace to search for LEMP Image
- Choose Server
- Choose Region
- Choose Monitoring (Optional)
- Authentication
- SSH or Password
- Change/Create Hostname
Login with IP with SSH
Create new User
- Create Password
- Other Information (Optional)
Add Permission to User
Use New User to Login
Add SSH to this server
- Create directory
- Get and Copy-Paste Computer SSH Key from Local to Remote server
Setting Up Basic Firewall
We need to make sure that the firewall allows SSH connections so that we can log back in next time. We can allow these connections by typing:
Install NGINX
After reboot you'll be logged out from the terminal session. Log back in in few minutes and install NGINX.
If you have the ufw firewall enabled, as recommended in our initial server setup guide, you will need to allow connections to Nginx. Nginx registers a few different UFW application profiles upon installation. To check which UFW profiles are available, run:
It is recommended that you enable the most restrictive profile that will still allow the traffic you need. Since you haven’t configured SSL for your server in this guide, you will only need to allow regular HTTP traffic on port 80.
Enable this by typing:
You can verify the change by running:
This command’s output will show that HTTP traffic is now allowed:
if Inactive
With the new firewall rule added, you can test if the server is up and running by accessing your server’s domain name or public IP address in your web browser.
Install MYSQL
Once you have Nginx installed, the next step is to install MySQL. You can do this by running the following command:
Ensure that the server is running using the systemctl start command:
These commands will install MySQL, start the MySQL service, and enable it to start on boot. We also need to run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system.
Warning: Starting from July 2022, if you try to use the mysql_secure_installation script without making some changes, it will show an error. This happens because the script tries to set a password for the main MySQL account, but on the Ubuntu systems, this account isn't set up to use a password by default.
Before July 2022, the script would just skip setting the password and keep going. But now, it will show an error message after you try to set a password for the root account.
When we are at this stage, the script executes into a recursive loop which we can only get out of by closing your terminal window.
However, the mysql_secure_installation script does more than just setting passwords; it helps secure your MySQL installation. It's still a good idea to run it before using MySQL. To avoid the loop, you need to change how the root MySQL user is authenticated first.
First, open up the MySQL prompt:
While we are at the mysql session, we can create a new user and database for our Laravel application.
Create Database
Now, we can run the mysql_secure_installation script to address the security concerns that are raised by the script.
When we run the script, it will prompt us with some questions to adjust our MySQL security settings. The first one will be about setting up the Validate Password Component/Plugin. This plugin helps us in checking the strength of new MySQL user passwords before accepting them.
If we choose to set up this plugin, any new MySQL user we create with a password will need to have a password that meets the criteria we choose during setup.
After dealing with the Validate Password Plugin, the script will prompt us to set a password for the MySQL root user. We'll need to enter a secure password of your choice and then confirm it. This password will be used to access the root account, so make sure it's strong and memorable. If you have already set the password, which is acceptable by the Password Validation Plugin, we can skip this step.
From here, we can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.
Note: Once the security script completes, we can then reopen MySQL and change the root user’s authentication method back to the default, auth_socket. To authenticate as the root MySQL user using a password, run this command:
Then revert back in using the default authentication method using this command:
This will mean that we can once again connect to MySQL as our root user using the sudo mysql command.
Once the script completes, our MySQL installation will be secured. We can now move on to installing PHP and configuring Nginx to use it.
Install PHP and other required extensions
- Install required extensions
Setup Laravel Project and Configure nginx
- Clone project to var/www/ using git.
- Configuring nginx to Use the PHP Processor
- Configure NGINX configuration file
- Add the following configuration into your conf file
- Check if the configuration is correct
- Restart NGINX
- Check the status of NGINX
If everything is correct, you should see the status as active. Now you can proceed to the next step.
Install Composer and Laravel Project Packages
- Install Composer
- Install Composer Dependencies for the project
- Create .env file from .env.example and generate key
- Configure .env Environment file for Laravel
- Give permission to storage and bootstrap/cache folder
- Run your migration files
Install Certbot
- Install Certbot
Now Certbot is ready to use. We can obtain an SSL certificate for our domain by running the following command:
Running this command initiates Certbot with the --nginx plugin, responsible for managing Nginx's configuration and reloading it as needed. By including the -d flags, you specify the domain names for certificate acquisition.
For first-time users, Certbot prompts you to provide an email address and agree to the terms of service. Upon completion, Certbot communicates with the Let's Encrypt server, executing a challenge to confirm domain ownership before issuing the certificate.
Install Supervisor (Optional)
- Create a new configuration file for the queue worker inside the /etc/supervisor/conf.d directory.
- The file can be named after your application, for example, queue-worker.conf.
- Add the following configuration to the file.
- After creating the file, you can tell Supervisor to read the new configuration file and start the queue worker by typing:
Conclusion
So, there you have it! We've covered the entire process of deploying a Laravel project on a Digital Ocean droplet. From creating and configuring our droplet to deploying our code and securing your application, we've walked through each step in detail. By following this guide, you'll gain a solid understanding of the entire process and be well-equipped to host your Laravel project on a Digital Ocean droplet.
Ready to host your Laravel app on DigitalOcean? Sign up now using my referral link and get $200 in credit over 60 days! Start hosting hassle-free today: Referral Link