How I deployed laravel Realworld Example App on debian 11

If you are new to DevOps and Cloud just like me, you can use the Laravel Realworld Example App to test your knowledge of deployment. Laravel is a php Framework.

Although, it took me 2 days to learn the process of deploying this app, it was worth my time of research. And I had butterflies in my tommy, when I finally succeeded.

I have written below the several steps I took in achieving my success, in a straightforward terms.

Steps to Deploying a laravel app cloned from a github repository in Debian 11

Prerequisites

  • You need a VM instance(I will take you through the steps of creating one)
  • You will need to install the following(The various steps will be stated below)
    • Git
    • Apache2
    • php8.0 and its dependencies
    • mysql and its apt repository
    • Composer
  • After installing, you will need to do some configurations and then you will be good to go.

Creating a VM instance with a cloud provider

I used AWS, but it is easier and straightforward to use other cloud providers like Linode and Digital Ocean. I will put you through how to use LInode. Follow the links below to create an account with any of the network providers of your choice

  • Linode, you will be given 100$ free credit as a new user

Installing the necessary applications(Donot forget to add sudo to run all your commands if you aren't using root

First we are going to update our apt using the command below

apt-get update -y

Install Git in our new launched VM to enable us clone or pull a repositiory(the laravel repo). Enter the commands below to install and configure git.

$ apt install git -y
$ git config --global user.name "first last"

$ git config --global user.email ""

Then lets install Apache2 using the command below:

$ sudo apt install apache2

Once installed, Apache should be running. If it’s not, for whatever reason, start it:

$ sudo systemctl start apache2
Install PHP and PHP extensions for Laravel Application. By default, PHP 8.1 is not included in the Debian 11 default repository. So you will need to add the DEB.SURY.ORG repository to APT.

First, install the required packages using the following command:

$ sudo apt-get install ca-certificates apt-transport-https software-properties-common -y

Once all the packages are installed, add a Sury repository to APT using the following command:

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list

Install Gnupg using the command below: $ apt-get install gnupg

Next, download and add the GPG key with the following command:

$ wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -

Once you are done, update the repository with the following command:

$ apt-get update -y

Now, you can install the PHP 8.1 using the following command:

$ sudo apt-get install php8.1 libapache2-mod-php php8.1-dev php8.1-zip php8.1-curl php8.1-mbstring php8.1-mysql php8.1-gd php8.1-xml

Verify if PHP is installed using the command below. $ php -v

Install Mysql server

$ apt-get install mysql-server

Check the status of mysql server

$ systemctl status mysql

Login into your server

$ mysql -u root -p

List the available databases

$ SHOW DATABASES;

Create your Database

$ CREATE DATABASE "name of database";

exit

Install Composer To download Composer, run the following command:

$ curl -sS https://getcomposer.org/installer | php

Next, move the composer file to the /usr/local/bin path.

$ sudo mv composer.phar /usr/local/bin/composer

Assign execute permission:

$ sudo chmod +x /usr/local/bin/composer

Verify the Composer version installed:

$ composer --version

Clone the git Repo

First, cd into your /var/www/html

  • Do git clone and the link of the repo

      $ git clone "github repo link"
    
  • The above command will clone the repo into that /var/www/html folder.

  • cd into the cloned folder(laravel-example-app) and list all the files using the command below

      $ cd laravel-example-app
      $ ls -al
    
  • Rename the .env.example file to .env using the command:

      $ mv .env.example .env
    
  • Edit the .env file

      $ vi .env
    

Edit only the following

DB_URL=http://"your server IP"
DB_PORT= "3306"
DB_USERNAME= "the username you used when logging into your mysql server"
DB_HOST="Your locallhost IP"
DB_DATABASE= "name of the database you created"
DB_PASSWORD= "the mysql password you created while downloading"

Save and close the file.

Using composer install the repository by typing the command below

$ sudo composer install

Check the version installed using the command

$ php artisan

Generate a key for the app

$ php artisan generate key
$ php artisan migrate

Edit the apache2 config file

  • Create your own config file for the app

    $ cd /etc/apache2/sites-available

  • Create your config file Input and edit the code below:


ServerName your-domain.com
ServerAdmin
DocumentRoot /var/www/html/laravelapp/public


Options +FollowSymlinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined

Save the file and exit

Enable the laravel virtual host.

$ sudo a2ensite laravel.conf

Restart the Apache web server.

$ sudo systemctl restart apache2

Note, make sure you use your server Ip address as your domain name to test the app.

Kudos! You have successfully deployed an app

You might encounter a 404 error, not to worry because your deployment is correct. Note, getting the 404 error, doesn't mean your deployment is wrong. But if you want to see the web interface of the app you just deployed, you can go further.

Follow the steps below to see the page:

cd into the folder that has the cloned repo, enter the command below;

$ cd routes

Edit the web.php file

$ vi web.php

Uncomment the session of the code that starts with "Routes"

Save and exit

Restart your Apache2 and refresh your browser.

Note that in a real working environment, the above step is not neccessary

Conclusion

Deploying an app for the first time can be really difficult and you are bound to see errors in the process. All you need to do is make sure every prerequisites and dependencies is installed. Follow the steps carefully and you will be successful.

you can also see the various links below to learn more:
dev.mysql.com/doc/mysql-getting-started/en
dev.mysql.com/doc/mysql-apt-repo-quick-guid..
mysqltutorial.org/mysql-create-database
linuxtuto.com/how-to-install-laravel-9-on-d..

Credits

  • Big credit to my mentor ValentineMadu for staying with me through the process.
  • AltSchool of Engineering for the stretch