Share your love
Best Steps to Install and Configure Laravel on Ubuntu 22.04
In this tutorial, we intend to teach you How to Install and Configure Laravel on Ubuntu 22.04.
Laravel is primarily used for building custom web apps using PHP. It’s a web framework that handles many things that are annoying to build yourself, such as routing, templating HTML, and authentication.
Laravel is entirely server-side, due to running on PHP, and focuses heavily on data manipulation and sticking to a Model-View-Controller design. A framework like React might put most of its attention on user interaction and shiny features, but Laravel simply presents a solid foundation for you to build off of—and does it right.
Steps To Install and Configure Laravel on Ubuntu 22.04
To install Laravel on Ubuntu 22.04, you need some requirements first.
Requirements
You need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 22.04.
And you need to have LAMP Stack installed on your server. To do this, you can check our article How To Install LAMP Stack on Ubuntu 22.04.
When you are done with these requirements, follow the steps below to Install and Configure Laravel on Ubuntu 22.04.
Install PHP extensions on Ubuntu 22.04
We assumed that you have installed the LAMP stack on your server, so you have PHP installed on your server. Now you need to install additional PHP extensions with the following command:
sudo apt install libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql
Here you need to create a database for the Laravel application.
Create a Database for Laravel on Ubuntu 22.04
At this point, you need to create a user and database for your Laravel. To do this, log in to your MariaDB shell with the following command:
sudo mysql -u root -p
Enter your root password you will see your MariaDB shell “MariaDB [(none)]>”.
Now create your Laravel database with the following command, here we use the laravel_db name, you can choose your own name for it:
CREATE DATABASE laravel_db;
Then, you need to create a Laravel user, here we named it laravel_user, and remember to replace the password with a strong password of your choice:
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'secretpassword';
Next grant all the privileges to your user with the command below:
GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost';
Flush the privileges:
FLUSH PRIVILEGES;
Now exit from your MariaDB shell:
QUIT;
Install Composer on Ubuntu 22.04
A composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
To install Laravel on Ubuntu 22.04, you need to install Composer.
First, download the Composer installer with the following command:
curl -sS https://getcomposer.org/installer | php
In your output you will see:
Output
All settings correct for using Composer
Downloading...
Composer (version 2.1.14) successfully installed to: /root/composer.phar
Use it: php composer.phar
As you can see this command downloads the composer.phar
file.
You need to move the file into the usr/local/bin
path with the command below:
sudo mv composer.phar /usr/local/bin/composer
Then, set the correct permissions for it:
sudo chmod +x /usr/local/bin/composer
Verify your Composer installation on Ubuntu 22.04 by checking its version:
composer --version
In your output you will see:
Ouput
Composer version 2.1.14 2021-11-30 10:51:43
Install Laravel on Ubuntu 22.04
To set up Laravel, first, switch to the webroot directory with the following command:
cd /var/www/html
Then, use the Composer command to install Laravel on Ubuntu 22.04:
sudo composer create-project laravel/laravel laravelapp
This command will create a directory called laravelapp and install all the files and directories for Laravel.
Now change the ownership of the Laravel directory and set the correct permissions with the commands below:
# sudo chown -R www-data:www-data /var/www/html/laravelapp
# sudo chmod -R 775 /var/www/html/laravelapp/storage
Next, switch to your laravelapp directory:
cd laravelapp
Verify your Laravel installation by using the following command on Ubuntu 22.04:
php artisan
In your output you will see:
Output
Laravel Framework 8.76.2
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
...
Configure Apache to Work with Laravel
At this point, you need to set up Apache to host the Laravel site. To do this, you need to create a virtual host file with your favorite text editor, here we use vi:
sudo vi /etc/apache2/sites-available/laravel.conf
Add the following contents to the file:
<VirtualHost *:80>
ServerName example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Note: Remember to replace the example.com ServerName directive with the FQDN or public IP of the server.
When you are done, save and close the file.
Here use the following commands to enable the Laravel site and Apache rewrite module:
sudo a2ensite laravel.conf
sudo a2enmod rewrite
Then, restart Apache on Ubuntu 22.04 to apply the changes:
sudo systemctl restart apache2
Access Laravel Web Interface
At this point, you have learned to Install and Configure Laravel on Ubuntu 22.04. Now you can access the Laravel web interface by typing your server’s IP address in your web browser:
http://your-domain-or-server-ip-address
You will see:
That it! You are done.
Conclusion
Laravel is one of the best PHP web frameworks, but there are many other frameworks in different languages. At this point, you have learned to Install and Configure Laravel on Ubuntu 22.04.
Hope you enjoy it.
You may be interested in these articles too:
How To Install Django on Ubuntu 22.04