Setting Up the Laravel Framework on Debian 11

Laravel is a robust, open-source PHP web framework that simplifies web application development by building on the Symfony framework. With its intuitive features, Laravel streamlines tasks like authentication, routing, sessions, and caching. Key features include Artisan, Object-Relational Mapping, Template Engine, MVC Architecture, Unit Testing, and a Database Migration System.

In this guide, we’ll walk you through installing Laravel on a Debian 11 server.

Prerequisites

  • A server running Debian 11.
  • A valid domain name linked to your server’s IP address.
  • A root password configured on the server.

Step 1: Install Apache Web Server

Initiate the installation of the Apache package with the following command:

apt-get install apache2 -y

After the Apache installation, verify its version with:

apache2ctl -v

You should expect the following output:

Server version: Apache/2.4.48 (Debian)
Server built:   2021-08-12T11:51:47

Step 2: Install PHP and Required Extensions

You’ll need PHP and additional extensions for Laravel. Start by installing the dependencies:

apt-get install apt-transport-https gnupg2 ca-certificates -y

Next, add the GPG key and PHP repository:

wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

Update the repository and proceed with PHP installation along with required extensions:

apt-get update -y
apt-get install libapache2-mod-php php php-common php-xml php-gd php8.0-opcache php-mbstring php-tokenizer php-json php-bcmath php-zip unzip curl -y

After PHP is installed, edit the php.ini file for necessary adjustments:

nano /etc/php/8.0/apache2/php.ini

Modify these lines:

cgi.fix_pathinfo=0 
date.timezone = Asia/Kolkata

Save your changes, close the file, and confirm the PHP version with:

php -v

Sample output:

PHP 8.0.10 (cli) (built: Aug 26 2021 16:06:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies

Step 3: Install Composer

Composer, the PHP dependency manager, can be installed using:

curl -sS https://getcomposer.org/installer | php

Sample output:

All settings correct for using Composer
Downloading...

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

Move the Composer binary to your system’s path:

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

Verify the Composer version:

composer --version

You should see:

Composer version 2.1.6 2021-08-19 17:11:08

Step 4: Install Laravel

Navigate to the Apache webroot and use Composer to download Laravel:

cd /var/www/html
composer create-project --prefer-dist laravel/laravel laravel

Expected output:

> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
76 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
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.

Set appropriate permissions and ownership:

chown -R www-data:www-data /var/www/html/laravel
chmod -R 775 /var/www/html/laravel

Proceed to the final configuration steps.

Step 5: Configure Apache for Laravel

Create an Apache virtual host configuration for Laravel:

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

Add this configuration:

<VirtualHost *:80>
    ServerName laravel.example.com

    ServerAdmin admin@example.com
    DocumentRoot /var/www/html/laravel/public

    <Directory /var/www/html/laravel>
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
    </Directory>

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

Save the file, then enable the virtual host and rewrite module:

a2enmod rewrite
a2ensite laravel.conf

Restart Apache to apply changes:

systemctl restart apache2

Check the status of the Apache service:

systemctl status apache2

You should see:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-08-27 06:00:25 UTC; 7s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 14020 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 14025 (apache2)
      Tasks: 6 (limit: 2341)
     Memory: 13.2M
        CPU: 97ms
     CGroup: /system.slice/apache2.service
             ??14025 /usr/sbin/apache2 -k start
             ??14026 /usr/sbin/apache2 -k start
             ??14027 /usr/sbin/apache2 -k start
             ??14028 /usr/sbin/apache2 -k start
             ??14029 /usr/sbin/apache2 -k start
             ??14030 /usr/sbin/apache2 -k start

Aug 27 06:00:25 debian11 systemd[1]: Starting The Apache HTTP Server...

Step 6: Access Laravel

Launch your browser and visit http://laravel.example.com to view the Laravel welcome page:

Laravel on Debian 11

Conclusion

Congratulations! You’ve successfully installed Laravel with Apache on Debian 11. You can now begin developing applications with the Laravel framework. If you have any questions, feel free to reach out.

Frequently Asked Questions

Why should I use Laravel?

Laravel offers a clean and elegant syntax, streamlining common web development tasks like authentication, routing, sessions, and caching.

Can I follow this guide if I’m using a different Debian version?

This guide is tailored for Debian 11. While it might work on other versions, adjustments could be necessary for compatibility.

What if I encounter issues using Composer?

Ensure you have a stable internet connection and refer to the official Composer documentation for additional troubleshooting tips.

How can I secure my Laravel installation?

For enhanced security, consider using HTTPS, regularly updating your software, and following Laravel’s security guidelines.