How to Deploy Laravel Project with Apache on Ubuntu
Tags: laravel, apache, webdev, beginners
Published: 2022-01-03 05:18:56

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.

## 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](https://techvblogs.com/blog/install-apache-on-ubuntu-20-04)

## 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.

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

## 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.

```
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.

```
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.

```
cd /var/www/html
cp .env.example .env
php artisan key:generate
```
Next, edit the .env file and define your database:
```
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+X, Y, 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:

```
sudo nano /etc/apache2/sites-available/techvblogs.conf
```

Add the following lines:
```
<VirtualHost *:80>

    ServerAdmin admin@techvblogs.com
    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.
```
sudo a2enmod rewrite
sudo a2ensite techvblogs.conf
```
Finally, restart the Apache server to apply the changes. Run the following command:
```
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.