How to Deploy Laravel on Ubuntu 20.04

Laravel is a free and open-source PHP framework based on the Symfony framework and follows the Model-View-Controller design pattern. It comes with a set of tools and resources that help you to build powerful and modern PHP applications. It offers a rich set of features including, Eloquent ORM, Support MVC Architecture, Template Engine, Libraries & Modular, Multiple back-ends for session and cache storage, and more.

In this guide, we will show you how to deploy a Laravel on Ubuntu 20.04 server.

Requirements

  • A server running Ubuntu 20.04.

  • A root password is set up on your server.

Install LAMP Server

Laravel requires the LAMP server must be installed in your system. You can install the Apache, MariaDB, PHP and other required PHP extensions using the following command:

apt-get install apache2 mariadb-server php libapache2-mod-php php-curl php-common php-xml php-gd php-opcache php-mysql php-mbstring php-tokenizer php-json php-bcmath php-zip unzip curl git -y

After installing all the packages, edit the php.ini file and tweak some settings:

nano /etc/php/7.4/apache2/php.ini

Change the following lines:

memory_limit = 512M post_max_size = 32M upload_max_filesize = 32M cgi.fix_pathinfo=0 date.timezone = Asia/Kolkata

Save and close the file when you are finished.

Create a Database for Laravel

Laravel uses a MariaDB or MySQL as a database backend. So you will need to create a database and user for Laravel.

First, connect to the MySQL using the command below:

mysql

Once you are connected, create a database and user with the following command:

MariaDB [(none)]> CREATE DATABASE laraveldb; MariaDB [(none)]> CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'password';

Next, grant all the privileges to the laraveldb with the following command:

MariaDB [(none)]> GRANT ALL ON laraveldb.* to 'laravel'@'localhost';

Next, flush the privileges and exit from the MySQL with the following command:

MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> EXIT;

At this point, the database and the user are ready. You can now proceed to the next step.

Install Laravel

Before installing Laravel, you will need to install the Composer in your system.

First, download the composer installation file using the command below:

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

Next, run the following command to install the Composer.

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

Once installed, verify the installed version of Composer with the following command:

composer --version

You should get the Composer version in the following output:

Composer version 2.0.12 2021-04-01 10:14:59

Next, change the directory to Apache web root directory and download the latest version of Laravel using the command below:

cd /var/www/html git clone https://github.com/laravel/laravel.git

Once the Laravel is downloaded, change the directory to laravel and install all dependencies with the following command:

cd laravel composer install

Next, set proper permission and ownership to laravel with the following command:

chown -R www-data.www-data /var/www/html/laravel chmod -R 755 /var/www/html/laravel chmod -R 777 /var/www/html/laravel/storage

Next, rename the default environment file and generate the artisan key with the following command:

mv .env.example .env php artisan key:generate

Next, edit the .env file and define your database:

nano .env

Change the following lines:

APP_URL=http://laravel.example.com DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laraveldb DB_USERNAME=laravel DB_PASSWORD=password

Save and close the file when you are finished.

Configure Apache for Laravel

Next, you will need to create an Apache virtual host configuration file for Laravel. You can create it with the following command:

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

Add the following lines:

<VirtualHost *:80> ServerAdmin admin@example.com ServerName laravel.example.com DocumentRoot /var/www/html/laravel/public <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/html/laravel> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Save and close the file then enable the Laravel virtual host and Apache rewrite module using the following command:

a2enmod rewrite a2ensite laravel.conf

Next, restart the Apache service to apply all configuration changes:

systemctl reload apache2

At this point, the Apache web server is configured to serve Laravel.

Access Laravel

Now, open your web browser and access the Laravel using the URL http://laravel.example.com. You will be redirected to the Laravel default page:

Conclusion

In the above guide, you learned how to deploy a Laravel with Apache on Ubuntu 20.04. Laravel is one of the best choices for building modern, full-stack web applications. You can now start deploying your first Laravel application.


Was this page helpful?

Thank you for helping us improve!