Laravel is a powerful web application framework that is widely used for developing enterprise-grade web applications. It follows the model-view-controller (MVC) architecture and is based on Symfony. With its clean and elegant syntax, Laravel allows developers to create applications with ease and readability. In this guide, we will walk you through the step-by-step process of installing Laravel on a Debian 12 server, along with the necessary components like Apache2, MariaDB, and PHP 8.2.
Prerequisites
Before we begin, make sure you have the following:
- A Debian 12 server
- A non-root user with administrator privileges
Installing LAMP Stack
The first step is to install the LAMP Stack, which includes Apache2, PHP, and MariaDB. To do so, update and refresh the Debian repositories by running the following command:
sudo apt update
Once the repositories are updated, install the LAMP Stack packages along with some required PHP extensions by executing the following command:
sudo apt install apache2 php php-curl php-bcmath php-json php-mysql php-mbstring php-xml php-tokenizer php-zip mariadb-server
During the installation, you will be prompted to confirm and proceed. Answer with ‘y’ to proceed.
After the installation is complete, verify that both Apache2 and MariaDB services are running by executing the following commands:
sudo systemctl status apache2 sudo systemctl status mariadb
If the services are running properly, you should see the output as ‘active (running)’.
Next, verify the PHP version and enabled modules by running the following commands:
php -v php -m
You should see PHP 8.2 installed with various modules enabled, such as curl, date, dom, and exif.
To ensure that the installation was successful, open your web browser and visit your Debian server’s IP address. If everything is set up correctly, you should see the index.html page of the Apache web server.
Configuring PHP
Before installing Laravel, it is necessary to enable certain PHP extensions such as fileinfo, mbstring, and openssl. To do this, open the PHP configuration file using the nano editor:
sudo nano /etc/php/8.2/apache2/php.ini
Uncomment the following lines by removing the ‘;’ at the beginning of each line:
extension=fileinfo extension=mbstring extension=openssl
Save the file and exit the editor.
Next, restart the Apache2 service to apply the changes:
sudo systemctl restart apache2
To verify that the PHP extensions fileinfo, mbstring, and openssl are enabled, run the following command:
sudo php -m
You should see these extensions listed in the output.
Configuring MariaDB Server
After configuring PHP, it’s time to set up your MariaDB server. Start by securing the MariaDB installation using the mariadb-secure-installation utility:
sudo mariadb-secure-installation
During the process, you will be prompted to configure several settings. Here’s a step-by-step guide:
- Press ENTER when asked for the MariaDB root password.
- Input ‘n’ when asked about the unix_socket authentication method.
- Input ‘Y’ to set up a new password for the MariaDB root user. Enter the new password and repeat it.
- Input ‘Y’ to remove the default anonymous user from MariaDB.
- Input ‘Y’ to disable remote login for the MariaDB root user.
- Input ‘Y’ to remove the default test database from MariaDB.
- Finally, input ‘Y’ to reload table privileges and apply the changes.
Once the configuration is complete, create a new database and user for your Laravel project. Log in to the MariaDB server using the following command:
sudo mariadb -u root -p
Enter your MariaDB root password when prompted.
Run the following queries to create a new database, user, and grant privileges:
CREATE DATABASE testapp; CREATE USER testapp@localhost IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON testapp.* TO testapp@localhost; FLUSH PRIVILEGES;
To verify that the user ‘testapp’ has access to the ‘testapp’ database, run the following query:
SHOW GRANTS FOR testapp@localhost;
The output should confirm that the user has the necessary privileges.
Type ‘quit’ to exit the MariaDB server.
Installing Composer
Composer is a PHP package manager that makes it easy to install Laravel and its dependencies. To install Composer on your Debian server, run the following command:
sudo apt install composer
After the installation is complete, verify the version of Composer:
sudo -u www-data composer --version
You should see the version number displayed.
Creating Your First Laravel Project
With all the prerequisites in place, you’re ready to create your first Laravel project. Start by creating a new document root directory for your project and changing the ownership to the www-data user:
sudo mkdir -p /var/www/{.cache,.config,testapp} sudo chown -R www-data:www-data /var/www/{.cache,.config,testapp}
Navigate to the project directory and run the following command to create the Laravel project:
cd /var/www/testapp/ sudo -u www-data composer create-project laravel/laravel.
During the installation, you will see progress information displayed.
Once the Laravel project is created, you need to configure the database settings in the .env file. Open the .env file using the nano editor:
nano.env
Change the APP_URL
parameter to the local domain name of your Laravel project. This will allow you to access your project using a local domain name. For example:
APP_URL=http://testapp.local
Next, update the database configuration by modifying the following parameters:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=testapp DB_USERNAME=testapp DB_PASSWORD=password
Save the file and exit the editor.
To migrate the database and create the necessary tables for your Laravel project, run the following command:
sudo -u www-data php artisan migrate
This command will apply any pending database migrations.
Configuring Apache Virtual Host
To access your Laravel project through a web browser, you need to configure an Apache virtual host. Start by enabling the rewrite module:
sudo a2enmod rewrite
Next, create a virtual host configuration file for Laravel using the nano editor:
sudo nano /etc/apache2/sites-available/laravel.conf
Insert the following configuration, replacing ‘testapp.local’ with your domain name:
<VirtualHost *:80> ServerAdmin admin@testapp.local ServerName testapp.local DocumentRoot /var/www/testapp/public <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/testapp> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Save the file and exit the editor.
Enable the virtual host configuration and verify the Apache2 configuration:
sudo a2ensite laravel.conf
sudo apachectl configtest
If there are no syntax errors, you will see the message ‘Syntax OK’.
Finally, restart the Apache2 service to apply the changes:
sudo systemctl restart apache2
Accessing Your Laravel Project
To access your Laravel project, you need to modify the hosts file on your client machine. If you’re using a Linux client, open the hosts file using the nano editor:
sudo nano /etc/hosts
If you’re on a Windows machine, modify the file ‘C:WindowsSystem32driversetchosts’.
Add the following line to the file, replacing ‘testapp.local’ with your domain name and ‘192.168.10.15’ with the IP address of your Debian server:
192.168.10.15 testapp.local
Save the file and exit the editor.
Finally, open your web browser and visit the local domain name of your Laravel project. For example, if your domain name is ‘testapp.local’, enter the following URL:
http://testapp.local
If everything is set up correctly, you should see the Laravel welcome page.
Conclusion
Congratulations! You have successfully installed Laravel on your Debian 12 server, along with the LAMP Stack. You have also configured the database, created your first Laravel project, and set up an Apache virtual host. Now you can start developing your web applications using the powerful Laravel framework.
Remember, installing Laravel is just the first step in building a robust web application. To ensure the best performance, scalability, and security for your Laravel projects, consider hosting them on a reliable and high-performance platform like Shape.host’s Linux SSD VPS. With Shape.host, you can enjoy fast and secure cloud hosting solutions tailored to your specific needs.
So go ahead, unleash the full potential of Laravel, and create amazing web applications with ease!