DEV Community

Cover image for How to Deploy Laravel Project with Apache on Ubuntu
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

How to Deploy Laravel Project with Apache on Ubuntu

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

cd /var/www/html
nano .env
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

sudo service apache2 restart
Enter fullscreen mode Exit fullscreen mode

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

Thank you for reading this blog.

Top comments (2)

Collapse
 
peter279k profile image
peter279k

Another tip is about Composer installation.

  • In the original post, using the sudo apt-get install composer to install that. However, this command will install the outdated version because this version is available on Ubuntu mirror by default:
peterli@peterli-Virtual-Machine:~$ sudo apt-cache policy composer
[sudo] password for peterli:
composer:
  Installed: (none)
  Candidate: 1.10.1-1
  Version table:
     1.10.1-1 500
        500 http://tw.archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        500 http://tw.archive.ubuntu.com/ubuntu focal/universe i386 Packages
peterli@peterli-Virtual-Machine:~$
Enter fullscreen mode Exit fullscreen mode
  • By default, the curl command is not available on Ubuntu operating system, we should use the sudo apt-get install curl command to install this firstly:

  • To fix above issue, we can use the curl command to download latest Composer versions:

peterli@peterli-Virtual-Machine:~$ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer (version 2.2.3) successfully installed to: /home/peterli/composer.phar
Use it: php composer.phar

peterli@peterli-Virtual-Machine:~$ php ./composer.phar --version
Composer version 2.2.3 2021-12-31 12:18:53
peterli@peterli-Virtual-Machine:~$
Enter fullscreen mode Exit fullscreen mode
  • If we want to download latest Composer V1 version, we can use following method:
peterli@peterli-Virtual-Machine:~$ curl -sS https://getcomposer.org/installer | php -- --1
All settings correct for using Composer
Downloading...

Composer (version 1.10.24) successfully installed to: /home/peterli/composer.phar
Use it: php composer.phar

peterli@peterli-Virtual-Machine:~$ php ./composer.phar --version
Composer version 1.10.24 2021-12-09 20:06:33
peterli@peterli-Virtual-Machine:~$
Enter fullscreen mode Exit fullscreen mode
  • If want to use the composer command rather than using the php /path/to/composer.phar command, we can use following command:
peterli@peterli-Virtual-Machine:~$ sudo mv composer.phar /usr/local/bin/composer
[sudo] password for peterli:
peterli@peterli-Virtual-Machine:~$ composer --version
Composer version 1.10.24 2021-12-09 20:06:33
peterli@peterli-Virtual-Machine:~$
Enter fullscreen mode Exit fullscreen mode
  • Please refer this link to know more details about Composer installation :).
Collapse
 
peter279k profile image
peter279k

Some recommended tips are as follows:

  • The systemctl command is available since Ubuntu 18.04 LTS is released. We can consider using above command because it's more friendly and powerful than using the service command.
  • According to the Apache installation tutorial post, it should not be secure.

We may be under the vulnerability risk when using the old Apache version.

It will install the version that is default on Ubuntu 20.04 mirror server.

We can consider following Bash snippets:

peterli@peterli-Virtual-Machine:~$ sudo apt-cache policy apache2
apache2:
  Installed: (none)
  Candidate: 2.4.41-4ubuntu3.8
  Version table:
     2.4.41-4ubuntu3.8 500
        500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
     2.4.41-4ubuntu3.6 500
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
     2.4.41-4ubuntu3 500
        500 http://tw.archive.ubuntu.com/ubuntu focal/main amd64 Packages
Enter fullscreen mode Exit fullscreen mode
peterli@peterli-Virtual-Machine:~$ sudo add-apt-repository ppa:ondrej/apache2 -y
Hit:1 http://tw.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://tw.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://tw.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 http://ppa.launchpad.net/ondrej/apache2/ubuntu focal InRelease [23.8 kB]
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:6 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease
Get:7 http://ppa.launchpad.net/ondrej/apache2/ubuntu focal/main amd64 Packages [4,784 B]
Get:8 http://ppa.launchpad.net/ondrej/apache2/ubuntu focal/main i386 Packages [4,772 B]
Get:9 http://ppa.launchpad.net/ondrej/apache2/ubuntu focal/main Translation-en [3,252 B]
Fetched 36.6 kB in 3s (14.4 kB/s)
Reading package lists... Done
peterli@peterli-Virtual-Machine:~$ sudo apt-cache policy apache2
apache2:
  Installed: (none)
  Candidate: 2.4.52-2+ubuntu20.04.1+deb.sury.org+1
  Version table:
     2.4.52-2+ubuntu20.04.1+deb.sury.org+1 500
        500 http://ppa.launchpad.net/ondrej/apache2/ubuntu focal/main amd64 Packages
     2.4.41-4ubuntu3.8 500
        500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
     2.4.41-4ubuntu3.6 500
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
     2.4.41-4ubuntu3 500
        500 http://tw.archive.ubuntu.com/ubuntu focal/main amd64 Packages
peterli@peterli-Virtual-Machine:~$
Enter fullscreen mode Exit fullscreen mode

As we can see, we can use the ondrej/apache2 repository to include the latest stable version and make our HTTP server secure.

And the Apache2 security vulnerability page is available here :).