How to Deploy Laravel Project with Apache on Ubuntu

  Smit Pipaliya   Laravel, Ubuntu, Linux   2 weeks ago
How to Deploy Laravel Project with Apache on Ubuntu - TechvBlogs

Hello Developers,

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 tools and resources that help you build robust 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.

Laravel powered millions of applications on the internet. But there are very few articles on the internet explaining how you can deploy the Laravel application with Apache on the Ubuntu server.

Laravel with Apache is a great solution! This tutorial will show you how to install Laravel with Apache on Ubuntu.

1. Prerequisites

  • The operating system running Ubuntu Linux
  • A root or non-root user with Sudo privileges
  • Has stable internet connection
  • Terminal window / Command line

2. Install Apache On Ubuntu

If you have installed Apache, you can skip this. If you have not installed Apache, then you click on this link: Install Apache on Ubuntu 20.04 LTS

3. Install Composer On Ubuntu

There are a few steps that we can follow to deploy Laravel on Apache. The first one is to install all the required dependencies on the server. The second is to clone the git repository or create a new Laravel project inside our project directory.
So, Let's get started with the first step.

If we are going to clone a git repo, we have to install git on our server. It is straightforward. And, we also have to install composer on the server because we have to install Laravel's dependencies using composer update or composer install command. So, Let's, first of all, install the git and composer on our server.

Execute the following commands to install git and composer on the Ubuntu server.

#! /bin/bash
sudo apt-get update
sudo apt-get install git composer -y

Read Also: How to Manage and Use Apache virtual hosts in Ubuntu

4. Install Laravel

Option 1: Clone a Git Repository

Git is a free and open-source distributed version control system designed to handle small and extensive projects with speed and efficiency.

Git is the most popular version control system developed by Linux itself, Linus Torvalds. It is straightforward to use, and millions of developers worldwide use git to manage different versions of their code. If you are working your Laravel code with git, go to your site's document root on your server and execute the git clone command.

I will deploy it inside the default document root of the Apache webserver. It means that I will navigate to /var/www/html and execute the git clone command, just like this.

#! /bin/bash
cd /var/www/html
git clone https://github.com/laravel/laravel.git .
composer install

For example, I am cloning the Laravel project from the official git repository of Laravel. You can clone your project from the repository you desire. Just replace the URL, and you are good to go. We added the "." at the end of the git clone command to clone the project files in the same directory.

Option 2: Deploy a new Laravel Project

If you choose the first option, you can skip this second option. But if you want to deploy a brand new Laravel project on your server, you can use the composer. Execute the following commands in your DocumentRoot to deploy a new Laravel project.

#! /bin/bash
cd /var/www/html
composer create-project --prefer-dist laravel/laravel .

5. Update ENV File and Generate An Encryption Key

To copy the file from .env.example to .env and generate an encryption key, run the following commands.

#! /bin/bash
cd /var/www/html
cp .env.example .env
php artisan key:generate

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

#! /bin/bash
cd /var/www/html
nano .env

Change the following lines:

APP_URL=https://techvblogs.com

DB_CONNECTION=mysql
DB_HOST=YOUR_DB_HOST
DB_PORT=3306
DB_DATABASE=techvblogs
DB_USERNAME=admin
DB_PASSWORD=YOUR_PASSWORD

After updating your .env file, press CTRL+XY, and Enter key to save the .env file.

6. Configure Apache for Laravel

Laravel is tricky. It is because the main index.php file of the project lives in the project's public directory. It means that we have to update our virtual host so that it should route the traffic into the public directory inside our project directory.

Next, We will need to create an Apache virtual host configuration file for Laravel. Run the following command:

#! /bin/bash
sudo nano /etc/apache2/sites-available/techvblogs.conf

 Add the following lines:

<VirtualHost *:80>

    ServerAdmin [email protected]
    ServerName techvblogs.com
    DocumentRoot /var/www/html/public

    <Directory /var/www/html/public>
       Options +FollowSymlinks
       AllowOverride All
       Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

After updating your virtual host file, press CTRL+X, Y, and Enter key to save the updated virtual host file.

Next, activate the Apache rewrite module and Laravel virtual host configuration using the following command.

#! /bin/bash
sudo a2enmod rewrite
sudo a2ensite techvblogs.conf

Finally, restart the Apache server to apply the changes. Run the following command:

#! /bin/bash
sudo service apache2 restart

 If your Apache server successfully restarts, you will be able to access your Laravel project in the browser.

Thank you for reading this blog.

Read Also: Top Laravel Interview Questions & Answers 2022

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
Laravel Developer