In this guide, you will learn How To Set up Laravel on Debian 11.
Laravel is an open-source PHP framework, which is robust and easy to understand. It follows a model-view-controller design pattern. Laravel reuses the existing components of different frameworks which helps in creating a web application. The web application thus designed is more structured and pragmatic.
Laravel offers a rich set of functionalities that incorporates the basic features of PHP frameworks like CodeIgniter, Yii, and other programming languages like Ruby on Rails. Laravel has a very rich set of features that will boost the speed of web development.
How To Set up Laravel on Debian 11
Before you start to set up Laravel, you need to log in to your server as a root or non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Debian 11.
Also, you need a domain name that pointed to your server’s IP address.
Now follow the steps below to complete this guide.
Install Apache and PHP on Debian 11
To install Laravel, you need to have Apache and PHP installed on your server.
First, update your local package index with the following command:
apt update
Then, install Apache with the command below:
apt install apache2 -y
At this point, you need to install PHP and its extensions for the Laravel on Debian 11.
First, install the required packages with the command below:
apt install apt-transport-https gnupg2 ca-certificates -y
Then, add the GPG key and PHP repository to your server with the following commands:
# 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 your local package index again:
apt update
Now you can install PHP and its extensions with the following command:
apt 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
Verify your PHP installation on Debian 11 by checking its version:
php -v
Output
PHP 8.1.5 (cli) (built: Apr 22 2022 04:56:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies
Here you need to make some configuration changes to the PHP config file. Open the file with your favorite text editor, here we use vi:
vi /etc/php/8.1/apache2/php.ini
Find the lines below and uncomment them by removing the “;” from the beginning of the line and changing them to:
cgi.fix_pathinfo=0 date.timezone = US/Eastern
Remember to put your time zone.
When you are done save and close the file.
Set up Composer on Debian 11
In this guide, you will install Laravel by using Composer on Debian 11. So you need to install Composer with the following command:
curl -sS https://getcomposer.org/installer | php
When your installation is completed, you will get the following output:
Output
All settings correct for using Composer
Downloading...
Composer (version 2.3.5) 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
Verify your Composer installation on Debian 11 by checking its version:
composer --version
Output
Composer version 2.3.5 2022-04-13 16:43:00
Install Laravel on Debian 11
At this point, you can start your Laravel installation. First, switch to your Apache webroot directory:
cd /var/www/html
Then, download and install Laravel by using the composer:
composer create-project --prefer-dist laravel/laravel laravel --ignore-platform-req=ext-curl
When your installation is completed, you will get the following output:
Output
78 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 --force
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.
Next, you need to set the correct permissions and ownership to the Laravel directory with the following commands:
# chown -R www-data:www-data /var/www/html/laravel # chmod -R 775 /var/www/html/laravel
Configure Apache for Laravel
At this point, you need to create an Apache virtual host configuration file for Laravel. Create and open the file with your favorite text editor, here we use vi:
vi /etc/apache2/sites-available/laravel.conf
Add the following content to the file with your own domain name:
<VirtualHost *:80> ServerName domain-name 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>
When you are done, save and close the file.
Now enable the Apache virtual host and rewrite module with the following commands:
# a2enmod rewrite # a2ensite laravel.conf
Finally, restart the Apache service to apply the changes:
systemctl restart apache2
Access Laravel Web Interface
At this point, you can access the Laravel web interface by typing your domain name in your web browser:
http://domain-name
You should see the following screen:
Conclusion
At this point, you learn to Set up Laravel on Debian 11.
Hope you enjoy it.
May you will be interested in this article about How To Install and Secure Laravel on AlmaLinux 8.