Complete Guide to Install Laravel 5.8 on Linux

Complete Guide to Install Laravel 5.8 on Linux

A comprehensive guide to installing Laravel 5.8 on Linux systems with all necessary dependencies and configurations.

Introduction

This guide will walk you through the process of installing Laravel 5.8 on a Linux system. We'll cover all the necessary prerequisites, installation steps, and post-installation configurations.

System Requirements

  • PHP >= 7.2.5
  • BCMath PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

1 Install PHP and Required Extensions

First, update your system and install PHP along with all required extensions:

sudo apt update
sudo apt install php7.2 php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-mysql php7.2-zip php7.2-fpm php7.2-mbstring php7.2-curl php7.2-xml php7.2-bcmath

2 Install Composer

Install Composer, PHP's package manager:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

3 Create New Laravel Project

Create a new Laravel 5.8 project using Composer:

composer create-project --prefer-dist laravel/laravel:^5.8.0 my-project

4 Configure Environment

Set up your environment configuration:

cd my-project
cp .env.example .env
php artisan key:generate

5 Set Directory Permissions

Set the correct permissions for Laravel directories:

sudo chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache

6 Configure Web Server

Set up Apache virtual host configuration:

<VirtualHost *:80>
    ServerName your-domain.com
    DocumentRoot /var/www/my-project/public

    <Directory /var/www/my-project/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

7 Enable Required Apache Modules

Enable the necessary Apache modules:

sudo a2enmod rewrite
sudo systemctl restart apache2

8 Run Migrations

Set up your database and run migrations:

php artisan migrate

Optimization Tips

  • Run php artisan config:cache to cache configuration
  • Run php artisan route:cache to cache routes
  • Run php artisan view:cache to cache views

Common Issues and Solutions

  • If you get permission errors, ensure proper ownership of project files
  • If mod_rewrite is not working, check Apache configuration
  • If storage links are broken, run php artisan storage:link