How to Deploy a Laravel Application
This guide shows how to deploy a Laravel PHP application on an Ubuntu server with PHP-FPM, Composer and Nginx.
1. Install PHP and required extensions
sudo apt update
sudo apt install -y php php-fpm php-cli php-mbstring php-xml php-bcmath php-curl php-mysql php-zip unzip git2. Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer3. Get your application
Clone your project (or create a fresh app) into the web root:
cd /var/www
sudo git clone https://github.com/your/repo.git example.com
cd example.com
composer install --no-dev --optimize-autoloader4. Configure the environment
cp .env.example .env
php artisan key:generate
php artisan migrate --force5. Set permissions
Laravel needs write access to storage and bootstrap/cache:
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 775 /var/www/example.com/storage /var/www/example.com/bootstrap/cache6. Nginx server block
server {
listen 80;
server_name example.com;
root /var/www/example.com/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
}7. Enable and reload
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxYour Laravel app is now live. Remember to set APP_ENV=production and APP_DEBUG=false in .env, and cache config with php artisan config:cache.