How to Install and Configure Laravel with LEMP on Ubuntu 16.04 / 18.04 LTS / 19.10 / 20.04 LTS
Laravel is an open-source PHP framework that provides a set of tools and resources to build modern PHP applications. In this guide, you’ll install and configure a new Laravel application on an Ubuntu 18.04 server, using Composer to download and manage the framework dependencies.
In order to complete the configuration, you will first need to perform the following tasks on your Ubuntu 18.04 server.
First logged into your remote system via ssh (Secure Shell).
ssh root@server_ip_address
Let’s begin by updating server’s package index
sudo apt-get update
Installation
Step 1: Install a LEMP (Nginx, Php & MySQL) Stack
If you haven’t set this up yet, you can follow my story on How to install Linux Nginx, MySQL, PHP (LEMP Stack) on Ubuntu. Please follow the Nginx, Php & MySQL installing processes only.
Step 2: Enable remote Login to MySQL Database Server
To complete this step, please follow my story on How to enable remote Login to MySQL Database Server in Ubuntu / Debian. You really need to do this for database connection purpose 😀 You can test the remote connection using MySQL Workbench
Step 3: Install Composer
sudo apt-get install composer zip unzip -y
Step 4: Install Laravel
Install laravel via composer command
cd /var/www/html
composer create-project --prefer-dist laravel/laravel LaraApp
composer install
Or, you can git clone from your repo url 😇
cd /var/www/html
sudo git clone [Your repository URL] .
cd [Your Repository Name]
composer install
composer dump-autoload
Step 5: Configure Laravel
Open the .env
file using your command. Here we’ll use nano
command here
nano .env
Paste the below code in .env
If you want to generate a new secure key for APP_KEY
, you can use the php artisan key:generate
command.
APP_NAME=LaraApp
APP_ENV=development
APP_KEY=APPLICATION_UNIQUE_KEY_DONT_COPY
APP_DEBUG=true
APP_URL=http://domain_or_IP
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dbOne
DB_USERNAME=root
DB_PASSWORD=passcode_here
...
Once environment is done, please run below command to migrate app Database schema
php artisan migrate
In a development or testing environment, you can leave the APP_DEBUG
option enabled, as this will give you important debug information while testing the application from a browser. The APP_ENV
variable should be set to development
or testing
in this case.
In a production environment, you should disable the APP_DEBUG
option, because it shows to the final user sensitive information about your application. The APP_ENV
in this case should be set to production
.
If you use ‘#’/ ‘@’ or any punctuation symbol then put your passcode between double quotes “passcode_here”
Step 6: Permit write access
Run below command to permit write access to the storage
and cache
folders, where Laravel stores application-generated files
sudo chown -R www-data.www-data /var/www/html/[directory]/storage
sudo chown -R www-data.www-data /var/www/html/[directory]/bootstrap/cache
Step 7: Configure Nginx to serve the application
sudo rm -rf /etc/nginx/sites-available/default
sudo rm -rf /etc/nginx/sites-enabled/defaultsudo nano /etc/nginx/sites-available/myLaraApp
The following configuration file contains the recommended settings for Laravel applications on Nginx
server {listen 80;
root /var/www/html/[directory]/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
#Or write the above line as server_name [server_IP][domain_name];location / {
try_files $uri $uri/ /index.php?$query_string;
}location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# I am using php7.4-fpm, you can use other versions too
}location ~ /\.ht {
deny all;
}}
Step 8: Enable the file by linking to the sites-enabled dir
sudo ln -s /etc/nginx/sites-available/myLaraApp /etc/nginx/sites-enabled
Step 9: Test Nginx config
sudo nginx -t
You should see output like this
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If the test failed then check the Nginx configuration step again.
Step 10: Do not forget to restart your Nginx server
sudo systemctl restart nginx
Step 11: Customize your the application
Open the main route file, routes/web.php
nano routes/web.php
Then update the file with the following content
<?php
/*
|-----------------------------------------------------------------
| Web Routes
|-----------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Routes are defined within this file using the static method Route::get
, which receives a path and a callback function as arguments.
Now go to your browser and access the application using the server’s domain name or IP address
http://server_domain_or_IP
If you want to activate SSL certificate on your domain/app, please read this story.
Have fun 🔥 💪
If you have any query, please put in below. Find me on Github.