Install php-fpm
Published on: 5th Dec 2020
Updated on: 19th Sep 2022
What is PHP
PHP is for server side programming for developing websites or writing scripts for processing some tasks.
Why
You have to install this component if you have web pages developed using PHP such as WordPress.
Here's how you install PHP
-
To install
php-fpm
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php7.3-fpm
After installation is completed, you have to edit the Nginx site configuration. See Install Nginx on how to install and configure it.
-
Assign permission for current user, without this, might get permission issue when PHP trying to access the directory.
sudo chown -R $USER:$USER /var/www/your_domain
-
To test if the PHP page is working, you have to create a test page and then load it in the browser.
sudo nano /var/www/your_domain/html/info.php
-
File contents of
info.php
<?php phpinfo(); ?>
-
To view the status of fast CGI process manager (FPM):
sudo systemctl status php7.3-fpm
-
In case you need Sqlite, you will have to install SQLite component,
sudo apt-get install php7.3-sqlite
-
After that, enable any extension by editing the PHP configuration,
sudo nano /etc/php/7.3/fpm/php.ini
Note: if you installed version other than 7.3, please change it to the correct version number.
-
In nano editor, press CTRL+w to search text. Type "pdo_sqlite" and press Enter key. Then, uncomment (remove the ";" symbol) that line and save it.
.... ;extension=pdo_pgsql extension=pdo_sqlite ;extension=pgsql ....
-
Restart PHP daemon after edited the configuration file,
sudo systemctl restart php7.3-fpm
-
Finally, edit the Nginx configuration file so that it routes the '.php' request to
php7.3-fpm
daemon.sudo nano /etc/nginx/sites-available/default
-
Add the following,
# route the request to php-fpm daemon. location ~\.php$ { include snippets/fastcgi-php.conf; # with php-fpm fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; }
-
Save the changes by pressing CTRL+O.
-
Exit the editor with CTRL+X.
-
Ensure that the Nginx configuration does not have a typo.
sudo nginx -t
-
Restart Nginx
sudo systemctl restart nginx
-
-
To troubleshoot any error,
# restart PHP service - in case any error, you will find it in syslog sudo systemctl restart php7.3-fpm # look for the last few logs tail /var/log/syslog
-
For example, you found the following error appear in syslog:
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_sqlite' PHP Warning: Module 'sqlite3' already loaded
-
This means that Sqlite extension has already been loaded as per the setting in the following directory:
ls /etc/php/7.3/fpm/conf.d
-
To resolve this issue, you have to disable the Sqlite extension in
php.ini
. -
To upgrade from x to v7.3
sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt install php7.3 sudo apt purge --auto-remove php7.2-fpm
References
-
How to Connect NGINX to PHP-FPM Using UNIX or TCP/IP Socket: https://www.tecmint.com/connect-nginx-to-php-fpm/
-
19.Sep.2022: how To Host Multiple Websites Securely With Nginx And Php-fpm On Ubuntu 14.04: https://www.digitalocean.com/community/tutorials/how-to-host-multiple-websites-securely-with-nginx-and-php-fpm-on-ubuntu-14-04
Related posts
Jump to #UBUNTU blog
Author
Lau Hon Wan, software developer.