Install Laravel with LAMP Stack on Debian 12 Bookworm | Best Guide Steps

Posted on

Install Laravel with LAMP Stack on Debian 12 Bookworm | Best Guide Steps

Install Laravel with LAMP Stack on Debian 12 Bookworm | Best Guide Steps

This tutorial is designed to guide you through the process of installing Laravel with a LAMP (Linux, Apache, MySQL/MariaDB, PHP) stack on Debian 12 Bookworm. Laravel is a powerful PHP framework, ideal for building robust and scalable web applications. It simplifies common development tasks like routing, templating, and authentication, allowing developers to focus on the unique features of their applications.

Before we dive into the installation, let’s ensure you have everything you need to proceed.

Requirements for Laravel Setup

Before you start, you’ll need the following:

  1. Server Access: A Debian 12 Bookworm server with SSH access, configured with a non-root user having sudo privileges and a basic firewall. Refer to "Initial Server Setup with Debian 12 Bookworm" for detailed instructions.
  2. LAMP Stack: A fully functional LAMP stack installed on your Debian 12 server. Follow the guide "How To Install LAMP Stack on Debian 12 Bookworm" for a comprehensive installation process.
  3. Domain Name (Optional): A domain name pointed to your server’s public IP address. This is recommended for a production environment but not strictly necessary for development purposes.

Once you have met these prerequisites, you’re ready to proceed with the following steps to install Laravel with the LAMP stack on Debian 12 Bookworm.

Step 1 – PHP Extensions for Laravel on Debian 12

Assuming you have a working PHP installation from the LAMP stack setup, you need to install several PHP extensions required by Laravel. Use the following command to install these extensions:

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

This command installs extensions for string manipulation (mbstring), command-line interface (cli), arbitrary precision mathematics (bcmath), JSON handling, XML parsing, ZIP archive management, database interaction (PDO, MySQL), common PHP functionality, tokenization, and cURL for making HTTP requests.

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

To install Laravel with the LAMP stack on Debian 12 Bookworm, a dedicated database and user are necessary. Log in to the MariaDB shell using the root user:

sudo mysql -u root -p

Enter the root password when prompted. Then, create the database:

-- MariaDB [(none)]>
CREATE DATABASE laraveldb;

Next, create a dedicated user for the Laravel application, replacing <secretpassword> with a strong, unique password:

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

Grant the user full access to the laraveldb database:

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

Finally, refresh the privilege tables and exit the MariaDB shell:

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

Step 3 – Install PHP Composer on Debian 12

Composer is a dependency manager for PHP, and we’ll use it to install Laravel.

First, download the Composer installer:

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

This command downloads the composer.phar file. The output will look similar to this:

-- Output
All settings correct for using Composer
Downloading...

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

Move the composer.phar file to the /usr/local/bin directory to make it globally accessible:

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

Set the correct permissions for the file:

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

Verify the Composer installation by checking its version:

composer --version

The output should display the installed Composer version:

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

Step 4 – Use Composer To Install Laravel on Debian 12

Now, let’s use Composer to install Laravel. Navigate to the webroot directory:

cd /var/www/html

Use the Composer create-project command to install Laravel:

sudo composer create-project laravel/laravel laravelapp

This command creates a directory named laravelapp and downloads all the necessary Laravel files and dependencies into it.

Change the ownership of the Laravel directory and set the correct permissions. This ensures that the web server can access the files:

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

Navigate to the newly created laravelapp directory:

cd laravelapp

Verify the Laravel installation by running the Artisan console:

php artisan

The output should display a list of available Artisan commands, confirming that Laravel is installed correctly:

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

Now, configure Apache to serve the Laravel application. Create a virtual host configuration file:

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

Add the following content to the file, replacing example.com with your server’s domain name or IP address:

<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: Replace example.com with the actual domain or IP address of your server.

Save and close the file. Enable the virtual host and the Apache rewrite module:

sudo a2ensite laravel.conf
sudo a2enmod rewrite

Restart Apache to apply the changes:

sudo systemctl restart apache2

Step 6 – Access Laravel Web Interface

Open your web browser and navigate to your server’s domain name or IP address:

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

You should see the default Laravel welcome page.

Conclusion

Congratulations! You have successfully installed Laravel with a LAMP stack on Debian 12 Bookworm. You can now start building your web application using the Laravel framework. If you encounter any issues, feel free to seek assistance.

Alternative Solutions for Installing Laravel

While the above steps detail a standard approach, here are two alternative ways to install and run Laravel on Debian 12 Bookworm:

1. Using Docker Compose

Docker offers a containerization solution, allowing you to run applications in isolated environments. Docker Compose simplifies the process of defining and managing multi-container Docker applications. This method offers several advantages:

  • Isolation: Laravel and its dependencies are isolated from the host system, preventing conflicts.
  • Reproducibility: The Docker configuration ensures consistent environments across different machines.
  • Simplified Deployment: Docker simplifies the deployment process.

Steps:

  1. Install Docker and Docker Compose: Follow the official Docker documentation to install Docker and Docker Compose on your Debian 12 Bookworm server.

  2. Create a docker-compose.yml file: In your project directory (e.g., /var/www/html/laravelapp), create a docker-compose.yml file.

    version: "3.8"
    services:
      app:
        build:
          context: .
          dockerfile: Dockerfile
        image: laravel-app
        container_name: laravel-app
        ports:
          - "8000:8000"
        volumes:
          - .:/var/www/html
        depends_on:
          - db
        environment:
          - APP_DEBUG=true
          - APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          - DB_CONNECTION=mysql
          - DB_HOST=db
          - DB_PORT=3306
          - DB_DATABASE=laraveldb
          - DB_USERNAME=laraveluser
          - DB_PASSWORD=secretpassword
    
      db:
        image: mysql:8.0
        container_name: mysql-db
        ports:
          - "3306:3306"
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=laraveldb
          - MYSQL_USER=laraveluser
          - MYSQL_PASSWORD=secretpassword
        volumes:
          - db_data:/var/lib/mysql
    
    volumes:
      db_data:
  3. Create a Dockerfile: In the same directory, create a Dockerfile to define the image for the Laravel application.

    FROM php:8.1-apache
    
    RUN apt-get update && apt-get install -y 
        git 
        zip 
        unzip 
        libpng-dev 
        libjpeg62-turbo-dev 
        libfreetype6-dev 
        locales 
        && rm -rf /var/lib/apt/lists/*
    
    RUN docker-php-ext-configure gd --with-freetype --with-jpeg 
        && docker-php-ext-install -j$(nproc) gd
    
    RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl opcache
    
    RUN pecl install xdebug 
        && docker-php-ext-enable xdebug
    
    COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
    
    WORKDIR /var/www/html
    
    COPY . .
    
    RUN composer install --no-interaction --optimize-autoloader
    
    RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
    RUN chmod -R 775 /var/www/html/storage
    RUN chmod -R 775 /var/www/html/bootstrap/cache
    
    EXPOSE 8000
    
    CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
  4. Run Docker Compose: From the project directory, run:

    docker-compose up -d

    This command builds the Docker images and starts the containers in detached mode. You can then access the Laravel application at http://your-server-ip:8000.

2. Using Laravel Valet (Specifically for Local Development)

Laravel Valet is a development environment specifically designed for macOS. While not directly applicable to a production Debian 12 server, it’s a fantastic alternative for local development. It configures your Mac to always run Nginx in the background and uses DnsMasq to proxy requests to your Laravel sites.

Steps:

  1. Install Composer: Make sure Composer is installed globally on your macOS machine.

  2. Install Valet:

    composer global require laravel/valet
  3. Configure Valet:

    valet install

    This configures Valet and DnsMasq. You may be prompted for your password to configure DnsMasq.

  4. Park a Directory: Navigate to the directory where you store your Laravel projects and run:

    valet park

    This tells Valet to serve all Laravel projects within that directory.

  5. Create a New Laravel Project (or link an existing one): You can create a new project with:

    composer create-project laravel/laravel my-project

    Or, for an existing project, navigate to the project directory and run:

    valet link my-project
  6. Access your site: You can now access your Laravel application in your browser at http://my-project.test. Valet automatically configures the .test domain.

Laravel Valet is a zero-configuration development environment, making it extremely easy to set up and use for local Laravel development. It’s significantly faster than using virtual machines or Docker for local development due to its lightweight nature. However, remember that it’s not suitable for production deployments, and is specifically designed for macOS.

These alternative solutions provide different approaches to deploying and developing Laravel applications, offering flexibility based on your specific needs and environment. The Docker Compose method provides a robust and portable solution suitable for both development and production, while Laravel Valet simplifies local development on macOS.

Leave a Reply

Your email address will not be published. Required fields are marked *