Programming

How to Install Laravel 7 on Ubuntu 20.04

In this tutorial, we will show you how it is easy to install Laravel 7 on Ubuntu 20.04.

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller architectural pattern and based on Symfony.

Prerequisites

Step 1 – Keep the server up to date

# yum update -y

Step 2 – Install software dependencies

# apt install -y php-mbstring php-xml php-fpm php-zip php-common php-fpm php-cli unzip curl nginx

Step 3 – Install Composer

# curl -s https://getcomposer.org/installer | php

# mv composer.phar /usr/local/bin/composer

Verify the composer installation using following command:

# composer diagnose

Output:

Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0 87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B 0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 1.10.10
PHP version: 7.4.3
PHP binary path: /usr/bin/php7.4
OpenSSL version: OpenSSL 1.1.1f 15 Oct 2020

Step 4 – Install MariaDB

# apt install -y mariadb-client mariadb-server

Start and enable the mariadb.service using following command:

# systemctl start mariadb.service

# systemctl enable mariadb.service

When we install Mariadb, root password set to blank. We need to set a root password and configure some settings. To do it run following command and configure it by answering some of the security questions.

# mysql_secure_installation

Step 5 – Install Laravel

Replace example with your project name where it appears throughout this guide.

1. Create a Laravel project with Composer. You may disregard the warning not to run Composer as root for this step. See the Composer documentation for details.

# cd /var/www/html
# composer global require laravel/installer
# composer create-project –prefer-dist laravel/laravel example

2. Install the example project.

# composer install

3. Test the Laravel application manually in the /var/www/html/example folder. Replace the example IP address with your server’s IP.

# cd /var/www/html/example
# php artisan serve –host=127.0.0.1 –port=8000

Step 6 – Configure Nginx

1. Set the file permissions. Replace example with your Laravel project name.

# chmod -R 755 /var/www/html/example

# chown -R nginx:nginx /var/www/html/example

2. Create an Nginx configuration file.

# vi /etc/nginx/sites-available/example

3. Paste the following to your example configuration file.

If you installed a different version of PHP, edit the /var/run/php/php7.4-fpm.sock value for your version. You can find your sock file in /var/run/php/.

server {
listen 80;
server_name example.com;
root /var/www/html/example/public;

add_header X-Frame-Options “SAMEORIGIN”;
add_header X-XSS-Protection “1; mode=block”;
add_header X-Content-Type-Options “nosniff”;

index index.php;

charset utf-8;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.(?!well-known).* {
deny all;
}
}

4. Enable the Nginx configuration.

# ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/

5. Test the Nginx configuration

# nginx -t

If there is no error after running above command restart nginx service.

# systemctl restart nginx

Test that your Laravel application loads properly in a web browser.

Step 7 – Configure MariaDB

Log in to MariaDB as root.

# mysql -p -u root

Create a database named laravel.

MariaDB [(none)]> CREATE DATABASE `laravel` CHARACTER SET utf8 COLLATE utf8_general_ci;

Create a database user.

MariaDB [(none)]> CREATE USER ‘laraveluser’@’%’ IDENTIFIED BY ‘password’;

Grant permissions.

MariaDB [(none)]> use laravel;
MariaDB [laravel]> GRANT ALL ON `laravel.*` TO ‘laraveluser’@’%’;
MariaDB [laravel]> FLUSH PRIVILEGES;
MariaDB [laravel]> EXIT;

Edit the Laravel application .env file.

# vi /var/www/html/example/.env

Set the database connection variables.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=password

Save and exit the file.

We have successfully install and configured Laravel.

In this tutorial we have seen how it is easy to install Laravel 7 on Ubuntu 20.04.

Related Articles