DEV Community

Cover image for Deploying Laravel App to Ubuntu or Debian Server VM Azure
Youssef Ahmed
Youssef Ahmed

Posted on

Deploying Laravel App to Ubuntu or Debian Server VM Azure

Cover Image

Deploying a Laravel application doesn't have to cost money. If you already have a VPS or an Azure Virtual Machine, you can deploy your Laravel project manually without paying for services like Laravel Forge or Laravel Cloud.

In this guide, I'll show you how to deploy a Laravel application step by step using Nginx, PHP, MySQL, and Composer on an Ubuntu VM. I'll also explain what each command does so you understand what's happening behind the scenes.

Don't have an Azure Virtual Machine yet?

Watch this video first:

https://youtu.be/Io34SbaYZmQ?si=a4btxewV0nFo0x_N


Prerequisites

Before getting started, make sure you have:

  • An Ubuntu or Debian server (this guide uses Ubuntu on Azure)
  • A GitHub repository containing your Laravel project
  • SSH access (or password access) to your server
  • Root or sudo privileges
  • A public IP address assigned to your VM

1. Update the Server

Before installing any packages, update your package index.

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Optionally, upgrade installed packages.

sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

2. Install Nginx

Nginx is a high-performance web server used to serve websites efficiently. It can also act as a reverse proxy and load balancer.

Think of Nginx as a highly efficient post office. It receives incoming requests, processes them, and forwards them to the correct destination.

Install Nginx:

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

Verify that it is running:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

3. Install MySQL

Laravel needs a database to store application data.

Install MySQL Server:

sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

sudo systemctl status mysql
Enter fullscreen mode Exit fullscreen mode

4. Install PHP

Nginx doesn't execute PHP files directly. Instead, it communicates with PHP-FPM (FastCGI Process Manager), which processes PHP requests.

Install PHP and the required extensions:

sudo apt install -y \
php \
php-fpm \
php-cli \
php-common \
php-curl \
php-mbstring \
php-xml \
php-soap \
php-gd \
php-bcmath \
php-zip \
php-mysql
Enter fullscreen mode Exit fullscreen mode

Note

If you're using a specific PHP version (for example PHP 8.4), install the matching packages instead.

Check your PHP version:

php -v
Enter fullscreen mode Exit fullscreen mode

5. Install Composer

Composer is the dependency manager for PHP.

Download and install Composer:

curl -sS https://getcomposer.org/installer | php

sudo mv composer.phar /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

composer --version
Enter fullscreen mode Exit fullscreen mode

6. Install Node.js & NPM

Laravel uses Node.js for compiling frontend assets with Vite.

Install Node.js and NPM:

sudo apt install -y nodejs npm
Enter fullscreen mode Exit fullscreen mode

7. Clone Your GitHub Repository

Move to the web directory:

cd /var/www/html
Enter fullscreen mode Exit fullscreen mode

Clone your project:

git clone YOUR_GITHUB_SSH_URL learn-vm
Enter fullscreen mode Exit fullscreen mode

Move into the project directory:

cd learn-vm
Enter fullscreen mode Exit fullscreen mode

8. Install Project Dependencies

Install Composer packages:

composer install --no-dev --optimize-autoloader
Enter fullscreen mode Exit fullscreen mode

Install Node packages:

npm install
Enter fullscreen mode Exit fullscreen mode

Build frontend assets if use front:

npm run build
Enter fullscreen mode Exit fullscreen mode

9. Configure Environment Variables

Copy the environment file:

cp .env.example .env
Enter fullscreen mode Exit fullscreen mode

Generate the application key:

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

10. Create the MySQL Database

Open MySQL:

sudo mysql
Enter fullscreen mode Exit fullscreen mode

Run the following SQL:

CREATE DATABASE learnvm;

CREATE USER 'learnuser'@'localhost'
IDENTIFIED BY 'YOUR_PASSWORD';

GRANT ALL PRIVILEGES ON learnvm.* TO 'learnuser'@'localhost';

FLUSH PRIVILEGES;

EXIT;
Enter fullscreen mode Exit fullscreen mode

11. Configure Laravel

Open the .env file:

nano .env
Enter fullscreen mode Exit fullscreen mode

Update your database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=learnvm
DB_USERNAME=learnuser
DB_PASSWORD=YOUR_PASSWORD
Enter fullscreen mode Exit fullscreen mode

12. Run Database Migrations

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

If your project includes seeders:

php artisan db:seed
Enter fullscreen mode Exit fullscreen mode

13. Set File Permissions

Laravel needs write access to the storage and bootstrap/cache directories.

sudo chown -R www-data:www-data storage bootstrap/cache

sudo chmod -R 775 storage bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

14. Configure Nginx

Create a new virtual host configuration:

sudo nano /etc/nginx/sites-available/learn-vm
Enter fullscreen mode Exit fullscreen mode

Paste the following configuration:

server {
    listen 80;
    server_name YOUR_VM_PUBLIC_IP;

    root /var/www/html/learn-vm/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:/run/php/php8.3-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_VM_PUBLIC_IP with your server's public IP address.


15. Enable the Site

sudo ln -s /etc/nginx/sites-available/learn-vm /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

16. Stop Apache (If Installed)

If Apache is already using port 80, stop it first.

sudo systemctl stop apache2
Enter fullscreen mode Exit fullscreen mode

17. Test the Nginx Configuration

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

If everything looks good, reload Nginx:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Restart PHP-FPM:

sudo systemctl restart php8.3-fpm
Enter fullscreen mode Exit fullscreen mode

18. Open Your Application

Visit your server's public IP address in your browser:

http://YOUR_VM_PUBLIC_IP
Enter fullscreen mode Exit fullscreen mode

Your Laravel application should now be running successfully.


Azure Networking

If your application isn't accessible, make sure the required ports are open.

Go to:

Azure Portal → Virtual Machine → Networking → Add Inbound Port Rule

Allow the following ports:

  • HTTP (80)
  • HTTPS (443)

After saving the rules, refresh your browser.

Azure Networking


Troubleshooting

If your application doesn't load, check the following:

  • Nginx is running.
  • PHP-FPM is running.
  • MySQL is running.
  • Port 80 is open.
  • Port 443 is open (if using HTTPS).
  • Laravel has the correct file permissions.
  • Your .env file contains the correct database credentials.
  • The Nginx configuration points to the correct public directory.
  • Run php artisan config:cache after updating your .env file.

Next Steps

Congratulations! Your Laravel application is now deployed successfully.

To make your server production ready, consider adding:

  • Add a custom domain name and point its DNS records to your server's public IP address.

Resources

Live Demo

http://51.107.72.82/

All Commands

https://user-cdn.hackclub-assets.com/019f68cc-736b-7627-b4cf-82f958d84b93/README.md


If this guide helped you, consider leaving a ❤️ on DEV and sharing it with other Laravel developers. Happy coding!!

From the River to the Sea, Palestine Will Be Free!🇵🇸

Top comments (0)