How to Deploy Laravel Project with Apache on Ubuntu

Laravel, if you don’t know, is the most popular PHP framework to build web applications fast. You can build almost any kind of website or application with Laravel. It has many features that can help you add new functionality to your application easily. Laravel applications are scalable too! For example, Laravel allows us to connect with multiple database servers at once. It means that we can send read and write queries to different MySQL servers. You can also deploy Laravel application with Apache on your server.

Millions of applications on the internet are powered by Laravel. But there are very few articles on the internet that can explain to you how exactly you can deploy Laravel application with Apache on Ubuntu server. In this tutorial, I will show you how to do just that.

Prerequisites

You can follow this guide if you are using Ubuntu 16.04 or Ubuntu 18.04. But, there are some pre-requisites that you have to check before getting started with the tutorial.

First of all, You must have a LAMP stack properly configured on your server. If you have not configured it yet, you can follow our guides to set up Apache with PHP-FPM on your server – the optimized stack.

  1. How to set up Apache with PHP-FPM on Ubuntu 16.04
  2. How to set up Apache with PHP-FPM on Ubuntu 18.04

The second thing you need is the root access or sudo privileges on the server. We need root access or a non-root user with sudo privileges because we have to update the Apache virtual host configuration file and install some important dependencies.

If you are clear on prerequisites, continue with the guide.

Deploy Laravel application with Apache on Ubuntu

There are a few steps that we have to follow to deploy Laravel on Apache. The first one is to install all the required dependencies on the server. The second one is to clone the git repository or create a new Laravel project inside our project directory. The third one is to adjust our Virtual host configuration file to support Laravel’s directory structure.

So, Let’s get started with the first step.

Install Required Dependencies on Server

If you are going to clone a git repository, you have to install git on your server. It is easy. 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 install the git and composer on our server.

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

$ sudo apt-get update
$ sudo apt-get install git composer -y

Once the installation is complete, it’s time to clone a git repository containing your Larvel project. Or, you can also use composer to deploy a completely new Laravel project on your server. We will go through both the options in this guide.

Option 1: Clone a Git Repository

Git is the most popular version control system developed by the founder of Linux itself, Linux Torvalds. It is very easy to use and millions of developers around the world use git to manage different versions of their code. If you are managing your Laravel code with git, go to the document root of your site on your server and execute the git clone command.

In this guide, I will deploy Laravel from it’s official GitHub repository. And I am going to deploy it inside the default document root of Apache webserver. It means that I will navigate to /var/www/html and execute the git clone command, just like this.

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

For the sake of 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.

If you are using Virtual hosts on your Apache server, you can clone the repository into the DocumentRoot of your project. Once you have cloned your git repository, you can move on to the next step.

Option 2: Deploy a new Laravel Project

If you went with option one, you can skip this 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.

$ cd /var/www/html
$ composer create-project --prefer-dist laravel/laravel .

If you are using Virtual hosts on your server, make sure you are creating a project inside the DocumentRoot of your server. Once done, you can move on to the next step.

Update the ENV file and Generate an Encryption key

After cloning the git repository or creating a new Laravel project, we have to create an .env file. Usually, we copy the .env.example file and then update the contents inside the copied .env file.

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

$ cd /var/www/html
$ cp .env.example .env
$ php artisan key:generate

You also have to create and migrate your database. However, I am not including the application level deployment steps in this guide. But make sure that the environment variables in your .env file are correct at this point.

Once done, you are ready to move on to the last step of the process. In the last step, we will adjust our VirtualHost file as per Laravel’s directory structure.

Adjust the VirtualHost file

Laravel is tricky. It is because the main index.php file of the project resides in the public directory of the project. It means that we have to update our virtual host in such a way that it should route the traffic into the public directory inside our project directory.

It’s easy too! We just have to add /public at the end of the DocumentRoot and at other places in the virtual host file where we have to define the document root of our project. In this demonstration, I am using the Apache’s default virtual host and the default DocumentRoot. But you have to perform the same steps in case of custom document root too!

To open your virtual host file in edit mode, execute the following command.

$ sudo nano /etc/apache2/sites-available/000-default.conf

Do not forget to replace 000-default.conf with the name of your virtual host file if you are using multiple virtual hosts on your server. Now, add /public at the end of the document root so that Apache will route all the traffic inside the /public route that holds the main index.php file of our project.

For example, Here is the updated default virtual host configuration file without comments.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After updating your virtual host file, press CTRL+X followed by Y followed by the Enter key to save the updated virtual host file. Now, we have to restart the Apache server to apply the changes. Execute the following command to restart the Apache server.

$ sudo service apache2 restart

After restarting the Apache server, you will be able to access your Laravel project in the browser.

 

Conclusion: Once you understand the process, it will become a lot easier to deploy different types of Laravel projects on your Apache server. Just the Virtual host part of the process is tricky in case of Laravel. You just have to understand why we have to perform that step with Laravel, the rest is normal and easy to understand.

Let us know if you need help to deploy Laravel project on your Apache server. You can either use the comment section given below or contact our support staff for help!

Leave a Reply