How to install Laravel with LEMP stack on Ubuntu
Laravel is one of the most popular open-source PHP framework, and Laravel allows you to build scalable and flexible web applications. Because of its outstanding features, for example, routing, authentication, sessions, caching, and unit testing, Laravel is a framework of choice for many PHP developers.
In this post, you will learn how to install Laravel with LEMP stack on an Ubuntu server.
Prerequisites
Before continuing, make sure you are logged in as a user with sudo privileges.
Install a LEMP stack. If you have not yet installed, then you can follow the guide on this post: How to Install Nginx, MySQL, and PHP on Ubuntu.
Step 1 - Installing Required PHP modules
Before you start installing Laravel, you have to install a couple of PHP modules that are needed by the framework.
Run the following command on terminal to install all required PHP modules:
sudo apt install php-mbstring php-xml php-bcmath php-zip
Step 2 - Installing Composer
Composer is a dependency manager for PHP and we will utilize it to download the Laravel core and install all important Laravel packages.
To install composer globally, download the Composer installer with curl
and then move the composer file to the /usr/local/bin
directory using below command:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Now composer is installed, verify it by checking its version:
composer --version
Step 3 - Creating a Database for the Application
To connect with the database from the Laravel application, we'll make a MySQL user, and grant this user full privileges over the database we want to connect from the Laravel app.
To begin, sign in to the MySQL console as the root user using the following command:
sudo mysql
Create Database:
mysql> CREATE DATABASE database_name;
Create Database User
At the time of this writing, the native MySQL PHP library mysqlnd
doesn't support caching_sha2_authentication
, which is the default authentication strategy for MySQL 8. We need to set up our database user with the mysql_native_password
authentication strategy so as to be able to connect to the MySQL database from PHP.
So, use the following command to make a new user with mysql_native_password
authentication method:
mysql> CREATE USER 'database_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Now we need to give this user permission over the database:
mysql> GRANT ALL ON database_name.* TO 'database_user'@'%';
Now FLUSH PRIVILEGES and exit.
mysql> FLUSH PRIVILEGES;
mysql> exit;
Step 4 - Creating a New Laravel Application
Run the Composer
command to install Laravel in the create-project
directory:my_app
composer create-project --prefer-dist laravel/laravel my_app
When the installation is finished, access the application’s directory and run Laravel’s
command to confim it. artisan
cd ~/my_app
php artisan
This will show Laravel version and some its commands.
Configuring Laravel
Now, we will edit the .env
file to add database credentials and other application configuration options.
Open the .env
file in your editor of choice. Here we’ll use vi
:
vi .env
The following
file is example of our application for development:.env
APP_NAME=Laravel
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=database_name
DB_USERNAME=database_user
DB_PASSWORD=password
Step 5 - Create Nginx Virtual Host for Laravel App
First, we need to give the webserver user write access to the storage and cache folders. Use the following command for that:
sudo chown -R www-data.www-data /var/www/my_app/storage
sudo chown -R www-data.www-data /var/www/my_app/bootstrap/cache
Next, create a virtual host configuration file at /etc/nginx/sites-available
:
sudo vi /etc/nginx/sites-available/my_app.com
The following snippet contains the recommended configuration for Laravel applications on Nginx. Add this to your virtual host file, and don't forget to replace your domain name and project path in this snippet.
server {
listen 80;
server_name server_domain_or_IP;
root /var/www/travel_list/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;
}
}
To enable this new virtual host configuration, create a symbolic link of this in /etc/nginx/sites-enabled
:
sudo ln -s /etc/nginx/sites-available/my_app.com /etc/nginx/sites-enabled/
Confirm that the configuration doesn’t contain any syntax errors:
sudo nginx -t
If any errors are reported, go back and recheck your configuration file before continuing.
To apply the changes, reload Nginx with:
sudo systemctl reload nginx
This concludes the installation and configuration of your Laravel with LEMP stack on Ubuntu server. Now in your browser, you can access the application using the server’s domain name or IP address as defined by the server_name
in your virtual host.
Please login or create new account to add your comment.