Laravel is the most popular PHP web application framework, providing an expressive, MVC-based structure with built-in features including Eloquent ORM, Blade templating, database migrations, queues, events, broadcasting, and the Artisan CLI. Laravel follows convention-over-configuration to reduce boilerplate and accelerates development with scaffolding tools like Breeze (authentication starter kit) and Jetstream. This guide covers creating a new Laravel 11 application on RHEL 9, configuring it behind Nginx with PHP-FPM, setting up the .env configuration and database connection, and running migrations.
Prerequisites
- PHP 8.3 and Composer installed on RHEL 9
- Nginx installed (
dnf install -y nginx) - MySQL 8 or PostgreSQL 16 running
Step 1 — Create a New Laravel Project
cd /var/www
# Create the project
composer create-project laravel/laravel myapp "^11.0"
cd myapp
# Set correct permissions
chown -R nginx:nginx /var/www/myapp
chmod -R 755 /var/www/myapp/storage /var/www/myapp/bootstrap/cache
Step 2 — Configure the .env File
# /var/www/myapp/.env
APP_NAME=MyApp
APP_ENV=production
APP_KEY=base64:$(php artisan key:generate --show)
APP_DEBUG=false
APP_URL=https://myapp.example.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=myapp_user
DB_PASSWORD=MyAppDBPass123!
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
# Generate app key (must be done once)
cd /var/www/myapp
php artisan key:generate
Step 3 — Configure Nginx Virtual Host
# /etc/nginx/conf.d/myapp.conf
server {
listen 80;
server_name myapp.example.com;
root /var/www/myapp/public;
index index.php;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
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;
}
location ~ /.(?!well-known).* {
deny all;
}
}
nginx -t && systemctl reload nginx
Step 4 — Create the Database and Run Migrations
mysql -u root -p <<'SQL'
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'MyAppDBPass123!';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
SQL
cd /var/www/myapp
php artisan migrate
Step 5 — Configure SELinux for Nginx + PHP-FPM
# Allow Nginx to connect to PHP-FPM and write to storage
setsebool -P httpd_can_network_connect 1
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/myapp/storage(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/myapp/bootstrap/cache(/.*)?"
restorecon -Rv /var/www/myapp/storage /var/www/myapp/bootstrap/cache
Step 6 — Optimise for Production
cd /var/www/myapp
# Cache configuration, routes, and views for maximum performance
php artisan config:cache
php artisan route:cache
php artisan view:cache
# Install composer dependencies without dev packages
composer install --no-dev --optimize-autoloader
Conclusion
Laravel 11 on RHEL 9 with Nginx and PHP-FPM provides a production-ready PHP web application stack. The three most impactful production optimisations are: caching the configuration (artisan config:cache), caching routes (artisan route:cache), and using Redis for sessions and cache instead of the file driver — file-based sessions and cache do not scale across multiple application servers. For zero-downtime deployments, clear and regenerate the caches during deployment rather than leaving stale caches in place.
Next steps: How to Install Composer on RHEL 9, How to Configure PHP-FPM with Nginx on RHEL 9, and How to Deploy a Symfony Application on RHEL 9.