
How to Install Laravel on Ubuntu Server
Installing Laravel on Ubuntu is a straightforward process that requires setting up a proper web server environment. This comprehensive guide will walk you through installing Laravel on a fresh Ubuntu server with all the necessary components including PHP, Nginx, MySQL, and SSL.
Prerequisites
Before you begin, ensure you have:
- A fresh Ubuntu 20.04 or 22.04 server installation li>SSH access with sudo privileges li>A domain name pointed to your server IP li>About 30 minutes of time
Step 1: Update Your System
First, update your system packages to ensure you have the latest security patches and software:
sudo apt update && sudo apt upgrade -y
**Screenshot description**: Terminal showing package update progress with green checkmarks indicating successful updates.
Step 2: Install PHP and Required Extensions
Laravel requires PHP 8.0 or higher. Install PHP and the necessary extensions:
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-xml php8.1-curl php8.1-gd php8.1-mbstring php8.1-xmlrpc php8.1-soap php8.1-intl php8.1-zip php8.1-bcmath php8.1-json php8.1-cli -y
**Screenshot description**: Terminal showing PHP installation process with package names being downloaded and configured.
Step 3: Install MySQL Database
Install MySQL for your Laravel application:
sudo apt install mysql-server -y
Secure your MySQL installation:
sudo mysql_secure_installation
During the setup, recommend these settings:
- Set up Validate Password Component: Yes li>Password strength level: 2 (Medium) li>Set a strong root password li>Remove anonymous users: Yes li>Disallow root login remotely: Yes li>Remove test databases: Yes li>Reload privilege tables: Yes
**Screenshot description**: MySQL security configuration interface showing interactive prompts and options.
Step 4: Create Laravel Database and User
Log into MySQL and create a database and user for Laravel:
sudo mysql -u root -p
Enter your MySQL root password, then run:
CREATE DATABASE laravel_app;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
**Screenshot description**: MySQL command line interface showing database and user creation commands.
Step 5: Install Nginx
Install Nginx web server:
sudo apt install nginx -y
**Screenshot description**: Nginx installation progress showing download and configuration process.
Step 6: Install Composer
Composer is PHP's dependency manager. Install it globally:
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
Step 7: Install Laravel
Create a directory for your Laravel application:
sudo mkdir -p /var/www/laravel-app
sudo chown -R www-data:www-data /var/www/laravel-app
Navigate to the directory and install Laravel using Composer:
cd /var/www/laravel-app
composer create-project laravel/laravel . --prefer-dist
**Screenshot description**: Composer installation process showing package downloads and dependency resolution.
Step 8: Configure Laravel Environment
Copy the environment file and configure it:
cp .env.example .env
Generate the application key:
php artisan key:generate
Edit the .env file with your database credentials:
APP_NAME=MyLaravelApp
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=YourStrongPassword123!
**Screenshot description**: .env file content showing database configuration and application settings.
Step 9: Set File Permissions
Laravel needs proper file permissions to work correctly:
sudo chown -R www-data:www-data /var/www/laravel-app
sudo find /var/www/laravel-app -type d -exec chmod 755 {} \;
sudo find /var/www/laravel-app -type f -exec chmod 644 {} \;
sudo chmod -R 777 /var/www/laravel-app/storage
sudo chmod -R 777 /var/www/laravel-app/bootstrap/cache
Step 10: Configure PHP-FPM
Edit the PHP-FPM configuration file:
sudo nano /etc/php/8.1/fpm/php.ini
Update these settings for better performance:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_vars = 3000
max_input_time = 300
Restart PHP-FPM:
sudo systemctl restart php8.1-fpm
**Screenshot description**: PHP configuration file with Laravel-recommended settings highlighted.
Step 11: Configure Nginx for Laravel
Create a new Nginx configuration file for your Laravel site:
sudo nano /etc/nginx/sites-available/laravel-app
Add this configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/laravel-app/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
}
Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/laravel-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
**Screenshot description**: Nginx configuration file with Laravel-specific rules and syntax highlighting.
Step 12: Install SSL Certificate with Let's Encrypt
Install Certbot for SSL certificates:
sudo apt install certbot python3-certbot-nginx -y
Obtain and install SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
When prompted, choose option 2 to redirect HTTP to HTTPS.
**Screenshot description**: Certbot interactive interface showing SSL certificate configuration options.
Step 13: Configure Firewall
Open necessary ports and enable UFW:
sudo ufw allow 'OpenSSH'
sudo ufw allow 'Nginx Full'
sudo ufw enable
Step 14: Run Database Migrations
Now that everything is configured, run the Laravel migrations:
cd /var/www/laravel-app
php artisan migrate --force
Step 15: Set Up Cron Job
Laravel requires a cron job for task scheduling. Open crontab:
crontab -e
Add this line to the file:
* * * * * cd /var/www/laravel-app && php artisan schedule:run >> /dev/null 2>&1
Step 16: Test Your Installation
Open your browser and navigate to your domain. You should see the Laravel welcome page. Test that everything is working:
-
li>Visit
https://yourdomain.com
- should show Laravel welcome page
li>Visit https://yourdomain.com/storage
- should work with permissions set
li>Visit https://yourdomain.com/env
- should show environment information
Troubleshooting Common Issues
502 Bad Gateway Error
If you get a 502 error, check PHP-FPM status:
sudo systemctl status php8.1-fpm
Permission Issues
If you get permission errors, ensure proper ownership:
sudo chown -R www-data:www-data /var/www/laravel-app
SSL Issues
If SSL isn't working, check certificate status:
sudo certbot certificates
Conclusion
Congratulations! You've successfully installed Laravel on Ubuntu server with a production-ready setup including Nginx, PHP-FPM, MySQL, and SSL. Your Laravel application is now live and ready for development.
From here, you can:
- Deploy your Laravel applications li>Set up continuous integration li>Configure additional services like Redis li>Set up monitoring and logging
Your Laravel server is now ready to handle production traffic!
Happy coding! 🚀