Tag Archives: Laravel

Install Laravel Debian 12

How to Install Laravel on Debian 12

Laravel is a free, open-source PHP web application framework used for web development. It follows the Model-View-Controller (MVC) architectural pattern and provides an elegant syntax and tools for tasks such as routing, templating, authentication, and more. Laravel aims to make web development tasks more enjoyable and efficient by providing a clean and expressive syntax, along with a set of conventions and tools for common tasks.

Laravel has gained popularity in the PHP development community due to its elegant syntax, developer-friendly features, and active community. It is widely used for building web applications, APIs, and various other web-based projects.

In this tutorial, we will show you how to install Laravel on Debian 12 OS with Nginx web server and MariaDB database server.

Step 1: Update Operating System

The first step is to ensure that your system is up-to-date. You can do this by running the following command:

# apt update && apt upgrade

Also, install necessary packages.

# apt install sudo nano wget unzip zip

Step 2: Install Nginx Web Server

To install Nginx web server, run the following command:

# apt install nginx

You can start the Nginx service and configure it to run on startup by entering the following commands:

# systemctl start nginx
# systemctl enable nginx

Verify the status of the Nginx service using systemctl status command:

# systemctl status nginx

Step 3: Install PHP and PHP extensions

To install PHP and additional PHP modules to support Laravel, run the following command:

# apt install php php-cli php-common php-json php-gmp php-fpm php-xmlrpc php-bcmath php-imagick php-curl php-zip php-gd php-mysql php-xml php-mbstring php-xmlrpc php-intl

Verify if PHP is installed.

# php -v
Output:
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

After installing all the packages, edit the php.ini file:

# nano /etc/php/8.2/fpm/php.ini

Change the following settings per your requirements:

max_execution_time = 300
memory_limit = 512M
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = America/Chicago

To implement the changes, restart the php-fpm service:

# systemctl restart php8.2-fpm

Step 4: Install MariaDB and create a database

You can install MariaDB with the following command:

# apt install mariadb-server mariadb-client

Start the database server daemon, and also enable it to start automatically at the next boot with the following commands:

# systemctl start mariadb
# systemctl enable mariadb

Once the database server is installed, log into the MariaDB prompt:

# mysql -u root

To create a database, database user, and grant all privileges to the database user run the following commands:

MariaDB [(none)]> CREATE DATABASE laravel_db;
MariaDB [(none)]> CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'Str0ngPa$$word';
MariaDB [(none)]> GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT

Step 5: Install Composer dependency manager

To install Composer, run the following commands:

# curl -sS https://getcomposer.org/installer | php
# mv composer.phar /usr/local/bin/composer

Verify that Composer has been installed successfully by running the following command:

# composer --version
Composer version 2.5.8 2023-06-09 17:13:21

Step 6: Install Laravel 

Navigate to the webroot directory, type:

# cd /var/www/html

Now, install Laravel using the composer command, type:

# composer create-project laravel/laravel laravelapp

The command creates a new directory called laravelapp and installs all the files and directories for Laravel.

Change the ownership of the Laravel directory to the webserver user and also the permissions:

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

Once the installation is done navigate to the installation directory and check the Laravel version:

# cd laravelapp
# php artisan

Step 7: Configure Nginx Web Server for Laravel

Navigate to /etc/nginx/conf.d directory and run the following command to create a configuration file:

# nano /etc/nginx/conf.d/laravel.conf

Add the following content:

server {
    listen 80;
    listen [::]:80;
    server_name your-domain.com;
    root /var/www/html/laravelapp/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Save the file and Exit.

Check Nginx syntax:

# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To implement the changes, restart Nginx webserver:

# systemctl restart nginx

Step 8: Access your Laravel Application

Open your browser and type your domain e.g http://your-domain.com

Comments and Conclusion

That’s it. You have successfully installed Laravel on Debian 12.

For additional help or useful information, we recommend you to check  the official Laravel documentation.

If you have any questions please leave a comment below.

How to Install Laravel 9 on Debian 11

Laravel is a popular open source web framework built for PHP developer. It’s an MVC framework for building simple to complex web applications using the PHP programming language, and it strictly follows the MVC (model–view–controller) architectural pattern.

In this tutorial we will show you how to install Laravel 9 (LTS) on Debian 11.

System prerequisites

Your system will need to satisfy the requirements below before proceeding.

  • Apache Web server
  • MySQL/MariaDB
  • PHP >= 8.0.2 with OpenSSL, PDO, Mbstring, Tokenizer, XML, Ctype and JSON PHP Extensions.
  • Composer – an application-level package manager for the PHP

Update Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Install Apache web server

To install Apache web server, run the following command:

$ sudo apt install apache2

Once installed, Apache should be running. If it’s not, for whatever reason, start it:

$ sudo systemctl start apache2

Then enable it to start on boot time.

$ sudo systemctl enable apache2

Install PHP and PHP extensions for Laravel Application

First and most importantly, Laravel 9 requires the latest PHP 8.

By default, PHP 8.1 is not included in the Debian 11 default repository. So you will need to add the DEB.SURY.ORG repository to APT.

First, install the required packages using the following command:

$ sudo apt-get install ca-certificates apt-transport-https software-properties-common -y

Once all the packages are installed, add a Sury repository to APT using the following command:

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

Next, download and add the GPG key with the following command:

$ wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -

Once you are done, update the repository with the following command:

$ apt-get update -y

Now, you can install the PHP 8.1 using the following command:

$ sudo apt-get install php8.1 libapache2-mod-php php8.1-dev php8.1-zip php8.1-curl php8.1-mbstring php8.1-mysql php8.1-gd php8.1-xml

Verify if PHP is installed.

php -v
Output:
PHP 8.1.2 (cli) (built: Jan 27 2022 12:22:31) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

Install MariaDB and create a database

You can install MariaDB with the following command:

$ sudo apt install mariadb-server mariadb-client

Note: If you want newer version of MariaDB you can follow our tutorial here.

Start the database server daemon, and also enable it to start automatically at the next boot with the following commands:

$ systemctl start mariadb
$ systemctl enable mariadb

Once the database server is installed, log into the MariaDB prompt:

$ sudo  mysql -u root -p

To create a database, database user, and grant all privileges to the database user run the following commands:

MariaDB [(none)]> CREATE DATABASE laravel_db;
MariaDB [(none)]> CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'Password';
MariaDB [(none)]> GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT

Install Composer

To download Composer, run the following command:

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

Next, move the composer file to the /usr/local/bin path.

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

Assign execute permission:

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

Verify the Composer version installed:

$ composer --version

Output:
Composer version 2.2.6 2022-02-04 17:00:38

Install Laravel 9 on Debian 11

Navigate to the webroot directory, type:

$ cd /var/www/html

Now, install Laravel using the composer command, type:

$ sudo composer create-project laravel/laravel laravelapp

The command creates a new directory called laravelapp and installs all the files and directories for Laravel.

Change the ownership of the Laravel directory to the webserver user and also the permissions:

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

Once the installation is done navigate to the installation directory and check the Laravel version:

$ cd laravelapp
$ php artisan

Framework 9.0.2

Configure Apache Web Server for Laravel

Navigate to /etc/apache2/sites-available directory and run the following command to create a configuration file for your Laravel installation:

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

Add the following content:

<VirtualHost *:80>

ServerName your-domain.com

ServerAdmin webmaster@your-domain.com
DocumentRoot /var/www/html/laravelapp/public

<Directory /var/www/html/laravelapp/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined

</VirtualHost>

Save the file and Exit.

Enable the laravel virtual host.

$ sudo a2ensite laravel.conf

Restart the Apache web server.

$ sudo systemctl restart apache2

Access your Laravel Application

Open your browser and type your domain e.g http://your-domain.com

Comments and Conclusion

That’s it. You have successfully installed Laravel 9 on Debian 11.

If you have any questions please leave a comment below.