Table of Contents
In this article, I will take you through 8 Popular Steps to Install Laravel on Ubuntu 20.04 LTS. If you are a PHP professional then you might be aware of Laravel Framework. It is a free, open-source PHP web application framework with expressive, elegant syntax. Laravel provides a wide range of functionalities that allows developers to write simple and easy code to build scalable product. It is based on Model, View and Controller pattern, also known as MVC pattern. Laravel provides powerful tools for making large, robust applications. You can check more about Laravel on its official documentation.
Steps to Install Laravel on Ubuntu 20.04 LTS
Also Read: Best Steps to Install Gradle on Ubuntu 20.04
Step 1: Prerequisites
a) You should have a running Ubuntu 20.04
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have apt
or apt-get
utility installed in your Server.
Step 2: Update Your Server
Before installing any package it is always recommended to run update once using apt update
or apt-get update
command. This will ensure the updation of all the packages with the latest version available from Ubuntu Repo.
NOTE:
root
user to run all the below commands. You can use any user with sudo
access to run all these commands. For more information, Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.root@localhost:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 https://packages.microsoft.com/repos/edge stable InRelease
Get:3 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Ign:5 http://ppa.launchpad.net/oguzhaninan/stacer/ubuntu focal InRelease
Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:7 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease [17.5 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [283 kB]
Step 3: Install Apache Web Server
In the next step you need to install Apache Server using apt install apache2 -y
or apt-get install apache2 -y
command as shown below.
root@localhost:~# apt install apache2 -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
python3.8-minimal
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser ufw
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0
0 upgraded, 9 newly installed, 0 to remove and 199 not upgraded.
Need to get 1,820 kB of archives.
Check the status of Apache web server by running systemctl status apache2
command. By default, apache service will be in running state after successful installation. If in case it is not running you can start it by using systemctl start apache2
command or you can restart the service by using systemctl restart apache2
command.
root@localhost:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-10-18 19:29:03 IST; 2min 9s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 2246 (apache2)
Tasks: 55 (limit: 2314)
Memory: 5.1M
CGroup: /system.slice/apache2.service
├─2246 /usr/sbin/apache2 -k start
├─2249 /usr/sbin/apache2 -k start
└─2250 /usr/sbin/apache2 -k start
Oct 18 19:29:03 localhost systemd[1]: Starting The Apache HTTP Server...
Oct 18 19:29:03 localhost apachectl[2245]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the>
Oct 18 19:29:03 localhost systemd[1]: Started The Apache HTTP Server.
Step 4: Install PHP
Laravel requires PHP framework and its modules to be installed. You can use below apt install command to install all the required php modules.
root@localhost:~ # apt install php7.4-common php7.4-cli php7.4-mysql php7.4-mbstring php7.4-xml -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
php7.4-cli is already the newest version (7.4.3-4ubuntu2.6).
php7.4-common is already the newest version (7.4.3-4ubuntu2.6).
php7.4-mysql is already the newest version (7.4.3-4ubuntu2.6).
php7.4-mbstring is already the newest version (7.4.3-4ubuntu2.6).
The following NEW packages will be installed:
php7.4-xml
0 upgraded, 1 newly installed, 0 to remove and 192 not upgraded.
Need to get 97.2 kB of archives.
After this operation, 443 kB of additional disk space will be used.
Step 5: Install MariaDB Database
Next step is to create a Database for Laravel Application. We can use any of the databases like MariaDB or MySQL with Laravel app. Here we are going to use MariaDB so we will first install the MariaDB Server by using apt install mariadb-server -y
command as shown below.
root@localhost:~# apt install mariadb-server -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
python3.8-minimal
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
galera-3 gawk libaio1 libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libhtml-template-perl libreadline5 libsigsegv2 libsnappy1v5
libterm-readkey-perl mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server-10.3 mariadb-server-core-10.3 socat
Suggested packages:
gawk-doc libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mailx mariadb-test tinyca
The following NEW packages will be installed:
galera-3 gawk libaio1 libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libhtml-template-perl libreadline5 libsigsegv2 libsnappy1v5
libterm-readkey-perl mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server mariadb-server-10.3 mariadb-server-core-10.3 socat
0 upgraded, 18 newly installed, 0 to remove and 199 not upgraded.
Need to get 19.9 MB of archives.
Once MariaDB is successfully installed, you can check the service status by using systemctl status mariadb command. By default, it should be in running state after installation. But in case it is not running then you can start it by using systemctl start mariadb
command or you can even restart by using systemctl restart mariadb
command.
root@localhost:~# systemctl status mariadb
● mariadb.service - MariaDB 10.3.31 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-10-18 19:34:50 IST; 13min ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 3526 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 31 (limit: 2314)
Memory: 66.2M
CGroup: /system.slice/mariadb.service
└─3526 /usr/sbin/mysqld
You can login to the database by using mysql -u root -p
command. By default, it will have no password so you just have to press Enter when you get password prompt and then you will be logged in.
root@localhost:~# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 50
Server version: 10.3.31-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Let's create a sample database called laravel_example_db
using create database laravel_example_db
command.
MariaDB [(none)]> create database laravel_example_db;
Query OK, 1 row affected (0.001 sec)
Then we will create a user called test_user
who will have complete access of database laravel_example_db
with password given as Test123
. It can created by using query create user 'test_user'@'localhost' identified by 'Test123'
as shown below.
MariaDB [(none)]> CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'Test123';
Query OK, 0 rows affected (0.001 sec)
Now you can provide the access using grant all on laravel_example_db.* to 'test_user'@'localhost'
query.
MariaDB [(none)]> GRANT ALL ON laravel_example_db.* TO 'test_user'@'localhost';
Query OK, 0 rows affected (0.001 sec)
Then flush the privileges to update the changes and exit the prompt by using quit
command.
MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> quit Bye
Step 6: Install Composer
In the next step you need to install Composer using curl -sS https://getcomposer.org/installer | php command as shown below. You can check 20 Useful curl command in Linux with Examples | How to Use curl in Linux to know more about curl tool.
root@localhost:~# curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...
Composer (version 2.1.9) successfully installed to: /root/composer.phar
Use it: php composer.phar
Then move the composer.phar
file to /usr/local/bin/composer
using mv composer.phar /usr/local/bin/composer
command.
root@localhost:~# mv composer.phar /usr/local/bin/composer
Now we need to assign execute permission to make it executable. This can be done by using chmod +x /usr/local/bin/composer command as shown below.
root@localhost:~# chmod +x /usr/local/bin/composer
It is always recommended to use composer as non-root user or you can even continue with the root user. Here we are going to use root user to run all composer command. So to check the current composer version you need to run composer --version
command as shown below. As you can see the current version is 2.1.9
.
root@localhost:~# composer --version Do not run Composer as root/super user! See https://getcomposer.org/root for details Continue as root/super user [yes]? yes Composer version 2.1.9 2021-10-05 09:47:38
Step 7: Install Laravel
In the next step, you need to go to Apache default root directory /var/www/html
using cd /var/www/html
command.
root@localhost:~# cd /var/www/html
Then to install Laravel, you need to first create a new Laravel project using composer create-project laravel/laravel exampleapp
command. Please feel free to use any name instead of exampleapp
as per your requirement.
root@localhost:/var/www/html# composer create-project laravel/laravel exampleapp Do not run Composer as root/super user! See https://getcomposer.org/root for details Continue as root/super user [yes]? yes Creating a "laravel/laravel" project at "./exampleapp" Installing laravel/laravel (v8.6.3) - Installing laravel/laravel (v8.6.3): Extracting archive Created project in /var/www/html/exampleapp > @php -r "file_exists('.env') || copy('.env.example', '.env');" Loading composer repositories with package information https://repo.packagist.org could not be fully loaded (The "https://repo.packagist.org/p2/symfony/polyfill-php56~dev.json" file could not be downloaded: failed to open stream: Cannot assign requested address), package information was loaded from the local cache and may be out of date Updating dependencies Lock file operations: 111 installs, 0 updates, 0 removals - Locking asm89/stack-cors (v2.0.3) - Locking brick/math (0.9.3) ......................................................................... Package manifest generated successfully. 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 No publishable resources for tag [laravel-assets]. Publishing complete. > @php artisan key:generate --ansi Application key set successfully.
Step 8: Configure Apache
Then you need to configure Apache Server to serve requests to Laravel site. This can be done by creating a Laravel configuration file called laravel.conf
using our favorite nano
editor under /etc/apache2/sites-available
directory and then add the details as shown below. Here you need to provide the details of your own ServerName which resolves to an IP Address.
root@localhost:~# nano /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerName exampleapp.com
ServerAdmin admin@exampleapp.com
DocumentRoot /var/www/html/exampleapp/public
<Directory /var/www/html/exampleapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then go to /etc/apache2/sites-available
directory and enable the Laravel site using a2ensite laravel.conf
command.
root@localhost:/etc/apache2/sites-available# a2ensite laravel.conf
Enabling site laravel.
To activate the new configuration, you need to run:
systemctl reload apache2
Then enable the rewrite module using a2enmod rewrite
command.
root@localhost:/etc/apache2/sites-available# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
Finally restart apache2 service to deploy the new configuration using systemctl restart apache2
command.
root@localhost:/etc/apache2/sites-available# systemctl restart apache2
Open your Browser and enter URL exampleapp.com
to access Laravel home page. You should be able to see the Laravel Home Page.