How to install PHP 7.4-FPM with Apache on Ubuntu 18.04 in Google Cloud Platform. There are two distinct options to run PHP using the web server. One is using the PHP’s CGI and the other one is FPM.
FPM is a process manager to manage the FastCGI in PHP. Apache ships with
mod_php
by default and works with all major web servers. Withmod_php
there is a little performance issue because it locks out the process.You can also configure PHP-FPM pools to run as the different user that owns the website if you are hosting multiple websites on your server in a chroot environment setup.
Getting Started
Make sure your Ubuntu server is having the latest packages by running the following command.
sudo apt update sudo apt upgradeThis will update the package index and update the installed packages to the latest version.
Add PPA for PHP 7.4
Add the
ondrej/php
which has PHP 7.4 package and other required PHP extensions.sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt updateOnce you have added the PPA you can install PHP 7.4.
Install PHP 7.4 FPM
Now we shall install PHP 7.4-FPM and some common modules to run a PHP application like WordPress.
sudo apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-soap php7.4-zip php7.4-bcmath -yWait for the installation to complete.
Once the installation is complete verify the installation using the following command.
sudo service php7.4-fpm statusYou will receive an output similar to the one below.
Output ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-01-09 08:36:51 UTC; 1min 48s ago Docs: man:php-fpm7.4(8) Main PID: 15920 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 669) CGroup: /system.slice/php7.4-fpm.service ├─15920 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─15938 php-fpm: pool www └─15939 php-fpm: pool wwwInstall Apache
Once you have your PHP-FPM up and running you can install Apache web server.
sudo apt install apache2Configure Apache with PHP-FPM
By default Apache will use
mod_php
so now you can configure Apache to use PHP-FPM.Disable the default Apache vhost configuration.
sudo a2dissite 000-defaultEnable
proxy_fcgi
module.sudo a2enmod proxy_fcgiCreate a new Apache vhost configuration.
sudo nano /etc/apache2/sites-available/cloudbooklet.conf
Paste the below configuration in the file.
<VirtualHost *:80> ServerName External_IP_Address DocumentRoot /var/www/html <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> <FilesMatch ".php$"> SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Hit
CTRL + X
followed byY
andEnter
to save and exit the file.Now you can enable the new Apache configuration.
sudo a2ensite cloudbooklet.conf
Restart Apache.
sudo service apache2 restartTest PHP-FPM with Apache
Here we have configured
/var/www/html
as the webroot in the Apache configuration. So now you can navigate into that directory and create aphpinfo
file to check the setup.cd /var/www/htmlsudo nano info.phpPaste the following.
<?php phpinfo;Hit
CTRL + X
followed byY
andEnter
to save and exit the file.Now go your browser and point it to your server IP address or domain name followed by the
info.php
. So your address will look like thishttp://IP_Address/info.php
You will see the PHP info page and confirm PHP-FPM is used with Apache.
PHP-FPM Configuration
PHP INI:
/etc/php/7.4/fpm/php.ini
Pool config:
/etc/php/7.4/fpm/pool.d/www.conf
Create New PHP-FPM Pool with different user
By default Nginx and Apache runs as
www-data
user. PHP-FPMwww.conf
is also configured to run aswww-data
user. If you have multiple websites and wish to keep them isolated with chrooted setup and run them with their own user. You can create multiple PHP-FPM pools with different users.Create a new PHP-FPM pool configuration.
sudo nano /etc/php/7.4/fpm/pool.d/user1.confPaste the following.
[user1] user = user1 group = group1 listen = /run/php/php7.4-fpm-user1.sock listen.owner = www-data listen.group = www-data pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3Now create a new Apache vhost configuration file and set the handler to the new pool you have created.
sudo nano /etc/apache2/sites-available/site1.conf
<VirtualHost *:80> ServerName domain.com DocumentRoot /var/www/html/site1 <Directory /var/www/html/site1> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> <FilesMatch ".php$"> SetHandler "proxy:unix:/var/run/php/php7.4-fpm-user1.sock|fcgi://localhost/" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>Enable new site.
sudo a2ensite site1.confMake sure the new site webroot is owned by the user specified in the pool configuration and inside the specified group.
Restart Services
Once the configuration is completed you need to restart PHP-FPM and Apache for the changes to take effect.
sudo php-fpm7.4 -t sudo service php7.4-fpm restartsudo service apache2 restartYou can create as many pools you like using the above mentioned setup with PHP-FPM. These are the flexibility available with PHP-FPM.
Conclusion
Now you have learned how to install PHP 7.4-FPM with Apache and configure Apache. You have also learned to setup PHP-FPM pools for multiple users.
How to Install PHP-FPM with Apache on Ubuntu 18.04 – Google Cloud
How to install PHP 7.4-FPM with Apache on Ubuntu 18.04 in Google Cloud Platform. There are two distinct options to run PHP using the web server. One is using the PHP’s CGI and the other one is FPM.
FPM is a process manager to manage the FastCGI in PHP. Apache ships with
mod_php
by default and works with all major web servers. Withmod_php
there is a little performance issue because it locks out the process.You can also configure PHP-FPM pools to run as the different user that owns the website if you are hosting multiple websites on your server in a chroot environment setup.