Install Laravel Framework on Debian 11

Laravel is a free and open-source PHP web framework based on the Symfony framework. It is used for the faster development of web applications. Laravel makes it easier to perform some common tasks such as authentication, routing, sessions and caching. It has several useful features including, Artisan, Object-relational mapping, Template Engine, MVC Architecture, Unit-Testing, and Database Migration System.

In this tutorial, I will show you how to install Laravel on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on the server.

Install Apache Web Server

First, you will need to install the Apache package to your server. You can install it using the following command:

apt-get install apache2 -y

Once the Apache package is installed, verify the Apache version using the following command:

apache2ctl -v

You should see the following output:

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

Install PHP and Other Required Extensions

Next, you will need to install PHP and other extensions required for the Laravel application. First, install the required dependencies using the following command:

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

Next, add the GPG key and PHP repository with the following command:

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'

Next, update the repository and install PHP with other extensions using the following command:

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

Once PHP is installed, edit the php.ini file and make some changes:

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

Change the following lines:

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

Save and close the file then verify the PHP version using the following command:

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

Install Composer

Next, you will need to install the Composer to your system. You can install it using the following command:

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

Next, move the Composer binary to the system path using the following command:

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

Next, verify the Compsoer version with the following command:

composer --version

You should see the following output:

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

Install Laravel

Next, change the directory to Apache webroot and download the latest version of Laravel using the Composer:

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

You should get the following 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.

Next, set proper permissions and ownership to the Laravel directory with the following command:

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

Once you are finished, you can proceed to the next step.

Configure Apache for Laravel

Next, you will need to create an Apache virtual host configuration file for Laravel. You can create it with the following command:

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

Add the following lines:

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

    ServerAdmin [email protected]
    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 and close the file then enable the Apache virtual host and rewrite module with the following command:

a2enmod rewrite
a2ensite laravel.conf

Finally, restart the Apache service to apply the changes:

systemctl restart apache2

You can also check the status of the Apache with the following command:

systemctl status apache2

You should get the following output:

? 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...

Access Laravel

Now, open your web browser and access the Laravel web UI using the URL http://laravel.example.com. You should see the Laravel default page on the following screen:

Laravel on Debian 11

Conclusion

Congratulations! you have successfully installed Laravel with Apache on Debian 11. You can now start developing your application using the Laravel framework. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment