How To Install Laravel on Debian 12
In this tutorial, we will show you how to install Laravel on Debian 12. For those of you who didn’t know, Laravel has emerged as one of the leading PHP frameworks for web development, offering an elegant and efficient approach to building web applications.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you step-by-step install Laravel PHP Framework on a Debian 12 (Bookworm).
Prerequisites
- A server running one of the following operating systems: Debian 12 (Bookworm).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Laravel.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Laravel on Debian 12 Bookworm
Step 1. Before installing any software on Debian 12, it is recommended to update the package list to ensure that you have the latest version of the software. You can do this by running the following command in the terminal:
sudo apt update sudo apt upgrade
Step 2. Installing Apache.
Now that the repository is up to date, you can proceed with the installation of Apache:
sudo apt install apache2
After the installation is complete, you can check the Apache service status to ensure it is running correctly:
sudo systemctl status apache2
If Apache is running successfully, you will see an “active (running)” message in the output. You can also verify by visiting your server’s IP address or domain name in a web browser. You should see the default Apache landing page if everything is set up correctly.
Step 3. Installing PHP.
Now that your system is up to date, it’s time to install PHP on Debian 12. First, add the Ondřej Surý PHP repository to Debian 12 by executing the following commands in the terminal:
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
These commands will enable your system to fetch PHP packages from the repository.
Next, update the package lists once again and install PHP and the necessary extensions by running the command:
sudo apt update sudo apt install php8.2
To install Laravel, there are some PHP extensions you must enable, such as fileinfo, mbstring, and OpenSSL. You can enable those extensions via the php.ini file;
sudo nano /etc/php/8.2/apache2/php.ini
Uncomment the following lines to enable the extension fileinfo, mbstring, and openssl.
extension=fileinfo extension=mbstring extension=openssl
save the file and restart the apache2 service and apply the changes:
sudo systemctl restart apache2
Step 4. Installing MariaDB on Debian 12.
To install the latest stable version of MariaDB, we’ll add the official MariaDB repository to our system:
wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
Make the downloaded file executable:
chmod +x mariadb_repo_setup
Run the setup file:
sudo ./mariadb_repo_setup
The setup script will configure the repository and import the repository key.
After adding the MariaDB repository, we need to update the repositories once more and to install the MariaDB server package, execute the following command:
sudo apt install mariadb-server
During the installation, you will be prompted to enter your password. Choose a strong password and keep it secure.
After the installation is complete, start the MariaDB service using the following command:
sudo systemctl start mariadb
To improve the default security settings, MariaDB provides a security script. Run the script and follow the prompts to enhance your installation’s security:
sudo mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] y - Remove anonymous users? [Y/n] y - Disallow root login remotely? [Y/n] y - Remove test database and access to it? [Y/n] y - Reload privilege tables now? [Y/n] y
Next, run the following queries to create a new database lavarels, the user lavarels, and the password your-strong-password. Be sure to change the password in the following query.
CREATE DATABASE laravels; CREATE USER laravels@localhost IDENTIFIED BY 'your-strong-password'; GRANT ALL PRIVILEGES ON laravels.* TO laravels@localhost; FLUSH PRIVILEGES;
After that, run the query below to ensure the user lavarels can access the database lavarels:
SHOW GRANTS FOR laravels@localhost;
Step 5. Installing Composer.
Now, it’s time to download the Composer installation script and verify its integrity. Execute the following commands:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Now that we have the Composer installation script, we can move the executable file to a globally accessible location on our system. Run the following command:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
To verify that Composer is successfully installed on your Debian system, run the following command:
composer --version
If the installation was successful, you will see the Composer version information displayed on the screen, indicating that Composer is ready for use.
Step 6. Creating a Laravel Project.
Now that everything is set up, it’s time to create your Laravel project. Run the following command:
sudo mkdir -p /var/www/{.cache,.config,newproject} sudo chown -R www-data:www-data /var/www/{.cache,.config,newproject}
Next, move to the /var/www/newproject/
directory and run the composer command below to create the first Laravel project. Your first Laravel project is stored at /var/www/newproject/
directory:
cd /var/www/newproject/ sudo -u www-data composer create-project laravel/laravel .
With the Laravel project created, next you will set up the database for your project via the .env
configuration file. The .env
file is used as the base configuration for your Laravel project:
nano .env
Change the APP_URL
parameter with the local domain name of your Laravel project:
APP_URL=http://newproject.local
Now change the database configuration DB_CONNECTION to MySQL and change the details of DB_DATABASE, DB_USERNAME, and DB_PASSWORD with your details MariaDB database and user.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravels DB_USERNAME=laravels DB_PASSWORD=ypur-strong-password
Save the file, then run the following command to migrate the database:
sudo -u www-data php artisan migrate
Step 7. Configuring Apache Virtual Host.
Now create the Apache2 virtual host configuration /etc/apache2/sites-available/laravel.conf
:
sudo nano /etc/apache2/sites-available/laravel.conf
Add the following file:
<VirtualHost *:80> ServerAdmin admin@newproject.local ServerName testapp.local DocumentRoot /var/www/newproject/public <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/newproject> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Save the file, then restart the apache2 service by executing the command below and applying the changes:
sudo a2ensite laravel.conf sudo a2enmod rewrite sudo systemctl restart apache2
Step 8. Accessing Laravel Web Interface.
Once successfully installed, open your web browser and access the Laravel web interface using the URL http://newproject.local
. You will be redirected to the following page:
Step 9. Troubleshooting Tips.
-
If you encounter any PHP extension-related issues, verify that the correct extensions are installed. Use
php -m
to check the enabled extensions. -
If Composer installation fails, double-check the PHP version and ensure that the necessary dependencies are installed.
-
For database-related errors, review your database configurations in the
.env
file of your Laravel project.
Congratulations! You have successfully installed Laravel. Thanks for using this tutorial to install Laravel PHP Framework on Debian 12 Bookworm. For additional help or useful information, we recommend you check the official Laravel website.