How To Install Laravel PHP Framework With Nginx On Ubuntu 20.04

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.

04

 Menu

 Menu 

How To Install Laravel PHP Framework with


Nginx on Ubuntu 20.04

Aaron Kili Last Updated: August 24, 2020 Laravel, Ubuntu Leave a comment

Laravel is the most popular, free, and open-source PHP framework in the world, known
for its expressive and elegant syntax. Laravel is accessible, powerful, and offers some
of the best web development tools required for large, robust, and modern applications.

In this article, you will learn how to install the Laravel PHP Framework on Ubuntu
20.04 server running on the Nginx web server.

Prerequisites

How To Install LEMP Stack with PhpMyAdmin in Ubuntu 20.04


https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 1/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Step 1: Installing Required PHP Modules


After setting up the LEMP stack on your Ubuntu 20.04 server as described in the guide
in the link above, you need to install additional PHP extensions required by Laravel as
follows:

$ sudo apt update


$ sudo apt php-common php-json php-mbstring php-zip php-xml php

Install PHP Modules in Ubuntu

Step 2: Creating a Database for Laravel 

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 2/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Next, you need to create a MySQL database for your Laravel application. So, log into
your mysql shell and create the database as follows.

$ sudo mysql
MariaDB [(none)]> CREATE DATABASE laraveldb;
MariaDB [(none)]> GRANT ALL ON laraveldb.* to 'webmaster'@'loca
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit

Create Database for Laravel

Step 3: Installing Composer in Ubuntu 20.04


https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 3/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel utilizes the composer (a dependency manager for PHP) to manage its
dependencies. Therefore, before using Laravel, ensure you have Composer installed on
your system as shown.

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


$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer

Install Composer in Ubuntu

Step 4: Installing Laravel in Ubuntu 20.04

Linux Foundation LFCS and LFCE Cer ti cation Preparation Guide - Get This Book

After installing the composer, use it to install the Laravel les. Move into your
/var/www/html directory where web les are stored, then install Laravel using the
composer as shown. Remember to replace example.com with the name of the
directory where the Laravel les will be stored.

$ cd /var/www/html
$ composer create-project --prefer-dist laravel/laravel example

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 4/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Install Laravel in Ubuntu

Note that while con guring NGINX to serve Laravel as described below, the
absolute path (or root path) for your site in NGINX con guration le will be
/var/www/html/example.com/publics .

Step 5: Con guring Laravel in Ubuntu 20.04


To list the contents of the new Laravel installation, run the following ls command. You
will notice that a .env le has been automatically created, which in the past, would
have to be manually created.

$ ls -la /var/www/html/example.com/

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 5/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

List Laravel Files

Next, set the appropriate permissions on the Laravel directory as follows.

$ sudo chown -R :www-data /var/www/html/example.com/storage/


$ sudo chown -R :www-data /var/www/html/example.com/bootstrap/c
$ sudo chmod -R 0777 /var/www/html/example.com/storage/
$ sudo chmod -R 0775 /var/www/html/example.com/bootstrap/cache/

Next, Laravel uses an application key to secure user sessions and other encrypted
data. The default .env contains a default application key but you need to generate
a new one for your laravel deployment for security purposes.

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 6/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

$ sudo php artisan key:generate

The generated key will be appended in the .env le as the value of the APP_KEY .
You can view the appended key using grep command.

$ grep -i APP_Key /var/www/html/example.com/.env

View Laraval Key

You also need to con gure the Laravel database connection details in .env as
shown in the following screenshot.

$ sudo nano /var/www/html/example.com/.env

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 7/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel Database Settings

Step 6: Con guring NGINX to Serve Laravel


Application
For NGINX to serve your new application, you need to create a server block for it
within the NGINX con guration, under the /etc/nginx/sites-available/
directory.

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 8/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

$ sudo nano /etc/nginx/sites-available/example.com.conf

In the con guration below, update the root directive to the Laravel application’s public
directory and make sure to replace www.example.com with the domain name of your
website as shown.

Also, set the fastcgi_pass directive should point to the medium PHP-FPM is
listening on for requests (for example fastcgi_pass unix:/run/php/php7.4-
fpm.sock ):

server{
server_name www.example.com;
root /var/www/html/example.com/public;
index index.php;

charset utf-8;
gzip on;
gzip_types text/css application/javascript text/javascr
location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php {
include fastcgi.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 9/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Save the le and then enable the Laravel site con guration by creating a link from
/etc/nginx/sites-available/example.com.conf to the /etc/nginx/sites-
enabled/ directory. Besides, remove the default server block con guration.

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/n


$ sudo rm /etc/nginx/sites-enabled/default

Next, check if the NGINX con guration syntax is correct by running the following
command before restarting the service.

$ sudo nginx -t
$ sudo systemctl restart nginx

Step 7: Accessing Laravel Application from a Web


Browser
At this stage, you need to test if your Laravel deployment is working ne and whether
it can be accessed from a browser. To use the dummy domain, example.com , let’s
use the /etc/hosts le on your local computer to create local DNS.

Run the following commands to get the IP address of the Laravel server and add it to
the /etc/hosts le (replace the value according to your settings).
$ ip ad
$ echo “192.168.56.11 example.com” | sudo tee -a /etc/hosts

Now open a web browser on the local computer and use the following address to
navigate.

http://www.example.com/ 

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 10/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel Web Page

Now that you have Laravel installed, you can start building your web application or
site. For more information, see the Laravel documentation.

 Laravel , Laravel PHP Framework , Ubuntu Tips

 10 Interesting and Useful Apps I What is MariaDB? How Does MariaDB


Discovered in Snap Store Work? 

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you
have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You 


Should Consider:
https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 11/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

TecMint is the fastest growing and most trusted community site for
any kind of Linux Articles, Guides and Books on the web. Millions of
people visit TecMint! to search or browse the thousands of
published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee
( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Related Posts

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 12/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

How to Install Laravel PHP Framework with Nginx on CentOS 8

Got something to say? Join the discussion.


Have a question or suggestion? Please leave a comment to start the discussion. Please
keep in mind that all comments are moderated and your email address will NOT be

published.

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 13/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Notify me of followup comments via e-mail. You can also subscribe without
commenting.

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Over 3,500,000+ Readers

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 14/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

A Beginners Guide To Learn Linux for Free [with Examples]

Red Hat RHCSA/RHCE 8 Certi cation Study Guide [eBooks]

Linux Foundation LFCS and LFCE Certi cation Study Guide [eBooks]

Learn Linux Commands and Tools

How to Find Recent or Today’s Modi ed Files in Linux

How to Set and Unset Local, User and System Wide Environment Variables in Linux

Rainbow Stream – An Advanced Command-line Twitter Client for Linux

How to List All Files Ordered by Size in Linux

Bat – A Cat Clone with Syntax Highlighting and Git Integration

cloc – Count Lines of Code in Many Programming Languages

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 15/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

If You Appreciate What We Do Here On TecMint, You Should Consider:

Linux Server Monitoring Tools

BCC – Dynamic Tracing Tools for Linux Performance Monitoring, Networking and
More

20 Netstat Commands for Linux Network Management

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 16/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Arpwatch Tool to Monitor Ethernet Activity in Linux

How to Add Hosts in OpenNMS Monitoring Server

Bashtop – A Resource Monitoring Tool for Linux

How to Install Nagios 4 in Ubuntu and Debian

Learn Linux Tricks & Tips

How to Record and Replay Linux Terminal Sessions using ‘script’ and
‘scriptreplay’ Commands

How to Find Out Top Directories and Files (Disk Space) in Linux

How to Find a Speci c String or Word in Files and Directories

5 Ways to Empty or Delete a Large File Content in Linux

How to Repair and Defragment Linux System Partitions and Directories

vlock – A Smart Way to Lock User Virtual Console or Terminal in Linux

Best Linux Tools

10 Best File and Disk Encryption Tools for Linux

Top 27 Tools for VMware Administrators

10 Best Markdown Editors for Linux

12 Best Notepad++ Alternatives For Linux

13 Best Tiling Window Managers for Linux

7 Best IRC Clients for Linux

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 17/18
26/10/2020 How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Donate to TecMint Contact Us Advertise on TecMint Linux Services Copyright Policy

Privacy Policy Career Sponsored Post

Tecmint: Linux Howtos, Tutorials & Guides © 2020. All Rights Reserved.
The material in this site cannot be republished either online or of ine, without our permission.

Hosting Sponsored by : Linode Cloud Hosting

https://www.tecmint.com/install-laravel-with-nginx-on-ubuntu/ 18/18

You might also like