How to Install Laravel with LAMP on Ubuntu 24.04

Introduction

Laravel is a web application framework with expressive, elegant syntax. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching.

Pre-requisites

  • A system with Ubuntu 24.04 installed and running.
  • root access to the system.
  • LAMP Stack installed and running. For this, you can refer to one of our guides on installing the LAMP Stack (Apache, MariaDB, and PHP).

Install Git

apt -y install git
git --version

Output:

root@vps:~# git --version
git version 2.43.0

Install Dependencies

apt install -y unzip php8.3-zip php8.3-mysql php8.3-xml php8.3-curl php8.3-mbstring

Verify PHP modules:

php -m

Install Composer

php -r "copy('http://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php

Output:

root@vps:~# php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 2.8.6) successfully installed to: /root/composer.phar
Use it: php composer.phar

Move Composer to a globally accessible location:

mv composer.phar /usr/local/bin/composer

Verify Composer installation:

composer -V

Output:

root@vps:~# composer -V
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? y
Composer version 2.8.6 2025-02-25 13:03:50
PHP version 8.3.6 (/usr/bin/php8.3)
Run the "diagnose" command to get more detailed diagnostics output.
root@vps:~# 

Setting Up Laravel

Configure Apache and Clone Laravel

If your website is already hosted in /var/www/html/, you can create a new directory and use that instead of html. If the html directory is empty, you can use it as is.

cd /var/www/html

Change ownership:

chown -R $USER:$USER /var/www/html

Clone Laravel repository:

git clone https://github.com/laravel/laravel.git /var/www/html

Navigate to Laravel directory and install dependencies:

composer install

Setup environment file:

cp .env.example .env

Set correct permissions:

chown -R www-data:www-data /var/www/html/storage

Generate application key:

php artisan key:generate

Configure Apache Virtual Host

To make Laravel accessible through a domain, configure Apache,

nano /etc/apache2/sites-available/laravel.conf

Add the following configuration,

<VirtualHost *:80>
    ServerAdmin laravel.domainhere.info
    ServerName laravel.domainhere.info
    DocumentRoot /var/www/html/public

    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and rewrite module,

a2ensite laravel.conf
a2enmod rewrite
systemctl restart apache2

Setup Database

Create MySQL database and user:

mysql -uroot
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON *.* TO 'laravel_user'@'localhost';
CREATE DATABASE laravel_db;
SHOW DATABASES;
exit;

Replace the securepassword with any strong password of your choice.

Open the .env File and configure Laravel database settings in .env:

nano .env

Find the following lines in the .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=securepassword

Replace securepassword with the password you set while creating the database.

Run migrations:

php artisan migrate

Restart Apache:

systemctl restart apache2

Access Laravel Application

You can configure your domain name in the Apache configuration file to access Laravel using your domain.

Once configured, open your domain in a web browser. This will direct you to the Laravel landing page on your server:

http://dev.domainhere.info

Replace dev.domainhere.info with the actual IP or domain configured on the server.

If you have not configured a domain name and want to access Laravel using your server’s IP address, run the following command:

php artisan serve --host=0.0.0.0 --port=8000

Then, open the Laravel application in your browser:

http://your-server-ip:8000

Replace your-server-ip with your actual server IP address.

This makes Laravel accessible from any machine on the network.

Now your Laravel application should be fully set up and accessible!


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS