Laravel is the most popular open-source framework of PHP. It is based on MVC. The framework is created by Taylor Otwell with nice documentation and has many features like routing, middleware, session, inbuilt authentication, caching, etc.
To install and successfully run the Laravel project make sure the LAMP stack (Apache, PHP, and Mysql) is installed in your system.
In this tutorial, we will install Laravel with the composer in ubuntu 16.04 LTS and 18.04 LTS. You can follow this tutorial to install on AWS and digitalocean.
Step 1. Install PHP Extension
Before we get started. The following extension is required to install the Laravel.
- PDO PHP Extension
- Mbstring PHP Extension
- Tokenizer PHP Extension
- Ctype PHP Extension
- JSON PHP Extension
- BCMath PHP Extension
Run the below command to install the above extension.
sudo apt-get install php-common php-mbstring php-xml php-zip php-mysql php-bcmath php-json
Step 2. Install Composer.
Laravel uses Composer to manage its dependency. So, before installing Laravel, Make sure the Composer is installed on your server/machine.
Composer is a dependency management tool in PHP. It is not the same as yum or apt package manager in Linux.
It is used to manage packages (Vendor Directory) in your project. It doesn't install globally by default. if you want to install the package globally you can use global to install.
Run the below command to install the composer.
sudo apt install composer
Step 3. Create a Laravel Project
To create a new Laravel project run the following command on your terminal. My active directory path is
#/var/www/html/ composer create-project laravel/laravel blog --prefer-dist
Note: --prefer-dist means composer to download and unzip the dependency from Github or any other API source.
Step 4. Changing the Laravel project directory permission.
Next, Change the storage and bootstrap directory permission otherwise will get the permission error.
#/var/www/html/ sudo chgrp -R www-data storage bootstrap/cache sudo chmod -R ug+rwx storage bootstrap/cache
Step 6. Laravel project configuration.
By default, Laravel ships with .env.example. we need to change .env.example to .env To rename enter the following command.
#/var/www/html cd blog cp .env.example .env
Generate the key
php artisan key:generate
Open Laravel project directory in your favorite code editor and edit .env file to configure the Laravel database.
Enter your MySQL credentials
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD=root
Step 5. Run the Laravel project via terminal with artisan command.
To test the installation. Enter the below command.
#/var/www/html/blog php artisan serve
you will get the following response
desert:blog$ php artisan serve Laravel development server started: <http://127.0.0.1:8000>
Open http://127.0.0.1:8000 in your browser and it's done.