Installing Laravel with Apache on Ubuntu 22.04

Laravel is a modern PHP-based web application framework utilized for building robust enterprise web applications. It adheres to the model-view-controller (MVC) architecture and is grounded in Symfony, offering a clean, elegant syntax that facilitates the creation of readable and maintainable code.

As an enterprise-grade framework, Laravel enables the swift deployment of comprehensive full-stack web applications. When paired with frontend frameworks such as React or Vue, Laravel can craft stunning and interactive interfaces. Moreover, it supports multiple databases including MySQL, PostgreSQL, SQLite, and SQL Server, while offering scaffolding for secure authentication.

This tutorial will guide you through the installation of Laravel on Ubuntu 22.04 LTS, including the setup of the LAMP stack required for Laravel development.

Prerequisites

  • An Ubuntu 22.04 server.
  • A non-root user with sudo privileges or the root user.

Installing Apache Web Server

Begin by installing the Apache2 web server on your Ubuntu system. Follow these steps to install the Laravel framework using Apache2.

Update your Ubuntu repository with the command:

sudo apt update

Then install Apache2 with:

sudo apt install apache2

Confirm the installation by pressing Y and ENTER. Once Apache2 is installed, allow HTTP and HTTPS services through the firewall:

sudo ufw allow "Apache Full"

Open your web browser and visit your server’s IP address (e.g., http://192.168.10.15) to confirm Apache is running. You should see the default Apache2 page.

Installing PHP 8.1

Next, install and configure PHP 8.1, as it well integrates with Laravel. Install PHP and necessary extensions using:

sudo apt install php php-curl php-bcmath php-json php-mbstring php-xml php-tokenizer php-zip

Afterward, edit the php.ini file to enable necessary PHP extensions: fileinfo, openssl, and mbstring.

sudo nano /etc/php/8.1/apache2/php.ini
extension=fileinfo
extension=mbstring
extension=openssl

Restart Apache2 to apply the changes:

sudo systemctl restart apache2

Verify your PHP configuration by checking the installed PHP version:

php --version

Ensure the correct PHP extensions are enabled:

php -m

Installing MariaDB Server

Now install the MariaDB database server and configure it for Laravel. Begin with:

sudo apt install mariadb-server

After installation, log into the MariaDB shell and create a new database and user for your Laravel project:

sudo mysql -u root -p
CREATE DATABASE laravelapp;
CREATE USER laravel@localhost IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravelapp.* TO laravel@localhost;
FLUSH PRIVILEGES;

Exit the MariaDB shell with:

EXIT

Installing Composer

Composer is a package manager for PHP, crucial for installing Laravel. First, download Composer’s installer:

curl -sS https://getcomposer.org/installer -o composer-setup.php

Install Composer using:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Verify Composer installation:

sudo -u www-data composer --version
sudo -u www-data composer --help

Start Installing Laravel

Create directories for your Laravel project:

mkdir -p /var/www/{.cache,.config,laravelapp}

Change ownership of these directories:

sudo chown -R www-data:www-data /var/www/{.cache,.config,laravelapp}

Navigate to the Laravel directory:

cd /var/www/laravelapp/

Install Laravel with Composer:

sudo -u www-data composer create-project laravel/laravel .

Edit the environment configuration file, .env, to reflect your setup:

nano .env

Update the APP_URL and database details:

APP_URL=http://laravelapp.howtoforge.local
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelapp
DB_USERNAME=laravel
DB_PASSWORD=password

Setup Apache Virtual Host

Create a new Apache virtual host configuration file for Laravel:

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

Add the configuration:

    ServerAdmin admin@howtoforge.local
    ServerName laravelapp.howtoforge.local
    DocumentRoot /var/www/laravelapp/public
    
        Options FollowSymLinks
        AllowOverride None
    
    
        AllowOverride All
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Enable the site and restart Apache2:

sudo a2enmod rewrite
sudo a2ensite laravel.conf
sudo apachectl configtest
sudo systemctl restart apache2

Update your /etc/hosts file with your domain and IP:

sudo nano /etc/hosts
192.168.10.15 laravelapp.howtoforge.local

Visit http://laravelapp.howtoforge.local in your browser to see the Laravel welcome page.

Conclusion

Congratulations! You have successfully installed the Laravel web framework on Ubuntu 22.04 and configured it with a LAMP stack.

Frequently Asked Questions (FAQ)

1. What is Laravel?

Laravel is a PHP-based web application framework following the MVC architecture, designed for building robust enterprise applications.

2. What prerequisites are needed to install Laravel?

You need an Ubuntu 22.04 server and a user with sudo privileges.

3. What is the purpose of Composer in Laravel installation?

Composer is a package manager for PHP that handles dependencies, making Laravel installation and maintenance easier.

4. Can I use a different database other than MariaDB?

Yes, Laravel supports databases like MySQL, PostgreSQL, SQLite, and SQL Server.

5. What are the steps to verify PHP installation?

Use the commands php --version and php -m to check PHP installation and enabled extensions.