Serverspace Black Friday
BM
February 6, 2023
Updated June 7, 2023

How to deploy Laravel application NGINX on Ubuntu 20.04 server

Linux Ubuntu

Introduction

In this publication, I will describe how to deploy a Laravel application on a server under Ubuntu Linux 20.04.

Laravel — is a publicly available PHP framework and was developed back in 2011. It is used to create multi-tiered Web resources and applications.

It promptly supports functional testing when creating software, and you can avoid using other software testing tools. Easy scalability of projects on Laravel framework.

The framework provides a simple but fast and accessible authorization system and gives you immediate access rights to your files. This allows us to restrict access to unauthorized users. Providing application security, the framework code is protected from threats such as SQL injection or cross-site query spoofing.

Laravel provides a simple and flexible approach to web application development. It comes with powerful features and tools that allow developers to create and maintain web applications quickly and easily. There is also an option for advanced developers to use templates and modules to design and develop applications more efficiently. In addition, Laravel supports many popular databases such as MySQL, Postgres, SQLite, and MS SQL, making it more suitable for developing applications with different databases. It has built-in mechanisms for security and authentication, and on top of that, it has powerful tools for monitoring applications. All this makes Laravel very attractive for web application development.

Nginx supports the following unique features:

  • Performance management: Nginx provides many tools to tune server performance, including the ability to scale and load control.
  • Large number of protocols support: Nginx supports wide range of protocols including HTTP, HTTPS, FTP, SMTP, and many more.
  • Caching management: Nginx allows caching tuning to improve performance and response time.
  • Support for distributed applications: Nginx supports distributed applications, allowing it to distribute the load across multiple servers.
  • Support for shared hosting: Nginx allows you to do shared hosting, allowing multiple web sites to run on a single server.

System requirements

  • Server on a Linux Ubuntu operating system;
  • Updated system and application packages;
  • Pre-installed software of: database; webserver.

System changes

Updating packages.

sudo apt update

Install the required packages of PHP modules.

sudo apt install php-mbstring php-xml php-bcmath

1lu

DB creation

First of all, lets research some unique options at mysql database.

  • Optimization strategies: using indexes, query optimization and configuration settings.
  • Master-slave replication, which allows asynchronously duplicating data between multiple servers.
  • Binary logs for logging changes in database tables.
  • Procedural SQL (PL/SQL) queries to perform complex tasks.
  • Use of triggers for automatic execution of actions after query execution.
  • Support for views that provide another mechanism for storing and manipulating data.
  • Support for stored procedures for complex tasks.
  • Support for deferred tasks to perform queries and actions at a specific time.
  • Using clustering to distribute data and queries between nodes.
  • Support for working with geodata that allows using data on maps.
sudo mysql

2lu

Created a new "database".

3lu

Created a user and gave me all the rights for the created "DB".

4lu

Granted all access for the "DB" to the user, then left the "DB".

5lu

This gives the user all the rights to access the "database" for administration.

Check whether we can enter MySQL using the credentials of our user.

7lu

Check the same access to the created "DB", then exit the "DB".

8lu

Creating a new application

I create a table in the DB.

10lu

Entering data into the table. In my case, I enter data on popular cities.

11lu

Check whether records have been added to the table. You will see a window like this.

12lu

Developing an app for the framework

Once you have created a citylist application. This is used to show the main configurations of the framework and access to the "database".

I have created a new directory called "citylist" where the framework itself will be contained.

composer create-project --prefer-dist laravel/laravel "name_of_your_project"

After installation, the following information will be shown on the command line.

13lu

Now open the directory of our application and run the artisan command to check if the components are installed correctly.

cd "name_of_your_project"
php artisan

Artisan is the CLI included in the framework. It gives you a number of commands that are useful for developing your application.

If everything is done as instructed, the following will be shown.

14lu

 Configuring the framework

The framework's configuration is located in the "config" directory of your main directory. When we installed the framework using the "Composer" utility, we created an environment file.

The environment file records the settings of your environment, where the application resides, and puts its file above the other settings files, which are located in the "config" directory.

Open the ".env" file.

Nano .env

Although the file seems to have a lot of configurations, but I will configure only the ones we need. Below I will write and describe which parameters I will change.

  • NAME is the name of the application.
  • ENV is an environment variable defining the current environment of the application.
  • KEY is the application key used to encrypt data.
  • URL is the URL of the application.
  • DATABASE is the name of the database.
  • USERNAME is the user name of the database.
  • PASSWORD is the database user password.

Change the following keys in the application file.

15lu

Setting up a web server

Move our application folder to "/var/www" directory, which is usually where all applications that run on a web server are stored.

mv ~/name_of_your_project /var/www/name_of_your_project

16lu

Gave access to the folders where the framework saves the files created by the application.

17lu

Let's proceed with setting up the web server. Create a virtual host configuration file.

nano /etc/nginx/sites-available/name_of_your_project

Insert the following settings for the web server.

server {
listen 80;
server_name IP ADDRESS/DOMAIN_NAME;
root /var/www/YOURPROJECT/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;}}

 

18lu

Now, run a new file with virtual host settings, create a link to your project in "sites-enabled".

sudo ln -s /etc/nginx/sites-available/name_of_your_project /etc/nginx/sites-enabled/

Check for error syntax in the settings.

sudo nginx -t

Reboot web-server

systemctl reload nginx

Configuring main framework page

Open the main route file.

nano routes/web.php

Enter the following parameters into our file.

 

20lu

Create a new file - a screen that will render the output from the "DB" for the user.

nano resources/views/travellist.blade.php

21lu

This code is designed to output the information. We write it in the configuration file.

<?php
use Illuminate\Support\Facades\DB;
Route::get('/', function () {
$visited = DB::select('select * from places where visited = ?', [1]);
$togo = DB::select('select * from places where visited = ?', [0]);
return view('yourproject', ['visited' => $visited, 'togo' => $togo ] );});

Save the file. Open the browser and reload the application. The page itself will look in this format.

22lu

Output

In this publication we have analyzed the following actions:

  1. Created and painted the "DB" for the application.
  2. Configured the new framework application.
  3. configured the framework and web server
  4. Configured the main page of the framework.

 

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.