Lesson 1.2: Apache and nginx with PHP-FPM
Many hosts already run Apache or nginx with PHP-FPM, and many customers expect it. This lesson shows the traditional setup the way chapter 8 describes it: the rewrite rules, the virtual host, the PHP-FPM handler, the derived nginx block and the file ownership rule that prevents most permission trouble.
Warning
.htaccess slows down every request. When .htaccess files are enabled (AllowOverride anything but None), let alone
used, Apache looks for them and reads and evaluates them on every request, in every directory of the path, and
page loading times go up drastically (measured). The only real fix is to switch the feature off for
the virtual host: AllowOverride None in the HTTPS virtual host, and the rules in the virtual host's own
configuration as mod_rewrite rules. That is far faster in production speed tests and well worth the time it takes
to optimize a production configuration. Switch over right away: copy the rules of the
example HTTPS virtual host into your virtual host (with the static cache, also
staticcache_offload.conf), set AllowOverride None, run apachectl configtest, reload Apache and
test the site. The same rules for other servers: nginx, LiteSpeed,
Caddy, IIS, Velocity (built in); where virtual host rules go in a control
panel: Plesk, cPanel, DirectAdmin, ISPConfig.
On LiteSpeed or OpenLiteSpeed, the server's own page cache can also answer anonymous visitors and crawlers without PHP: Page caching with LiteSpeed and OpenLiteSpeed.
Note
The exercise needs root on the learner's own virtual machine. Never practise on a server that hosts other sites.
At a glance
| Course | Hosting and operations |
| Module | Module 1: Serving the site |
| Duration | 45 minutes (15 presenting, 30 exercise) |
| Level | Intermediate |
| Audience | System administrators and hosting support staff |
| Prerequisites | Lesson 1.1; a Linux virtual machine with Apache, PHP-FPM and a training installation |
Learning objectives
After this lesson, learners can:
- enable the rewrite rules by copying
.htaccess_rootto.htaccess; - write a virtual host whose document root is the installation root and whose PHP goes to a PHP-FPM pool;
- check and reload Apache safely with
apachectl configtest; - explain how the nginx server block of chapter 8 maps to
.htaccess_rootand why it is marked as derived; - say which directories the PHP user must be able to write, and why all writers should be one user.
Trainer's notes
How to present it
Source: Chapter 8, Apache with PHP-FPM, nginx with PHP-FPM and File permissions and ownership.
- The rewrite rules (3 minutes).
cp .htaccess_root .htaccess: copy, do not move, so an update can bring a new version..htaccess_root_staticis only for a generated static cache. Show the console way as well:./console exp:setup:htaccess(a dry run that says what it would do), then--apply. It refuses to replace a.htaccessthat differs and prints the diff, which is also how learners see what an upgrade changed (Setting up .htaccess and config.php from the console). - The virtual host (4 minutes). Show the
<VirtualHost *:80>example of section 8.5.1:DocumentRoot,AllowOverride Allso the.htaccessis read, and the<FilesMatch \.php$>handler that sends scripts to the PHP-FPM socket. Modules:mod_rewrite,mod_proxy,mod_proxy_fcgi; for HTTPSmod_ssl, for HTTP/2mod_http2. - nginx (4 minutes). Exponential ships no nginx configuration. The block in section 8.6 is a translation of
.htaccess_rootand must be tested on the site and compared after every upgrade. Explain nginx's location precedence: exact matches, then^~prefixes, then regular expressions in file order, then the longest plain prefix. That is why the dot-file and script rules come first. - Plesk (2 minutes). Plesk writes the virtual host itself: point the document root at the installation root,
choose "FPM application served by Apache", keep
.htaccess. Each domain's pool is served by a unit such asplesk-php85-fpm;systemctl restart php-fpmrestarts a different master (section 8.5.3). - One user (2 minutes). Permission problems almost always come from two users writing the same files. Run the pool, the Velocity workers, cronjobs and console scripts as the site's user (section 8.7.2).
Common questions
Can I avoid .htaccess for speed? Yes: copy the rules into the <Directory> block and set AllowOverride None.
In server context every pattern needs a leading slash; take the rules from .htaccess_root, not from the older
doc/eng-us/examples/ezpublish.conf.
Every page is a 404. Why? .htaccess is not read (AllowOverride) or mod_rewrite is off.
A 500 error after the switch? Read the Apache and PHP-FPM error logs; chapter 8 names them as the first place to look.
Do security headers need Apache configuration? The kernel sends them on its pages
(site.ini [HTTPHeaderSettings] SecurityHeaders[]); for static files add them with mod_headers if wanted
(chapter 13).
Pitfalls
- Distribution differences. Debian names the service
apache2and enables modules witha2enmod; Red Hat style systems call ithttpd. Prepare both command lists for a mixed class. - Wrong socket path. The
SetHandlerline must name the socket of the learner's PHP-FPM pool; have learners look it up in their pool file before editing. - Root-owned cache files. A learner who ran a console command as root breaks the site for the pool user. Show
ls -l var/and fix ownership as in step 10.
Hands-on exercise
Use your own Linux virtual machine with Apache, PHP-FPM and a training installation in, for example,
/var/www/training.
Change to the installation root and run
cp .htaccess_root .htaccess, or./console exp:setup:htaccess --applyas the site's user. Run./console exp:setup:statusafterwards: it names.htaccess_rootas the template.Enable the modules (Debian:
a2enmod rewrite proxy_fcgi), or check they are loaded on your system.Find your PHP-FPM pool's socket path in its pool file (the
listenline) and note the pool'suser.Create a virtual host file with the example of section 8.5.1, using your document root, a training host name such as
training.example.invalidand your socket path.Run
apachectl configtestand fix anything it reports until it printsSyntax OK.Reload Apache (
systemctl reload apache2orsystemctl reload httpd).Add the host name to the virtual machine's
/etc/hostspointing at127.0.0.1.Run the four checks of lesson 1.1 against
http://training.example.invalid/.Open the front page in a browser on the virtual machine (or with
curl -sI).Give the writable directories to the pool user, as section 8.7.1 shows:
sudo chown -R <pool-user>:<pool-user> design extension settings varRead the nginx block of section 8.6 and find the line that corresponds to the final
RewriteRuleof.htaccess_root.
Expected result
apachectl configtestprintsSyntax OK; the reload gives no error.- The four checks give the same answers as in lesson 1.1 (
404,404,200,0). - The front page answers
200(or a redirect) through Apache. - New files under
var/belong to the pool user after a few requests. - In step 11 the learner points at
location /, the plain prefix that sends everything else toindex.php. - The trainer checks by asking each learner for the result of the
.git/configrequest through Apache.
Quiz
- Why is
.htaccess_rootcopied rather than moved to.htaccess? - Which Apache setting must allow the
.htaccessfile to be read? A.Options IndexesB.AllowOverride AllC.DirectoryIndexD.ProxyPreserveHost - On a Plesk server, why does
systemctl restart php-fpmnot apply a change to a site? - True or false: the project ships a tested nginx configuration in its repository.
- A cache file written by
rootbreaks the site. What is the rule that prevents this?
Answers
- So an update can bring a new version of
.htaccess_root(section 8.5.1). - B. Without
AllowOverridethe rules are not read and every page is a 404. - Plesk's sites run under a different master, such as
plesk-php85-fpm; thephp-fpmunit is one no Plesk site uses (section 8.5.3). - False. The nginx block in chapter 8 is derived from
.htaccess_rootand must be tested. - Run everything that writes
var/(pool, Velocity workers, cronjobs, console) as the site's user, or use a shared group with group-writable directories (section 8.7.2).
Further reading
- Apache with PHP-FPM: the host's view of the setup.
- nginx with PHP-FPM: the derived block and its caveats.
- Plesk: Exponential on a Plesk server.
- Setting up .htaccess and config.php from the console:
exp:setup:htaccess,exp:setup:statusand which servers read.htaccess. - Writable directories and permissions: ownership in detail.
Related pages
- Module 1: Serving the site
- Hosting and operations: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.