On this page
- Why pair Apache with PHP-FPM for Laravel?
- Which packages should you install?
- How do you connect Apache to PHP-FPM?
- How do you place the code and set ownership?
- What does the Apache virtual host look like?
- How do you finish the environment and enable HTTPS?
- Where do queues, the scheduler and timezone fit in?
- How do you troubleshoot an Apache Laravel site?
- Questions
Why pair Apache with PHP-FPM for Laravel?
PHP-FPM lets Apache use the event MPM, which handles many keep-alive connections with few threads, while PHP runs in its own pool with separate limits. The older mod_php setup forces the prefork MPM and loads PHP into every Apache process, including those that only send images or CSS. Either works, but FPM gives you clearer memory control on a VPS and lets you change PHP versions without touching Apache. If you already followed the LAMP stack guide with mod_php, the steps below show how to switch.
Apache also has one practical advantage for Laravel: it reads the .htaccess file that ships in public/, so routing works without hand-written rewrite rules.
Which packages should you install?
Install Apache, PHP-FPM with the Laravel extensions, a database server and the tools for fetching code.
sudo apt update
sudo apt install -y apache2 mariadb-server php-fpm php-cli php-mysql \
php-mbstring php-xml php-curl php-zip php-bcmath php-intl unzip git
apache2 -v
php -vThe PHP branch depends on the release: 8.3 on Ubuntu 24.04, 8.2 on Debian 12 and 8.4 on Debian 13. Ubuntu 22.04 carries 8.1, which current Laravel no longer accepts, so raise PHP first with the upgrade guide. Composer comes from its own verified installer, described in Composer for PHP projects.
How do you connect Apache to PHP-FPM?
Enable the FastCGI proxy modules and the configuration snippet that the PHP-FPM package drops into conf-available.
ls /etc/apache2/conf-available/ | grep fpm
sudo a2enmod proxy_fcgi setenvif rewrite headers
sudo a2enconf php8.3-fpm
sudo a2dismod php8.3 mpm_prefork 2>/dev/null
sudo a2enmod mpm_event
sudo systemctl restart apache2 php8.3-fpmReplace 8.3 with the version from the ls output. The a2dismod line only matters if mod_php was installed earlier; on a clean server it simply reports that the module does not exist. Confirm it with apachectl -M | grep -E "mpm|proxy_fcgi|rewrite": you want mpm_event_module, proxy_fcgi_module and rewrite_module in the list.
How do you place the code and set ownership?
Create the site folder, clone as the deploy user, and install dependencies without development packages.
sudo mkdir -p /var/www/app.example.net
sudo chown deploy:www-data /var/www/app.example.net
cd /var/www/app.example.net
git clone https://git.example.net/team/app.git .
composer install --no-dev --optimize-autoloader --no-interaction
sudo chgrp -R www-data .
sudo chmod -R g+rX,o-rwx .
sudo chmod -R g+w storage bootstrap/cacheThis leaves the source readable but not writable for www-data, the account that both Apache and PHP-FPM use on Debian-based systems. Only the cache and storage paths accept writes, which limits what a compromised script could change. Create the database and its user exactly as for any MariaDB app; the users and privileges guide shows the grants.
What does the Apache virtual host look like?
One vhost per site, with the document root on public and overrides allowed so the shipped .htaccess takes effect. Write /etc/apache2/sites-available/app.example.net.conf:
<VirtualHost *:80>
ServerName app.example.net
DocumentRoot /var/www/app.example.net/public
<Directory /var/www/app.example.net/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch "^\.">
Require all denied
</FilesMatch>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
ErrorLog ${APACHE_LOG_DIR}/app.example.net-error.log
CustomLog ${APACHE_LOG_DIR}/app.example.net-access.log combined
</VirtualHost>sudo a2ensite app.example.net
sudo a2dissite 000-default
sudo apachectl configtest
sudo systemctl reload apache2configtest must print Syntax OK before the reload. The FilesMatch block refuses dot files such as a stray .env copy inside public.
How do you finish the environment and enable HTTPS?
Generate the key, switch the app to production mode, migrate and cache.
cp .env.example .env
php artisan key:generate
chmod 640 .env
# set APP_ENV=production, APP_DEBUG=false, APP_URL=https://app.example.net
# set DB_CONNECTION=mysql and the DB_ values
php artisan migrate --force
php artisan storage:link
php artisan optimizeOpen the firewall and request a certificate; Certbot writes a second vhost for port 443 and a redirect.
sudo ufw allow 'Apache Full'
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d app.example.net
curl -I https://app.example.netA 200 or 302 status with a laravel_session cookie proves Apache, PHP-FPM and Laravel are linked. Renewal runs from a systemd timer; sudo certbot renew --dry-run tests it. More on redirects is in HTTPS and www redirects.
Where do queues, the scheduler and timezone fit in?
They run outside Apache and are set up the same way on either web server. In short: a systemd unit running php artisan queue:work as www-data, one cron line for schedule:run every minute, and the timezone value in config/app.php kept at UTC unless you have a firm reason to change it. The Laravel with Nginx guide has the full unit file and cron entry, which you can copy unchanged. Remember php artisan queue:restart after each release, so workers load the new code.
How do you troubleshoot an Apache Laravel site?
- Raw PHP source shown in the browser: the
php8.x-fpmconf is not enabled. Runa2enconffor your version and reload. - 404 on every route but the home page:
mod_rewriteis off orAllowOverrideisNone. Both appear in the vhost and module list above. - 503 Service Unavailable: PHP-FPM is down or its socket path changed. Check
systemctl status php8.3-fpmand/var/log/apache2/app.example.net-error.log. - Permission denied in storage/logs: group write was lost, often after a deploy as root. Reapply the
chgrpandchmodlines. - Slow first request after deploy: caches were cleared. Run
php artisan optimizeas part of every release.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Deploy Laravel with Apache
Can I keep mod_php instead of PHP-FPM?
Yes, it works with libapache2-mod-php and the prefork MPM. PHP-FPM is preferred because it uses less memory per connection and isolates PHP settings per pool.
Is AllowOverride All a security risk?
It lets .htaccess files change settings, so only enable it on the public directory you control. Laravel needs it for its routing file there.
Should I move the .htaccess rules into the vhost?
You can, then set AllowOverride None to skip per-request file lookups. The gain is small for most sites, so many keep the shipped file.
Which is faster for Laravel, Apache or Nginx?
Most time is spent in PHP and the database, so either server performs similarly once PHP runs in FPM. Pick the one your team knows.
How do I host a second Laravel app on the same server?
Add another folder, another database, another vhost and ideally a separate PHP-FPM pool, as explained in the PHP-FPM pools guide.