Ugacomp

How to host a Laravel application on Ubuntu using Apache

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Laravel is a powerful PHP framework that allows you to build PHP applications with easy. To host a Laravel application on Ubuntu using Apache Web server, you need to go through the following steps:

Install Apache Web Server

Before we run the apache installation command, let’s first update the system using the update command as seen below;

sudo apt update

Now that our systen is updated, we need to run the following command to install Apache web server:

sudo apt install apache2

After installing apache, it will automatically start to run by default. But to be sure if apache is running on the system, you can check the status using the following command:

sudo systemctl status apache2

If Apache server is running, you will be able to see something like this on your terminal. See the image below:

How to host a Laravel application on Ubuntu using Apache

If apache is not running, then you need to start it by using the following command:

sudo systemctl start apache2

You can also run the following command to ensure that apache starts on system boot:

sudo systemctl enable apache2

Configure Firewall to allow HTTP & HTTPS

Apache server uses HTTP for unecrypted web traffic and HTTPS for encrypted traffic. These two protocols require that port 80 & 443 are opened through the firewall respectively. Port 80 is used for HTTP and port 443 is for HTTPS.

RECOMMENDED READING: How to install and configure UFW firewall on Ubuntu Linux

In this setup, we will be using UFW firewall. By default, UWF firewall comes preinstalled on most Linux systems, and you can check its status using the following command:

service ufw status

If it’s running, you will be able to see something like this on your terminal:

ufw.service - Uncomplicated firewall
     Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2023-04-03 08:43:06 UTC; 1 weeks 3 days ago
       Docs: man:ufw(8)
    Process: 442 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=0/SUCCESS)
   Main PID: 442 (code=exited, status=0/SUCCESS)

Open Port 80 for HTTP

To use UFW to open port 80 for HTTP traffic, run the following command:

sudo ufw allow 80

Open Port 443 for HTTPS

You will need to open this port as it’s necessary after installing SSL certificate on your server. So, to use UFW to open port 443 for HTTPS, run the following command:

sudo ufw allow 443

Check the status of the firewall

Confirm if the firewall rules you implemented are working perfectly, check the UFW status using the following comand:

sudo ufw status

The output of the above comand will as seen below:

To                         Action      From
--                         ------      ----
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere

RECOMMENDED READING: How to deploy WordPress on a Ubuntu LAMP Server

Install PHP & related packages

Since Laravel is a PHP framework, it requires PHP to be installed on the system. To install PHP on Ubuntu, run the following command:

sudo apt install php

To comfirm if PHP is installed and running on the system, use the following command:

php -v

If PHP is running, you will be able to see something like this on your terminal. See the image below:

RECOMMENDED READING: How to fix Server crashes when running “composer update”

Install PHP Modules

PHP modules are extensions or libraries that can be dynamically loaded into the PHP runtime to provide additional functionality. We need to install a couple of these modules because they’re critical to Laravel applications. We can use the following command to install the required modules;

sudo apt install php-mbstring php-mysql php-curl php-cli php-dev php-imagick php-soap php-zip php-xml php-imap php-xmlrpc php-gd php-opcache php-intl

Install Database Server

Most Laravel applications take in dynamic data that’s stored in the database. This means that we need also to install a database server. In this guide, we will install a MariaDB database server by running the following command:

sudo apt install mariadb-server

We need to log into the MariaDB prompt by running the following command:

sudo mysql -u root -p

RECOMMENDED READING: How to Install WordPress on a VPS Server using Cloudron?

Create a Database

We need to create the database that will be associated with the Laravel application. This can be done using the following command:

CREATE DATABASE laravel_db;

Feel free to replace laravel_db with your preffered name of the database.

Create a Database User

A database user is an account created with credentials to access the database. The user we create will be associated with the database we just created. To create a database user, run the following comand:

CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'secretpassword';

The above comand will create a database user named ‘laravel_user‘ and will have a password ‘secretpassword‘. You can change the name of the user and also ensure that you put a more secure password other than what we’re using in this example.

Grant all Privillages to the Database User.

Granting privileges to a database user means giving that user specific rights and permissions to perform certain operations or actions on a database. We can achieve this by running the following command:

GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost';

The above comand means that the database user named laravel_user who is connecting from the local machine (localhost) wil have the privilleges to access the database named laravel_db with permission to to read, write, create, update, and delete data in the database.

RECOMMENDED READING: installer: Could not pull cloudron/base error – Suggested fix

Flush Privilleges

This is necessary to instruct the database server to reload the privilege tables from disk. Flushing privillages is needed after making changes to the privilege tables, such as granting or revoking privileges, in order for those changes to take effect. To achieve this, we can simply run the following command:

FLUSH PRIVILEGES;

You will need to exit the MariaDB terminal by running the following command:

EXIT;

Allow the Database through the Firewall

MariaDB server will be accessed locally by the Laravel application. But if we need to access it remotely in future, you will probably need to open port 3306, which is the default listening port for database servers like MySQL or MariaDB. We will run the following command to open this port:

sudo ufw allow 3306

Install Composer

Composer is a dependency manager for PHP, which means it helps manage the libraries and packages that your PHP project relies on. In simpler terms, it helps you keep track of and install the external code (libraries or packages) that your project needs to run.

RECOMMENDED READING: How to use the apt command in Linux | Syntaxes & Examples

Composer is a crucial tool for managing dependencies in Laravel and PHP projects. It simplifies the process of including external code, handles autoloading, and ensures version compatibility, making your development workflow more efficient and maintainable.

Before we proceed, let’s first install some useful tools like curl, git and unzip using the following command:

sudo apt install git curl unzip

Download Composer

On our system, we need to download and install composer by running the following command:

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

Move the composer.phar 

When you download Composer for the first time, you usually get a file named composer.phar. To use Composer globally from the command line, it’s a common practice to move the composer.phar file to /usr/local/bin directory, which is the location where executable files are commonly stored. This allows you to run Composer commands from any location in the terminal without specifying the full path to the composer.phar file. All you need to do is to run the following command:

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

Assign execute permission

Run the following command to allow the execute permission for Composer

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

To check Composer’s version, run the following command on the terminal:

composer --version

Install Laravel on Ubuntu

RECOMMENDED READING: How does Alibaba Cloud Free trial work?

We will use composer to install Laravel on the system. We need to first navigate to /var/www directory which is the webroot directory of the Laravel applaction we would like to host: To achieve this, we use the following command:

cd /var/www/html

Create Laravel project

 We will now install Laravel using the composer create project command. We will also specify the name of the project which will be called, ugacomp or you can specify the preferred name for your Laravel project folder. This will create the directory that will contain all the files needed by Laravel. To achieve this, we need to run the following command:

sudo composer create-project laravel/laravel ugacomp --prefer-dist

So, in essence, this command will create a new Laravel application folder called ugacomp and install all the necessary Laravel dependencies using Composer.

You need to go to the Laravel application folder using the following command:

cd ugacomp

Check the Laravel Version

Inside the Laravel application folder, you can use the following command to check the Laravel version:

php artisan | less

The following will be the Outpu indicating the version:

OutputLaravel Framework 9.38.0

Please note that your version output could be different so don’t freak out if you don’t something similar to the above output.

RECOMMENDED READING: Step-by-Step Guide to Installing FTP on Ubuntu Server

Set the required ownership of the Laravel directory

We need to assignthe Laravel directory to the www-data user and group and ensure that proper permissions are also allocated. This can be achieved by running the following command:

sudo chown -R www-data:www-data /var/www/html/ugacomp

The above command recursively changes the ownership of the directory /var/www/html/ugacomp and all of its contents to the user www-data and the group www-data

Typically, the www-data user is used to run web server processes. By changing the ownership of the /var/www/html/ugacomp directory to www-data, the web server will be able to access and modify the files in that directory.

You will also need to add  has read, write, and execute permissions to the Laravel directory using the following command:

sudo chmod -R 775 /var/www/html/ugacomp

Configure Apache for Laravel

This step requires you to create or edit the apache configuration file that will include all the server settings the Laravel application requires to be accessed in the browser.
In this guide, we will create the apache configuration file, named laravel.conf, and we can achieve this by running the following command:

sudo nano /etc/apache2/sites-available/laravel.conf

Inside the configuration file, laravel.conf, we need to add the following lines:

<VirtualHost *:80>
ServerName example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/ugacomp/public
<Directory /var/www/html/ugacomp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

From the above, you need to change the following to your prefferences:

  • ServerName: You need to replace example.com with your actual domain name
  • serverAdmin: Change [email protected] with your actual admin email
  • DocumentRoot: You need to replace ugacomp in /var/www/html/ugacomp/public with the appropriate name of your Laravel application directory

Make sure to save the above changes based on the editor you’re using on the terminal.

RECOMMENDED READING: How to Point a Domain Name from Namecheap to Contabo VPS

Enable the configuration file

You will need to enable the apache configuration file you just created by running the following command:

sudo a2ensite laravel.conf

Enable Apache rewrite module

You can run the following command to achieve this:

sudo a2enmod rewrite

Restart Apache Server

You will need to restart the Apache server using the following command:

sudo systemctl restart apache2

Access Laravel from a web browse

To access your Laravel application in the browser, you need to your Server IP address as seen below:

http://server-ip

But if you already configured the domanin name, then you can use it to access the application as seen below:

http://domain-name

More Apache-related articles to explore

More Ubuntu-related articles to explore

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.