Deploying a Laravel application on an Apache web server is a common practice in the PHP ecosystem. Apache is one of the most stable, well-supported, and widely used web servers in the world. It continues to power a large portion of the internet, especially shared hosting platforms, managed hosting environments, and traditional VPS setups. Although more modern stacks like Nginx have become popular, Apache remains a dependable choice due to its extensive configuration capabilities, compatibility with PHP, and widespread availability.
When deploying Laravel on Apache, it is important to understand how Apache processes requests, how to configure virtual hosts, how Laravel interacts with the server, and how to properly set up rewrite rules, permissions, environment variables, optimizations, and security settings. A correctly configured Apache setup ensures a fast, secure, and stable production environment for your Laravel applications.
This extensive article provides an in-depth, step-by-step, and comprehensive explanation of how to deploy Laravel on Apache, what to configure, why certain settings are important, best practices, and common issues developers face during deployment.
Why Choose Apache for Laravel Deployment
Apache has many advantages that make it suitable for Laravel deployments:
- It is widely available on shared hosting platforms.
- It has strong compatibility with PHP and PHP-FPM.
- It supports .htaccess files for rewrite rules.
- It has extensive documentation and community support.
- It is easy to configure and maintain.
- It works well with mod_rewrite for clean URLs.
- It integrates with control panels like cPanel, Plesk, and DirectAdmin.
- It offers stable long-term performance.
Even for modern cloud servers, Apache remains a preferred option due to its flexibility and ability to host multiple applications under complex environments.
Understanding How Laravel Works With Apache
Laravel applications are served through the public directory. All incoming requests must be directed to:
public/index.php
This file bootstraps the Laravel framework and handles routing. Apache is configured to point the virtual host’s DocumentRoot toward the public directory, not the project root. This ensures security by preventing access to sensitive files such as:
- .env
- composer.json
- vendor directory
- storage
Apache uses mod_rewrite to redirect all requests to index.php so that Laravel’s routing system can handle them.
Preparing the Server Environment
Before deploying Laravel, ensure the server includes required components:
- Apache web server
- PHP or PHP-FPM with required extensions
- Composer
- MySQL or other database
- OpenSSL
- mod_rewrite enabled
Laravel typically requires extensions such as:
- mbstring
- tokenizer
- xml
- ctype
- json
- openssl
- pdo
- fileinfo
The server environment must meet the Laravel version’s system requirements.
Uploading the Laravel Application
You can upload your Laravel application to the server using:
- FTP or SFTP
- SSH
- Git
- Deployment scripts
- Control panel file managers
Place the project in:
/var/www/projectname
or inside the hosting account directory. The public directory should be set as the web-accessible folder.
Installing Dependencies With Composer
After transferring the files, navigate to the project directory and run:
composer install --no-dev --optimize-autoloader
This installs production dependencies and creates an optimized autoload map, improving performance.
Composer must be available on the server for this step.
Configuring the .env File
The environment file contains essential configuration, such as:
- Database connection
- APP_KEY
- Mail settings
- Caching settings
- Logging
- Session configuration
Create the .env file:
cp .env.example .env
Then generate the key:
php artisan key:generate
Ensure correct production settings before deployment.
Setting the Apache Document Root
The most important step in deploying Laravel on Apache is pointing the DocumentRoot to the public directory. A typical virtual host configuration looks like:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/projectname/public
<Directory /var/www/projectname/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
This ensures Apache serves only the public folder, protecting sensitive files.
Enabling Apache Modules
For Laravel to work correctly, mod_rewrite must be enabled:
a2enmod rewrite
Additionally, enable:
- mod_headers
- mod_php or PHP-FPM integration
- mod_ssl (for HTTPS)
Restart Apache:
systemctl restart apache2
Configuring .htaccess for Laravel
Laravel includes an .htaccess file inside the public directory, responsible for routing non-existing files to index.php.
The default .htaccess looks like:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This ensures that Laravel handles the request routing instead of Apache.
Setting Permissions
Laravel requires certain directories to be writable:
- storage
- bootstrap/cache
Use:
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
Correct permissions ensure caching, logging, and session files can be written.
Running Laravel Optimization Commands
For production deployment, run:
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
These commands improve application performance and reduce load time in Apache.
Using PHP-FPM With Apache
Apache can work with PHP via:
- mod_php
- PHP-FPM using proxy_fcgi
PHP-FPM provides better performance. Enable it with:
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm
This improves concurrency, stability, and memory usage.
Virtual Hosts for Multiple Laravel Applications
To host multiple Laravel apps, create separate virtual host files:
sudo nano /etc/apache2/sites-available/projectone.conf
sudo nano /etc/apache2/sites-available/projecttwo.conf
Each should point to its respective public directory:
DocumentRoot /var/www/projectone/public
DocumentRoot /var/www/projecttwo/public
Enable sites:
a2ensite projectone
a2ensite projecttwo
Reload the server.
Deploying Laravel on Shared Hosting
Most shared hosting environments already use Apache. Deployment involves:
- Uploading your Laravel files
- Moving public contents to public_html
- Updating index.php paths
- Setting environment variables in cPanel
- Running composer via the hosting terminal
Using shared hosting is simple because Apache and mod_rewrite are preconfigured.
Managing Laravel Deployments With cPanel
cPanel allows:
- Creating domains and subdomains
- Managing Apache virtual hosts
- Editing .htaccess
- Creating databases
- Installing SSL certificates
Deploying with cPanel involves mapping public_html to the public folder of Laravel.
Using Subdomains With Apache for Laravel Apps
To deploy a Laravel app on a subdomain:
subdomain.example.com
Point its DocumentRoot:
/var/www/projectname/public
This allows multiple apps under one domain structure.
Configuring SSL With Apache
SSL certificates improve security. Enable SSL with:
a2enmod ssl
a2ensite default-ssl
To install a Let’s Encrypt SSL certificate:
sudo certbot --apache
Apache updates virtual host settings for HTTPS automatically.
Setting Up Rewrite Rules for HTTPS Redirection
Force HTTPS with .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This ensures secure routing in production.
Using Environment Variables in Apache
Apache’s virtual host can define environment variables:
SetEnv APP_ENV production
SetEnv APP_DEBUG false
These settings override .env when needed.
Configuring PHP Settings for Laravel
Laravel requires specific PHP settings. Update php.ini:
- memory_limit
- upload_max_filesize
- post_max_size
- max_execution_time
Restart Apache after updating PHP settings.
Using Queues in Apache Deployments
Laravel queues require:
- Supervisor
- Redis or database queue driver
- Worker processes
Supervisor ensures that queue workers restart automatically after crashes.
Logging Configuration for Laravel on Apache
Apache logs requests in:
/var/log/apache2/
Laravel logs application events in:
storage/logs/
Correct permissions ensure proper log rotation and error tracking.
Caching Strategies in Apache Deployments
Performance improvements involve caching:
- Config cache
- Route cache
- View cache
- Opcode cache with OPCache
- HTTP caching with Cache-Control headers
Apache can serve static assets efficiently.
Database Configuration for Production
Ensure production databases:
- Use optimized indexes
- Enable query caching
- Use persistent connections
- Have strict SQL modes disabled if needed
Laravel’s configuration file must match production credentials.
Hardening Security for Laravel on Apache
Security steps include:
- Disable directory browsing
- Deny access to sensitive files
- Hide Apache version information
- Enable firewall rules
- Use HTTPS
- Restrict file permissions
Security improvements protect your production applications.
Preventing Public Access to Sensitive Files
Block sensitive directories using Apache rules:
<Directory /var/www/projectname/storage>
Deny from all
</Directory>
Never expose application code publicly.
Configuring Backups and Restoration
Automated backups include:
- Database backups
- Storage folder backups
- Application code backups
- Server snapshots
Scheduling regular backups ensures disaster recovery.
Deployment Automation With Apache
You can automate Laravel deployments using:
- Shell scripts
- Git hooks
- CI/CD pipelines
- Rsync
- SCP
Each automated deployment moves updated files to the server and restarts services.
Handling File Uploads in Apache
Ensure:
- Correct permissions in storage
- Correct symbolic link for public storage
- Correct PHP upload settings
Run:
php artisan storage:link
This exposes uploaded files correctly.
Common Issues When Deploying Laravel on Apache
Common problems include:
- 500 internal server errors
- Missing modules
- Incorrect .env permissions
- Wrong DocumentRoot
- mod_rewrite disabled
- Incorrect index.php paths
- Permission issues in storage
Most issues are caused by misconfigured virtual hosts or missing modules.
Debugging Laravel Errors on Apache
Debugging steps:
- Check Apache logs
- Check Laravel logs
- Disable caching temporarily
- Verify .env configuration
- Run composer diagnose
- Test PHP modules
Proper debugging helps identify deployment issues quickly.
Performance Optimization Tips for Apache
Optimize:
- Use OPCache
- Enable HTTP/2
- Enable GZIP compression
- Use KeepAlive
- Optimize PHP-FPM workers
- Limit .htaccess usage when possible
These improvements significantly speed up your Laravel application.
Scaling Laravel Applications on Apache
Scaling strategies include:
- Load balancing
- Using multiple servers
- Using Redis caching
- Optimizing database queries
- Using queues for heavy tasks
- Moving assets to CDN
Apache works well in multi-server environments.
Best Practices for Apache Deployment
Best practices include:
- Always point DocumentRoot to public
- Use correct permissions
- Use SSL
- Avoid exposing sensitive files
- Use PHP-FPM
- Use caching
- Keep software updated
- Use environment variables securely
Leave a Reply