Install Laravel with LAMP Stack on Debian 12 Bookworm

This tutorial intends to teach you to Install and Configure Laravel with LAMP Stack on Debian 12 Bookworm. Laravel is a server-side web framework that is used to build custom web apps by using PHP. With Laravel you can handle many things that are annoying to build your web apps, such as routing, templating HTML, and authentication.

Step To Install Laravel with LAMP Stack on Debian 12 Bookworm

Before you start your Laravel installation on Debian 12 Bookworm, you need some requirements.

Requirements

First, you must have access to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can visit this guide on Initial Server Setup with Debian 12 Bookworm.

Because we want to install Laravel with LAMP, you must install it on your server by visiting this guide on How To Install LAMP Stack on Debian 12 Bookworm.

Also, you need a domain name that is pointed to your server’s IP address.

When you are done, follow the steps below to complete this guide.

Step 1 – PHP Extensions for Laravel on Debian 12

At this point, we assumed that you have installed PHP from the LAMP stack installation. Now you should install some PHP extensions that are needed for Laravel by using the following command:

sudo apt install libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql php-curl php-mysql -y

Next, proceed to the next step to create a Laravel database user.

Step 2 – Create a Laravel Database and User on Debian 12

At this point, you need to create a user and database for your Laravel. To do this, log in to your MariaDB shell with the following command:

sudo  mysql -u root -p

Now create your Laravel database with the following command:

MariaDB [(none)]> CREATE DATABASE laraveldb;

Then, you need to create a Laravel user, and remember to replace the password with a strong password of your choice:

MariaDB [(none)]> CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'secretpassword';

Next, grant all the privileges to your user with the command below:

MariaDB [(none)]> GRANT ALL ON laraveldb.* TO 'laraveluser'@'localhost';

Flush the privileges and exit from the MariaDB shell with the command below:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Step 3 – Install PHP Composer on Debian 12

In this guide, we will use Composer to install Laravel on Debian 12.

First, download the Composer installer with the following command:

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

In your output you will see:

Output
All settings correct for using Composer
Downloading...

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

As you can see this command downloads the composer.phar file.

You need to move the file into the usr/local/bin path with the command below:

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

Then, set the correct permissions for it:

sudo chmod +x   /usr/local/bin/composer

Verify your Composer installation on Debian 12 by checking its version:

composer --version

In your output you will see:

Output
Composer version 2.5.8 2023-06-09 17:13:21

Step 4 – Use Composer To Install Laravel on Debian 12

To set up Laravel, first, switch to the webroot directory with the following command:

cd /var/www/html

Then, use the Composer command to install Laravel on Debian 12:

sudo composer create-project laravel/laravel laravelapp

This command will create a directory called laravelapp and install all the files and directories for Laravel.

Now change the ownership of the Laravel directory and set the correct permissions with the commands below:

# sudo chown -R www-data:www-data /var/www/html/laravelapp
# sudo chmod -R 775 /var/www/html/laravelapp/storage

Next, switch to your laravelapp directory:

cd laravelapp

Verify your Laravel installation by using the following command on Debian 12:

php artisan

In your output you will see:

Output
Laravel Framework 10.17.1

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
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                   Display basic information about your application
  clear-compiled          Remove the compiled class file
  completion              Dump the shell completion script
  db                      Start a new database CLI session
  docs                    Access the Laravel documentation
  down                    Put the application into maintenance / demo mode
  env                     Display the current framework environment
...

Step 5 – Configure Apache to Host Laravel on Debian 12

At this point, you need to set up Apache to host the Laravel site. To do this, you need to create a virtual host file with your favorite text editor, here we use the vi editor:

sudo vi /etc/apache2/sites-available/laravel.conf

Add the following contents to the file:

<VirtualHost *:80>
ServerName example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: Remember to replace the example.com ServerName directive with the FQDN or public IP of the server.

When you are done, save and close the file.

Here use the following commands to enable the Laravel site and Apache rewrite module:

# sudo a2ensite laravel.conf
# sudo a2enmod rewrite

Then, restart Apache on Debian 12 to apply the changes:

sudo systemctl restart apache2

Access Laravel Web Interface

At this point, you can access the Laravel web interface by typing your server’s IP address in your web browser:

http://your-domain-or-server-ip-address

You will see the default Laravel page:

Laravel default page Debian 12

Conclusion

At this point, you have learned to Install and Configure Laravel with LAMP Stack and Composer on Debian 12 Bookworm and access your Laravel default page.

Hope you enjoy it. For more articles, you can visit the Orcacore website.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!