How to Deploy a Laravel Project on DigitalOcean – Step-by-Step Guide
So, you’ve built an amazing Laravel project, and now it’s time to deploy it online? DigitalOcean is a fantastic choice for hosting, offering flexibility, speed, and affordability. Whether you’re a beginner or an experienced developer, this guide will walk you through deploying your Laravel application on DigitalOcean in a simple and efficient way.
Let’s get started! 🚀
Step 1: Set Up a DigitalOcean Droplet
A Droplet in DigitalOcean is a virtual private server (VPS) where your Laravel project will run.
1.1 Create a DigitalOcean Account
- Go to DigitalOcean and sign up.
- Verify your email and set up your payment details.
1.2 Create a New Droplet
- Click on “Create” → “Droplets.”
- Choose Ubuntu (latest version) as your operating system.
- Select a plan (Basic $5/month is enough for small projects).
- Add your SSH key (recommended) or use a password for authentication.
- Click “Create Droplet.”
- Once created, note down the IP address of your server.
Step 2: Connect to Your Droplet
2.1 Using SSH to Access the Server
If you’re on Windows, use PuTTY or Git Bash. On macOS/Linux, use the terminal. Run:
ssh root@your_server_ip
Replace your_server_ip
with the actual Droplet IP. Enter your password (if not using an SSH key).
Step 3: Install Required Packages
Before deploying Laravel, install the necessary software.
apt update && apt upgrade -y
apt install nginx mysql-server php-cli php-mbstring php-xml php-bcmath php-curl unzip git -y
3.1 Install Composer
Laravel requires Composer to manage dependencies.
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Step 4: Set Up MySQL Database
Login to MySQL:
mysql -u root -p
Create a new database and user:
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 5: Upload Laravel Project to the Server
There are two ways to upload your Laravel project:
5.1 Using Git (Recommended)
If your project is in a Git repository:
cd /var/www
git clone https://github.com/your-repo.git laravel_project
5.2 Using SFTP
If you prefer manual upload, use an FTP client like FileZilla to transfer files.
Step 6: Configure Laravel
6.1 Set Permissions
Navigate to your Laravel directory:
cd /var/www/laravel_project
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache
6.2 Set Up Environment Variables
Rename the .env.example
file:
cp .env.example .env
Edit database details in .env
:
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_password
Run migrations:
php artisan migrate
Generate application key:
php artisan key:generate
Step 7: Configure Nginx for Laravel
Create a new Nginx configuration file:
nano /etc/nginx/sites-available/laravel
Add the following configuration:
server {
listen 80;
server_name your_domain_or_IP;
root /var/www/laravel_project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Save and exit (CTRL+X
, then Y
, then Enter
).
Enable the configuration:
ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
Step 8: Set Up Supervisor for Queue Workers (Optional)
If your Laravel project uses queues, install Supervisor:
apt install supervisor -y
Create a new Supervisor configuration:
nano /etc/supervisor/conf.d/laravel-worker.conf
Add:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel_project/artisan queue:work
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log
Save, then restart Supervisor:
supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker:*
Step 9: Set Up SSL with Let’s Encrypt (Recommended for HTTPS)
Install Certbot:
apt install certbot python3-certbot-nginx -y
Generate and install an SSL certificate:
certbot --nginx -d your_domain
Renew SSL automatically:
certbot renew --dry-run
Step 10: Testing and Finalizing Deployment
Restart services:
systemctl restart nginx
systemctl restart php-fpm
Visit your server IP or domain in a browser, and your Laravel project should be live! 🎉
Deploying a Laravel project on DigitalOcean might seem complex at first, but by following these steps, you’ll have your application running smoothly in no time. Whether you’re hosting a personal project or a business application, DigitalOcean provides a scalable and cost-effective solution.
If you need professional help with website development or deployment, CodeHunger is a great choice. Their expert team can assist in launching high-performance websites tailored to your needs.
Happy coding! 🚀