DEV Community

Cover image for Deploying Laravel on Lamp Stack with Linux(Ubuntu)
Josephat Kene
Josephat Kene

Posted on • Updated on

Deploying Laravel on Lamp Stack with Linux(Ubuntu)

Table of Content

  1. Introduction
    • Lamp Stack
    • Laravel
  2. Prerequisites

  3. Steps of Deploying Laravel with Lamp

    • Installing Lamp
    • Installing PHP Dependencies
    • Downloading Composer
    • Cloning the Laravel Git Repository and installing Dependencies
    • Installing Composer Dependencies and Setting up Environment Variables
    • Setting Up MySQL Database
    • Reconfigure Apache
  4. Conclusion

Introduction

There is a cool website that is being built and you are tasked with deploying the website on a Linux server and to make the website work, you have to install Lamp stack.

In this article i will guide you through the steps of manually setting up laravel with lamp stack. By the end of this article, you will know the steps of doing that and what the commands actually mean or do and why you had to run some of the commands.

Lamp Stack
Lamp is a web development platform that consist of four main components. They are: Linux, Apache, MySQL, PHP. You can say LAMP is an acronym for those four components.

Linux: This is the Operating System which the other 3 components of the stack runs. It is chosen because of its security, stability and it is open source. It’s a good foundation to host web applications.

Apache: This is the web server software that handles requests from your web browsers and responds to it. Basically, when you open a web browser and search for a website, your web browser makes a request for that website and you get your desired web page (that is the response). That whole process is handled by apache.

MySQL: This is a database management system that is used for storing, sorting and managing the web application’s data like usernames, passwords etc. These are all sorted and managed by MySQL.

PHP: This is the server-side scripting language for developing dynamic web pages. Can be called backend side. It helps the server that is hosting the web application understand what and it should do when someone visits the web application page.

Laravel
This is an open source PHP framework used for web development, providing rich sets of features for efficient and structured way of building web applications.
It is basically like a set of instruction template or booklet that web developers get to follow or use in organizing the ready to use tools for building web applications so they don’t get to start from scratch every time.

Prerequisites

  • A Linux(Ubuntu) Operating System or a virtual machine running the Ubuntu OS.

  • A user on your Linux OS with sudo privileges or a root user

  • Terminal window or command line (i prefer git bash)

  • Knowledge of some linux command

Steps of Deploying Laravel with Lamp Stack

- Installing Lamp

The first thing to is to update your Linux server. It is best practice to always update so you can get recent packages on your server. You can also upgrade the packages if you want. It is like updating your windows OS.

So run sudo apt update for updating your Linux packages. And sudo apt upgrade to upgrade the packages

The next step is to install the Linux, Apache, MySQL and PHP.

Now the command to install Apache is:
sudo apt install apache2 -y

You can check if apache is active by using the command:
sudo systemctl status apache2

Or you could enter the IP of your virtual machine on your browser, and you should get the picture below which is the apache default page

Image of apache default page

To Install MySQL, the command to run is:
sudo apt install mysql-server -y

Now for PHP, you need to install the PHP version 8.2 or a higher version because that is the version Laravel works with, and by default the sudo apt install module installs a version of PHP that is lower than PHP8.2.
So to get the PHP version 8.2 command you need to first run the command:
sudo add-apt-repository ppa:ondrej/php
This command adds a Personal Package Archive (PPA) maintained by Ondřej Surý, which contains PHP packages, to the system's list of repositories. This enables the system to install PHP8.2 packages from this PPA using the apt package manager.

After this, you can now install PHP8.2 with the command
sudo apt install php8.2 -y

- Installing PHP Dependencies

Now Laravel needs some dependencies to run on your PHP and it doesn’t come when you install the PHP8.2 so you have to install those dependencies.

To check the default dependencies you have, run the command:
php -m
The output below are the dependencies that are installed with php8.2
image of PHP8.2 dependencies already installed

To install the other dependencies needed, run the command:
sudo apt install php8.2-curl php8.2-dom php8.2-mbstring php8.2-xml php8.2-mysql zip unzip -y

- Downloading Composer

Why Download Composer?
Remember those PHP dependencies you installed? Composer is the tool used in managing them, ensuring that the correct version of these packages are installed and compatible with each other. It generates autoloader for your project. This autoloader allows Laravel load classes and files automatically.

First step to installing composer is to change directory into an executable path. You do that by running the command
cd /usr/bin

Then you run the command below which uses the curl command that transfers data from and to a server to download the Composer installer script from the official Composer website (https://getcomposer.org/installer), uses the grep symbol (|) to pipe it to sudo php and then executes it. The -sS means its should run in silent mode and show error if it occurs.
curl -sS https://getcomposer.org/installer | sudo php

The result of the is you having a composer.phar file. For better readability and usability you should renamed it to just composer with the command below:
sudo mv composer.phar composer

- Cloning the Laravel Git Repository and installing Dependencies

First you want to run these next commands in your home directory so you change back to your home directory with the command:
cd

Considering you will be hosting the Laravel on Apache, you navigate to our default apache directory hosting the default apache page with the command:
cd /var/www/html

This path is where you will clone your Laravel repository, so it’s best you remove the content html directory and change the owner of the directory to your current user (you could just clone the repository on that directory without removing the contents of the html though) with the next 2 commands:
sudo rm -r *
sudo chown -R $USER:$USER /var/www/html

Now to actually clone the git repository, you need to be sure you are in the /var/www/html directory. You can run the command cd /var/www/html to be sure.

Then clone with the command:
git clone https://github.com/laravel/laravel

It is important you do not run the command above with sudo because it changes the ownership of the cloned repository to root. Which should not happen considering we changed the owner to the current user. You only use sudo if you are running the command as a root user.

- Installing Composer Dependencies and Setting up Environment Variables

Next set of command should be executed in the Laravel directory so run the command
cd laravel/

Now, install composer managed dependencies with the command
sudo composer install --optimize-autoloader --no-dev

This command above tells composer to install the dependencies listed in the composer.json file. It reads the json file, resolve dependencies and the required packages into the vendor directory.
The –optimize-autoloader tells composer to generate an optimized autoloader for better performance and --no-dev tells composer to exclude development dependencies to reduce the size of installed packages and speed up installation.
Now you have installed the environment variables, they are saved in the env.example file and this application has been configured to read variables from the .env file. So the next step is to copy the .env.example file to the .env file (you can also rename instead by using mv instead of cp) with the command
sudo cp .env.example .env

The command above should be executed in the Laravel directory
Now if you open the .env file with a code editor, you’ll see that the APP_KEY part of the configuration is empty.
Image of APP_KEY

To populate it, execute the command:
sudo php artisan key:generate

The application key is important because is used for encrypting various types of data, like cookies and passwords. Without a proper key, your application's security could be compromised.
The image below shows the generated app key after running the command to generate it
Image of app key genrated

In many web server configurations, the web server process for apache runs as a specific user for security reasons. To get that user for apache, execute the command below
ps aux | grep “apache” | awk ‘{print $1}’ | grep -v root | head -n 1

The result of command above is used to change the ownership of laravel’s storage directory and the bootstrap/cache directory so that apache is able to read into it and avoid permission issues. The commands to do these are the next commands to execute. Run them one after the other

sudo chown -R www-data storage
sudo chown -R www-data bootstrap/cache

- Setting Up MySQL Database

Setting up a database for deploying Laravel is important because Laravel utilizes a database to store and retrieve application data.
Some of the reasons why you need to set up a database are for storing and retrieving data, data security and scalability.
So for Laravel to work properly you have to create a new database. And doing that means you have to create a database name, user and password.
To do that you should run the following commands one after the other:
sudo mysql -uroot
CREATE DATABASE database_name;
CREATE USER 'Username'@'localhost' IDENTIFIED BY 'Password';
GRANT ALL PRIVILEGES ON *.* TO 'Username'@'localhost';
SHOW DATABASES;
exit

N.B: Replace “database_name”, “Username” and “Password” with your preferred names for these values
The output will look like the picture below if you did it correctly:
Database setup success

Remember that .env file, you have to populate it with the values of the database details you created. Change the value of DB_CONNECTION to “mysql”, The values of the DB_DATABASE, DB_USERNAME, DB_PASSWORD to the names of the database, username and the password of the database you created. Run the command;
sudo vim .env
The image below is the part of the .env file you should populate
Image of the env file before

After making the changes, putting the values of your database name, username and password, the .env file should look like this:
Image of .env file after

To confirm that the .env file configuration and the Laravel application are in sync, run the command
php artisan migrate
The output should be the picture below
Image of artisan migrate success

- Reconfigure Apache

To access the Laravel page directly from the IP of your virtual machine, or the local host or assigned domain name, you have to reconfigure apache.

The default apache configuration is located in the sites-available file. So change directory to the path of the sites-available file with the command:
cd /etc/apache2/sites-available/

Then you create a new configuration file with the command:
sudo vim Laravel.conf

Populate it with the new configuration file with the details below:
<VirtualHost *:80>
ServerName webmaster@localhost
DocumentRoot /var/www/html/laravel/public
<Directory /var/www/html/laravel>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/demo-error.log
CustomLog ${APACHE_LOG_DIR}/demo-access.log combined
</VirtualHost>

Next thing to do is to disable the default configuration of apache with the command:
sudo a2dissite 000-default.conf

Now enable the Laravel configuration file you created with the command:
sudo a2ensite Laravel.conf

For your new configuration to kick in, restart apache with the command:
sudo systemctl reload apache2

N.B: If you are getting any errors when you try to reload apache, check the laravel.conf file you created and make sure the configuration lines are well indented.
The configuration should look like the picture below

Laravel configuration

Now when you enter the IP of your virtual machine in your browser, your output would look like the picture below.

Image of laravl page

Conclusion

Now you can manually deploy Laravel with lamp stack on an ubuntu server or an ubuntu virtual machine. Also knowing why you had to use some of the commands that were complicated.
This process can be automated with bash script also which is on this Git Repository. I automated this process with bash script and ran it on a slave node using ansible.

Top comments (1)

Collapse
 
nathan_simpson_224e720069 profile image
Nathan Simpson

Great write up, minor issue is you’re using servername which should be the FQDN of the machine and serveradmin is where you put your email address.