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:
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
Optionally, upgrade installed packages.
sudo apt upgrade -y
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
Verify that it is running:
sudo systemctl status nginx
3. Install MySQL
Laravel needs a database to store application data.
Install MySQL Server:
sudo apt install mysql-server -y
Verify the installation:
sudo systemctl status mysql
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
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
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
Verify the installation:
composer --version
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
7. Clone Your GitHub Repository
Move to the web directory:
cd /var/www/html
Clone your project:
git clone YOUR_GITHUB_SSH_URL learn-vm
Move into the project directory:
cd learn-vm
8. Install Project Dependencies
Install Composer packages:
composer install --no-dev --optimize-autoloader
Install Node packages:
npm install
Build frontend assets if use front:
npm run build
9. Configure Environment Variables
Copy the environment file:
cp .env.example .env
Generate the application key:
php artisan key:generate
10. Create the MySQL Database
Open MySQL:
sudo mysql
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;
11. Configure Laravel
Open the .env file:
nano .env
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
12. Run Database Migrations
php artisan migrate
If your project includes seeders:
php artisan db:seed
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
14. Configure Nginx
Create a new virtual host configuration:
sudo nano /etc/nginx/sites-available/learn-vm
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;
}
}
Replace
YOUR_VM_PUBLIC_IPwith your server's public IP address.
15. Enable the Site
sudo ln -s /etc/nginx/sites-available/learn-vm /etc/nginx/sites-enabled/
16. Stop Apache (If Installed)
If Apache is already using port 80, stop it first.
sudo systemctl stop apache2
17. Test the Nginx Configuration
sudo nginx -t
If everything looks good, reload Nginx:
sudo systemctl reload nginx
Restart PHP-FPM:
sudo systemctl restart php8.3-fpm
18. Open Your Application
Visit your server's public IP address in your browser:
http://YOUR_VM_PUBLIC_IP
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.
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
.envfile contains the correct database credentials. - The Nginx configuration points to the correct
publicdirectory. - Run
php artisan config:cacheafter updating your.envfile.
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
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)