Installing Laravel featured image

Installing and Configuring Laravel with Nginx on Ubuntu 20.04

Introduction

Laravel is a web application framework that comes with a set of built-in tools and resources to develop modern PHP applications. It’s well-known for its reliability, performance, efficiency, modularity, and security. Because of its various built-in features, Laravel has seen incredible growth in its popularity. Many developers are adopting Laravel to streamline the web development process.

This guide will demonstrate how to install and configure a new Laravel application on Ubuntu 20.04.

Laravel on Ubuntu

We will use Composer, a PHP dependency manager, to download and manage Laravel. To install Composer follow the instructions of this tutorial. The Nginx server will serve the application. It will establish a demo Laravel application using the MySQL database.

This guide requires some preconditions. We assume that you already have a properly configured Ubuntu 20.04 server up and running. For reference, here’s a quick guide on setting up an Ubuntu server. It’s also required to have the LEMP (Linux, Nginx, MySQL, and PHP) stack installed and configured. We have a detailed guide on installing and configuring the LEMP stack.

Because we’re going to install various packages, it requires root permission. In the case of Ubuntu, the root user is disabled by default. So, we need access to a non-root user with sudo privilege. Here’s how to grant sudo permission to other users.

Step 1 – Installing PHP Modules

Before installing Laravel, we need some important PHP modules for the framework. These modules are directly available from the Ubuntu default package repos. These modules support features like character encoding, XML, and precision mathematics.

First, update the APT cache and install the packages:

Step 2 – Creating a Database for the Application

To demonstrate the functionality of Laravel, we will make a travel list application that shows the list of places the user would like to visit, and a list of places that are already visited. We can store this data in a table place with fields name (location name) and visited (marking as visited or nonvisited). An additional field will feature a unique identity for each entry. To connect the data with our Laravel application, we will create a dedicated MySQL user, and grant the user full permission over the database travellist.

The native MySQL PHP library mysqlnd no longer supports caching_sha2_authentication. Thus, we need to set our database with the authentication method mysql_native_password. Access the MySQL shell as the root:

Next, create a new database named travellist:

After that, create a dedicated user travellist_user with a suitable password:

Give the user travellist_user full permission over the database travellist. It will also prevent the user from creating and modifying any other database on the server:

Exit the MySQL shell:

exit

Let’s put the new user to the test. The following command will log in to the MySQL shell as the user travellist_user. The flag -p will prompt for the user password:

Print all the available databases. As configured earlier, it should show travellist and information_schema only:

Next, we need to create the data table places. Run the following MySQL statement:

Let’s populate the table with some sample data:

Let’s see if the data was stored successfully on the database:

As we can see, the test table is populated with valid data. We can now exit the MySQL shell:

MySQL shell

Step 3 – Creating a New Laravel App

We are now ready to create a new Laravel application using Composer. We’ll use travellist as a demo application. This application will display the location list from the MySQL database, demonstrating the basic configuration of Laravel. Go to the user’s home directory:

The next step is to create a new Laravel application using Composer. It will create a dedicated directory travellist with a barebones Laravel application with default settings. Run the following command:

composer create-project

Once the app is established, we can verify if all the components were installed successfully:

php artisan

The output verifies that all the application files are in place. It also assures that all the Laravel command-line tools are functional. However, it still requires app configuration to set up the database and a few extra tweaks.

Step 4 – Laravel Configuration

All the Laravel configuration files are stored within a directory named config within the root app directory. In addition, when installing Laravel with Composer, it also creates an environment file. It contains various settings specific to the current environment. Values declared within it will take precedence over the values set in regular configuration files at config.

Each unique installation will require its own environment file to define values like database connection settings, app URL, debug options, etc. Note that the environment file contains sensitive info like database credentials, security keys, etc. That’s why it’s strongly recommended to never share this file publicly.

Open the environment file with a text editor:

Here’s a sample environment code:

DB_PASSWORD=password123

Let’s break down the values:

  • APP_NAME: This field describes the name of the application. This name will be used for notifications and messages.
  • APP_ENV: The current environment of the application.
  • APP_KEY: This key is generated automatically when installing Laravel through Composer. It is used to generate salts and hashes. No need to change it.
  • APP_DEBUG: Debug mode toggle. When turned on, it will show debug info on the client-side.
  • APP_URL: App base URL. It’s also used for generating application links.
  • DB_DATABASE: The name of the database. In this case, it will be the dedicated database we created.
  • DB_USERNAME: The username to connect to the database.
  • DB_PASSWORD: The password to connect to the database.

Once the changes are made, save the file and close the editor.

Step 5 – Setting up Nginx

We’ve successfully installed Laravel on a local folder of the user’s home directory. This configuration works fine for the local development environment. However, it’s not recommended to implement it for web servers that are open to the public internet.

Our next step is to move the application to /var/www as it’s the usual location for web apps running on Nginx. Use the mv command to move travellist to /var/www/travellist:

Next, assign the user write access to the storage and cache folders. These are the locations of Laravel app-generated files:

The application files are now in place. However, we need to tell Nginx to serve the content. Create a new virtual host configuration file:

Here’s a sample of recommended settings for a Laravel application running on Nginx:

Time to activate the new virtual host configuration file. Create a symbolic link to sites-enabled directory:

The following Nginx command will test the configuration files for syntax errors:

Then, reload Nginx:

The application should now be available using the domain name or IP address (as defined by the server_name directive):

Installing Laravel image 3

Step 6 – Customizing the Main Page

With the Laravel application configured, we can now start working on it. For demonstration, it’s time we created a sample page showcasing the sample data from the database table places. First, open the main route file in a text editor:

The file will contain some content by default. Here, routes are defined using the static method (Route::get). It receives a path and a callback function as arguments.

Replace the code with the following one:

Installing Laravel image 2

Save the file and close the editor. Next, we’ll create the layout to render the database query results to the user. Create a new file within resource/views:

In the following code, we’ve created two lists of places depending on the value of visited and togo:

Installing Laravel image 1

Save the file and close the editor. Now, access the web app page again:

Travel list

Voila! We now have a functional Laravel application using MySQL database for contents.

Final Thoughts

In this tutorial, we demonstrated how to set up a Laravel application on top of the LEMP stack running on the Ubuntu 20.04 server. We also showcased how to customize the default route to query database content and display results in a custom view. For more information on Laravel, review the official documentation on views, routes, and database support.

The installation of this tutorial is performed on a standalone virtual machine. However, you can also deploy Laravel on the LEMP stacks on Docker.

Happy Computing!