How to Install Laravel on Debian 11

install laravel on debian 11

In this blog post, we will explain in step-by-step detail how to install Laravel on Debian 11 OS.

Laravel is an open-source PHP framework that uses the famous MVC (Model-View-Controller) architectural pattern.

Taylor Otwell writes the Laravel framework, and its usage is increasing on a daily basis for building from the simplest to the most complex websites. In this post, we will install Laravel with the LAMP stack.

Installing Laravel on Debian 11 is straightforward and may take up to 15 minutes. Let’s get things done!

Prerequisites

  • A server with Debian 11 as OS
  • User privileges: root or non-root user with sudo privileges
  • A valid domain with pointed A record to the server IP address

Step 1. Update the System

Before we start installing the LAMP stack and Laravel, we will update the system packages to their latest versions.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install LAMP Stack

First, we will install the Apache Web server.

sudo apt-get install apache2 -y

Once installed, start and enable the service.

sudo systemctl enable apache2 && sudo systemctl start apache2

Check if the service is up and running:

sudo systemctl status apache2

You should receive the following output:

root@host:~# sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-03-31 09:18:02 CDT; 1h 37min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 498 (apache2)
      Tasks: 7 (limit: 4675)
     Memory: 22.9M
        CPU: 661ms
     CGroup: /system.slice/apache2.service

First, add the GPG key and the repo because, in the default repository of Debian 11, they do not exist. To install the PHP along with its extensions, execute the following command:

apt -y install lsb-release apt-transport-https ca-certificates 

wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list

sudo apt-get update -y

Once the PHP key and repo are added, you can install the PHP with extensions using this long command:

sudo apt-get install php8.1 php8.1-common php8.1-curl libapache2-mod-php php8.1-imap php8.1-redis php8.1-cli php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring php8.1-gd php8.1-xml php8.1-mysql php-mbstring -y

sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-imap php8.1-redis php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring php8.1-curl

After successful installation, check the PHP version:

php -v

You should get the following output:

root@host:~# php -v
PHP 8.2.4 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

p>The last of the LAMP stack is the MariaDB database service:

sudo apt-get install mariadb-server -y

Start and enable the mariadb.service with the following commands:

sudo systemctl start mariadb && sudo systemctl enable mariadb

Check the status of the mariadb.service

sudo systemctl status mariadb

You should receive the following output:

root@host:~# sudo systemctl status mariadb
● mariadb.service - MariaDB 10.5.18 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-03-31 11:01:41 CDT; 15s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 20668 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 19 (limit: 4675)
     Memory: 67.6M
        CPU: 497ms
     CGroup: /system.slice/mariadb.service
             └─20668 /usr/sbin/mariadbd

Step 4. Install Composer

We need to install the Composer responsible for installing all the Laravel components.

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

Check the Composer installation:

composer

You should receive the following output:

root@host:~# composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.5.5 2023-03-21 11:50:05

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display help for the given command. When no command is given display help for the list command
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi|--no-ansi           Force (or disable --no-ansi) ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
      --no-scripts               Skips the execution of all scripts defined in composer.json file.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Step 5. Install Laravel

We will install Laravel with the composer in the default apache2 document root location:

cd /var/www/html/

composer create-project laravel/laravel "ProjectName"

After successful installation, you should receive the following output:

59 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.

  laravel/sail ................................................................................................................................ DONE
  laravel/sanctum ............................................................................................................................. DONE
  laravel/tinker .............................................................................................................................. DONE
  nesbot/carbon ............................................................................................................................... DONE
  nunomaduro/collision ........................................................................................................................ DONE
  nunomaduro/termwind ......................................................................................................................... DONE
  spatie/laravel-ignition ..................................................................................................................... DONE

80 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].

No security vulnerability advisories found
> @php artisan key:generate --ansi

   INFO  Application key set successfully

Set the right permissions to Laravel’s document root

cd /var/www/html/"ProjectName"

chown -R www-data:www-data .

chmod -R 775 storage

Step 6. Create Apache Virtual Host File

Go into the Apache directory where the configuration files are stored and create a configuration file for the Laravel application.

cd /etc/apache2/sites-available/

touch laravel.conf

Paste the following lines of code, save the file and close it.

<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html/ProjectName/public

<Directory /var/www/html/ProjectName>
AllowOverride All
</Directory>

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

</VirtualHost>

Enable the Apache configuration for Laravel.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
a2ensite laravel.conf

Check the syntax:

apachectl -t

You should receive the following output:

root@vps:~# apachectl -t
Syntax OK

If the syntax is OK, restart the Apache service.

systemctl reload apache2

Once the Apache service is restarted, you can access the Laravel website at http://yourdomain.com

That’s it! You successfully installed Laravel PHP Framework on Debian 11 with Composer and Apache as a web server.

If you do not know how to install and configure Laravel on Debian 11, feel free to contact our technical support. We are available 24/7 and will help you with any aspect of configuring your server.

If you liked this post on how to install Laravel on Debian 11, please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment