How to Install Laravel on Fedora

Install Laravel on Fedora

Laravel has revolutionized PHP web development with its elegant syntax and powerful features. If you’re running Fedora Linux and want to set up Laravel for your next project, you’re in the right place. This comprehensive guide will walk you through every step of installing Laravel on Fedora, from setting up the prerequisites to running your first Laravel application.

Table of Contents

What is Laravel and Why Use It?

Laravel is a modern PHP framework that follows the Model-View-Controller (MVC) architectural pattern. Created by Taylor Otwell, it has become one of the most popular PHP frameworks due to its expressive syntax and developer-friendly features.

Key Features of Laravel

Laravel comes packed with features that make web development faster and more enjoyable:

  • Eloquent ORM: Laravel’s built-in Object-Relational Mapping system simplifies database interactions
  • Blade Templating Engine: A powerful templating system that makes creating views effortless
  • Artisan CLI: Command-line interface for automating repetitive tasks
  • Built-in Authentication: Ready-to-use authentication system
  • Migration System: Version control for your database schema
  • Queue Management: Handle time-consuming tasks asynchronously

Benefits of Using Laravel on Fedora

Fedora provides an excellent environment for Laravel development. Here’s why this combination works so well:

  • Latest PHP Versions: Fedora typically includes recent PHP versions that meet Laravel’s requirements
  • Package Management: DNF package manager makes installing dependencies straightforward
  • Development Tools: Fedora comes with extensive development tools pre-installed
  • Security: Regular security updates keep your development environment secure
  • Community Support: Both Fedora and Laravel have active, helpful communities

Prerequisites for Installing Laravel on Fedora

Before diving into the installation process, let’s ensure your system meets all requirements.

System Requirements

Laravel requires specific software components to function properly:

  • PHP 8.1 or newer: Laravel’s latest version requires PHP 8.1+
  • Composer: PHP dependency manager
  • Web Server: Apache or Nginx
  • Database: MySQL, MariaDB, PostgreSQL, SQLite, or SQL Server
  • PHP Extensions: Several extensions are required for full functionality

Checking Your Fedora Version

First, let’s verify your Fedora version:

cat /etc/fedora-release

Fedora 35 and newer versions come with PHP 8.0 or later, which meets Laravel’s requirements. If you’re using an older version, consider upgrading your system first.

Step 1 – Installing the LAMP Stack

LAMP (Linux, Apache, MySQL/MariaDB, PHP) provides the foundation for running Laravel applications. Let’s install each component.

Installing Apache Web Server

Apache will serve as our web server. Install it using DNF:

sudo dnf install httpd

Starting and Enabling Apache

Once installed, start Apache and enable it to start automatically on boot:

sudo systemctl start httpd
sudo systemctl enable httpd

Verify Apache is running:

sudo systemctl status httpd

Installing PHP and Required Extensions

Laravel requires PHP with several extensions. Install them all at once:

sudo dnf install php php-zip php-mysqlnd php-xml php-mbstring php-json php-gd php-curl php-tokenizer php-bcmath php-opcache php-intl

These extensions provide:

  • php-zip: Archive handling
  • php-mysqlnd: MySQL database connectivity
  • php-xml: XML processing
  • php-mbstring: Multi-byte string support
  • php-json: JSON handling
  • php-gd: Image processing
  • php-curl: HTTP client functionality
  • php-tokenizer: Code parsing
  • php-bcmath: Precision mathematics
  • php-opcache: Performance optimization
  • php-intl: Internationalization support

Verifying PHP Installation

Check your PHP version to ensure it meets Laravel’s requirements:

php --version

You should see PHP 8.1 or newer. Also verify that required extensions are loaded:

php -m | grep -E "(zip|mysql|xml|mbstring|json)"

Installing MariaDB/MySQL

MariaDB is an open-source alternative to MySQL that works perfectly with Laravel:

sudo dnf install mariadb-server

Start and enable MariaDB:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Securing MariaDB Installation

Run the security script to set up root password and remove insecure defaults:

sudo mysql_secure_installation

Follow the prompts to:

  • Set a root password
  • Remove anonymous users
  • Disable remote root login
  • Remove test database
  • Reload privilege tables

Step 2 – Installing Composer

Composer is PHP’s dependency manager and the primary tool for installing Laravel.

What is Composer?

Composer manages PHP packages and their dependencies automatically. It’s essential for modern PHP development and required for Laravel installation.

Installing Composer on Fedora

You can install Composer directly from Fedora’s repositories:

sudo dnf install composer unzip

Alternatively, install it manually for the latest version:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Verifying Composer Installation

Test Composer installation:

composer --version

You should see the Composer version information, confirming successful installation.

Step 3 – Installing Laravel

Now we’ll install Laravel using Composer. There are two main methods to choose from.

Method 1: Using Composer Create-Project

This is the recommended method for creating new Laravel projects:

cd /var/www/html
sudo composer create-project --prefer-dist laravel/laravel mylaravelapp

This command:

  • Downloads the latest Laravel version
  • Installs all dependencies
  • Sets up the basic project structure
  • Generates the application key

Method 2: Cloning from GitHub

Alternatively, you can clone Laravel’s repository directly:

cd /var/www/
sudo git clone https://github.com/laravel/laravel.git mylaravelapp

Installing Dependencies

If you used Method 2, install dependencies manually:

cd /var/www/mylaravelapp
sudo composer install

This process may take several minutes as Composer downloads and installs all required packages.

Step 4 – Configuring Laravel

Proper configuration ensures Laravel runs smoothly and securely.

Setting Directory Permissions

Laravel needs write access to specific directories. Set appropriate permissions:

sudo chown -R apache:apache /var/www/mylaravelapp
sudo chmod -R 755 /var/www/mylaravelapp
sudo chmod -R 755 /var/www/mylaravelapp/storage
sudo chmod -R 755 /var/www/mylaravelapp/bootstrap/cache

Configuring Environment Variables

Laravel uses an .env file for environment configuration. Copy the example file:

cd /var/www/mylaravelapp
sudo cp .env.example .env

Generating Application Key

Generate a unique application key for encryption:

sudo php artisan key:generate

This command creates a 32-character encryption key and adds it to your .env file automatically.

Database Configuration

Edit the .env file to configure database settings:

sudo nano .env

Update these lines with your database information:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=your_username
DB_PASSWORD=your_password

Create the database in MariaDB:

mysql -u root -p
CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON laravel.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

Step 5 – Setting Up Virtual Host

Configure Apache to serve your Laravel application properly.

Creating Apache Virtual Host

Create a new virtual host configuration:

sudo nano /etc/httpd/conf.d/laravel.conf

Add this configuration:

<VirtualHost *:80>
    ServerName laravel.local
    DocumentRoot /var/www/mylaravelapp/public
    
    <Directory /var/www/mylaravelapp/public>
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog /var/log/httpd/laravel_error.log
    CustomLog /var/log/httpd/laravel_access.log combined
</VirtualHost>

Enabling the Virtual Host

Restart Apache to load the new configuration:

sudo systemctl restart httpd

Add the domain to your hosts file for local development:

sudo echo "127.0.0.1 laravel.local" >> /etc/hosts

Step 6 – Testing Your Laravel Installation

Let’s verify everything is working correctly.

Starting the Development Server

For quick testing, use Laravel’s built-in development server:

cd /var/www/mylaravelapp
php artisan serve --host=0.0.0.0 --port=8000

Accessing Laravel Welcome Page

Open your browser and navigate to:

  • http://localhost:8000 (for artisan serve)
  • http://laravel.local (for Apache virtual host)

You should see Laravel’s welcome page with the message “Laravel” and version information, confirming successful installation.

Troubleshooting Common Issues

Here are solutions to common problems you might encounter.

Permission Problems

If you encounter permission errors, ensure proper ownership and permissions:

sudo chown -R apache:apache /var/www/mylaravelapp
sudo chmod -R 755 /var/www/mylaravelapp
sudo chmod -R 775 /var/www/mylaravelapp/storage
sudo chmod -R 775 /var/www/mylaravelapp/bootstrap/cache

SELinux Configuration

Fedora’s SELinux may block Laravel from writing to storage directories.

Setting SELinux Context

Allow Apache to write to Laravel directories:

sudo setsebool -P httpd_can_network_connect 1
sudo chcon -R -t httpd_sys_rw_content_t /var/www/mylaravelapp/storage
sudo chcon -R -t httpd_sys_rw_content_t /var/www/mylaravelapp/bootstrap/cache

Make these changes persistent:

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/mylaravelapp/storage(/.*)?"
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/mylaravelapp/bootstrap/cache(/.*)?"
sudo restorecon -R /var/www/mylaravelapp/storage
sudo restorecon -R /var/www/mylaravelapp/bootstrap/cache

Security Best Practices

Implement these security measures to protect your Laravel application.

File Permissions

Never set permissions to 777. Use these secure permissions instead:

  • Directories: 755
  • Files: 644
  • Storage and cache directories: 775

Environment Configuration

  • Keep .env files out of version control
  • Use strong, unique application keys
  • Configure proper database credentials
  • Set APP_DEBUG=false in production
  • Use HTTPS for production deployments

Performance Optimization Tips

Optimize your Laravel installation for better performance:

  1. Enable OPcache: Already included in our PHP installation
  2. Use Redis for caching: Install and configure Redis for session and cache storage
  3. Optimize Composer autoloader: Run composer install --optimize-autoloader --no-dev in production
  4. Configure application caching: Use php artisan config:cache and php artisan route:cache
  5. Enable Gzip compression: Configure Apache to compress responses

Frequently Asked Questions (FAQs)

1. What version of PHP do I need for Laravel on Fedora?

Laravel requires PHP 8.1 or newer. Fedora 35 and later versions include PHP 8.0 or newer, but you may need to install PHP 8.1+ from additional repositories for the latest Laravel versions. Check your PHP version with php --version and upgrade if necessary.

2. Can I use Nginx instead of Apache with Laravel on Fedora?

Yes, absolutely! You can use Nginx instead of Apache. Install Nginx with sudo dnf install nginx, configure a server block for your Laravel application, and ensure the document root points to your Laravel project’s public directory. The configuration process is similar to Apache but uses Nginx syntax.

3. How do I update Laravel to the latest version after installation?

To update Laravel, navigate to your project directory and run composer update. This updates Laravel and all dependencies to their latest compatible versions. For major version upgrades, check Laravel’s upgrade guide as some changes may require manual intervention.

4. Why am I getting permission denied errors when accessing my Laravel application?

Permission errors typically occur due to incorrect file ownership or SELinux policies. Ensure Apache owns the Laravel files (sudo chown -R apache:apache /var/www/mylaravelapp) and configure SELinux to allow Apache to access the necessary directories. Also verify that storage and bootstrap/cache directories have write permissions.

5. How do I enable HTTPS for my Laravel application on Fedora?

To enable HTTPS, install an SSL certificate (you can use Let’s Encrypt for free certificates), configure Apache to use SSL, and update your Laravel .env file to force HTTPS. Install mod_ssl (sudo dnf install mod_ssl), create a secure virtual host configuration, and ensure your Laravel application’s APP_URL uses HTTPS.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts