How to Install Laravel on RHEL 7

Laravel is one of the most popular PHP frameworks, renowned for its expressive syntax, robust ORM (Eloquent), built-in authentication scaffolding, and an ecosystem of first-party packages for queuing, broadcasting, and caching. Installing Laravel on RHEL 7 requires assembling several components: a recent PHP version from the Remi repository, a MySQL or MariaDB database, Composer for package management, and Nginx as the web server. This guide walks through every step from prerequisites to a running Laravel application, including Nginx virtual host configuration, environment setup, permission hardening, and SELinux policy adjustments specific to RHEL 7.

Prerequisites

  • RHEL 7 server with root or sudo access
  • PHP 7.4 or newer installed from the Remi repository (see the PHP installation guide)
  • PHP extensions: php-fpm, php-mysqlnd, php-mbstring, php-xml, php-zip, php-bcmath, php-curl, php-opcache
  • Composer installed globally at /usr/local/bin/composer
  • Nginx installed and running
  • MySQL 5.7 or MariaDB 10.x installed and a database created for the application
  • Firewall open on port 80 (and 443 for HTTPS)

Step 1: Install and Start Nginx

If Nginx is not already installed:

sudo yum install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

Open port 80 in the firewall:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 2: Create a MySQL Database for Laravel

Connect to MySQL and create a dedicated database and user:

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

Step 3: Create the Laravel Project with Composer

Navigate to your web directory and create the Laravel project. Replace myapp with your application name:

cd /var/www
sudo composer create-project laravel/laravel myapp

Composer will download Laravel and all its dependencies into /var/www/myapp/. This may take a minute depending on connection speed.

Verify the directory structure was created correctly:

ls /var/www/myapp

You should see directories including app/, bootstrap/, config/, database/, public/, resources/, routes/, storage/, and vendor/.

Step 4: Set Directory Ownership and Permissions

Nginx and PHP-FPM run as the nginx user. Assign ownership of the application directory accordingly:

sudo chown -R nginx:nginx /var/www/myapp

Laravel requires write access to storage/ and bootstrap/cache/ for logs, compiled views, and the filesystem cache:

sudo chmod -R 775 /var/www/myapp/storage
sudo chmod -R 775 /var/www/myapp/bootstrap/cache

Step 5: Configure the .env File

Laravel reads environment configuration from a .env file in the project root. Copy the example file that ships with Laravel:

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

Edit the .env file and update the database connection and application settings:

sudo vi /var/www/myapp/.env
APP_NAME=MyApp
APP_ENV=production
APP_DEBUG=false
APP_URL=http://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=StrongP@ssword1

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_CONNECTION=sync

Step 6: Generate the Application Key

Laravel uses an application key to encrypt session data and other values. Generate it with Artisan:

cd /var/www/myapp
sudo -u nginx php artisan key:generate

This writes a random 32-character base64 key to APP_KEY in .env. Confirm it was set:

grep APP_KEY /var/www/myapp/.env

Step 7: Run Database Migrations

Laravel includes built-in migration files for the users, password resets, and failed jobs tables. Run them to initialize the schema:

sudo -u nginx php artisan migrate

If you see errors about the database connection, double-check the credentials in .env and confirm the MySQL service is running with sudo systemctl status mysqld.

Step 8: Configure Nginx for Laravel

Create an Nginx virtual host configuration for the Laravel application:

sudo vi /etc/nginx/conf.d/laravel.conf
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    root /var/www/myapp/public;
    index index.php index.html;

    access_log /var/log/nginx/laravel_access.log;
    error_log  /var/log/nginx/laravel_error.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ .php$ {
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_read_timeout 300;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }

    client_max_body_size 64M;
}

Test the Nginx configuration for syntax errors:

sudo nginx -t

Reload Nginx to apply the new configuration:

sudo systemctl reload nginx

Step 9: Configure SELinux for Laravel

RHEL 7 runs SELinux in enforcing mode by default, which can block PHP from writing to the filesystem even when Unix permissions are correct. Enable the httpd_unified boolean to allow the web server to read and write application files:

sudo setsebool -P httpd_unified 1

Allow the web server to make outbound network connections (required for API calls, mail, queues):

sudo setsebool -P httpd_can_network_connect 1
sudo setsebool -P httpd_can_network_connect_db 1

If Laravel still cannot write to storage/, apply the correct SELinux file context manually:

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

Note: semanage is part of the policycoreutils-python package. Install it if missing:

sudo yum install -y policycoreutils-python

Step 10: Optimize Laravel for Production

Run these Artisan commands to cache configuration, routes, and views for improved performance:

cd /var/www/myapp
sudo -u nginx php artisan config:cache
sudo -u nginx php artisan route:cache
sudo -u nginx php artisan view:cache

To clear all caches (useful when deploying updates):

sudo -u nginx php artisan optimize:clear

Step 11: Verify the Installation

Open a browser and navigate to http://yourdomain.com. You should see the Laravel welcome page with the framework version displayed. Check Nginx error logs if the page does not load:

sudo tail -f /var/log/nginx/laravel_error.log
sudo tail -f /var/www/myapp/storage/logs/laravel.log

Conclusion

Laravel is now installed and running on RHEL 7 with Nginx and MySQL, with SELinux properly configured to allow the web server the access it needs without disabling mandatory access control entirely. The application key is set, database migrations have run, and the application is optimized with configuration and route caching. From this foundation you can scaffold authentication with php artisan make:auth (Laravel 6) or install the Laravel Breeze/Jetstream starter kits for newer versions, create models and controllers with Artisan generators, configure queues, and deploy further with tools like Deployer or Envoyer.