How to Deploy Laravel Project with Apache and Linux Server

Hello Artisan

In this brand new tutorial i am going to discuss that how we can deploy laravel project with apache and linux server. Laravel deploy project on linux ubuntu server tutorial you will learn it from scratch. 

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

In this example, i will show you two ways that you can deploy your laravel application on linux server. You have to follow just some step by step instruction to do that. 

 

Table of Contents
Table of Contents 
1. Check Prerequisites
2. install git and composer on Ubuntu server
3. Clone Git Repository
4. Deploy New Laravel Project
5. Update .env file
6. Adjust Virtual Host File
7. Strat Apache Server
8. Run Project

 

Prerequisites

  • You must have a LAMP stack properly configured on your server
  • You need the root access or sudo privileges on the server.

 

If you are all right on prerequisites, you can continue to next step to deploy laravel project on linux ubuntu server.

Deploy Laravel application with Apache on Ubuntu Server

There are some 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 one 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, then we have to install git on our server. It is very 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 of all install the git and composer on our server.

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

 

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

 

Once the installation is completed, it’s time to clone the git repository containing our Larvel project. 

OPTION 1: CLONE A GIT REPOSITORY

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.

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

 

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

 

OPTION 2: DEPLOY A NEW LARAVEL PROJECT

If you choose option one, you can skip this option two. But if you want to deploy a new Laravel project on your server, you can use the composer. Execute the following commands in your DocumentRoot for deploying a new Laravel application.

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

 

Recommended : Getting Started with Git and GitHub | Complete Beginner's Guide

 

If you are using Virtual hosts in  your server, make sure that you have created a project inside the DocumentRoot of your server.

Having cloned the git repository or creating a new Laravel project, we have to create an .env file. Now, you will see that a .env.example file. Now copy the .env.example file and then update the contents inside the copied .env file.

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

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

 

We also have to create and migrate your database. Make sure that the environment variables in your .env file are correct at this point

Setup VertualHost On Linux Server

This is the final steps, now you need to adjust vertualhost in your linux server. To open your virtual host file in edit mode, execute the following command.

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

 

Make sure that you 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, you can see the updated default virtual host configuration file without comments

demo
< VirtualHost *:80 >
  ServerAdmin [email protected]
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. 

We are almost set to to test our deploying laravel app on linux server.  Now, we time to restart the Apache server to apply the changes. Execute the below command to restart the Apache server.

command
$ sudo service apache2 restart

 

After restarting the Apache server, you will be able to access your Laravel project in the browser. In this example, you have learned how to deploy laravel project app on linux server. As well as how to adjust vertualHost file.