Install and run Laravel application on a remote server
(Part B)

Install and run Laravel application on a remote server (Part B)

This tutorial is a continuation of part A (Part A). In Part A, I demonstrated how to set up your server. In this part, I'll guide you through installing the Laravel application on the server.

Note: Because of the time gap between the two tutorials, the server's IP address in this tutorial differs from the one used in Part A. For this tutorial, I have created a new droplet on Digital ocean.

To install your Laravel application on the server, please follow along as we go through the following steps.

1. Create user

After setting up the droplet on DigitalOcean, SSH into the server as root, then create a new user via the Linux CLI by running the command “adduser {username}”.
adduser laravel

2. Add permissions to the user

You need to add permission and modify the created user so it can perform some functions that you want it to perform. To do this, run the command:
usermod -aG sudo laravel

The usermod command is used to modify a user. Then the -aG flag appends a group “sudo” to the “laravel” user. Sudo users can execute permission as root (superuser) using the sudo command. So, in essence, with this command, you want to allow the “laravel” user to execute commands with the superuser privileges.

3. Login to the new user
Login to the new user with the command below:
su - laravel

4. Install Nginx
Nginx is a high performance web server for serving websites and applications that require high performance and availability. Nginx will be used as the server for this Laravel application.

To install, run the command:
sudo apt update
The command above will update the package index in linux and ensure that you get the most current information about the repositories listed in your system sources.

sudo apt install nginx

The command above will actually install Nginx on the server.

Note: There are other alternatives like Apache, Tomcat, LiteSpeed etc that can be used in place of Nginx.

5. Configure firewall

To do this, run the command:
sudo ufw allow 'Nginx HTTP'

By running this command, you are allowing incoming HTTP traffic to your server, which is essential for serving web content using Nginx.

6. Install PHP
As you all know by now, Laravel is a PHP framework. So, to get your Laravel app up and running, you need to have PHP installed on your server. To install PHP, run the commands below:

sudo add-apt-repository ppa:ondrej/php

This command adds the ondrej/php repository to your system.

sudo apt-get update && sudo apt-get install ca-certificates apt-transport-https software-properties-common

This command above is often run to prepare a system to securely download software from third-party repositories or over HTTPS.

I will be using PHP 8.3, the latest stable PHP version for this tutorial. To install the version run the command:

sudo apt-get install php8.3-fpm

After installation, run the command below to check the status of the PHP server:

sudo systemctl status php8.3-fpm

You need to configure Nginx to handle PHP file requests by passing them to PHP-FPM for processing. To do that, open the Nginx configuration file using the command below:

sudo nano /etc/nginx/sites-available/default

Uncomment the lines as shown in the image below:

7. Install php extensions.
Laravel core functionalities and some features rely on some PHP extensions. So, for the application to work properly, you need to install these PHP extensions. To do that, run the commands below:

sudo apt-get install -y php8.3-common php8.3-fpm php8.3-mysql php8.3-redis php8.3-zip php8.3-gd php8.3-mbstring php8.3-cli php8.3-curl php8.3-xml php8.3-bcmath

8. Install Composer

Composer is a tool for managing dependencies and packages in Laravel applications, making development faster, efficient and more organised.
To install composer, run the command below

curl -sS https://getcomposer.org/installer |php
This command will download composer installer and execute with PHP, which will install Composer to the current directory.
After installing Composer, you need to move it to a directory that’s in your System’s path to make it accessible globally. To do this run the command:

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

To see if composer has been installed and also check the version of Composer installed, run the command:

composer

That concludes this tutorial. The final sections will be covered in part three.