TroubleChute Logo
DEBIAN LINUX

Install PHP for Nginx server (Quick Guide | PHP FPM)


Published: Jul 3, 2024
Last Edit: Jul 3, 2024
Linux Web Hosting
337 Words, 2 Minutes.

Watch the video:


Timestamps:
0:00 - Intro/Explanation
0:14 - Installing PHP8.3
1:00 - Installing PHP Extensions
1:52 - Linking PHP with Nginx
3:09 - Verify sites have PHP handling set

Why install PHP-FPM with Nginx?

Nginx by default doesn’t have a way to work well with PHP. However, being as modular as it is: It’s super easy to install and start using PHP on your server.

Just be aware of the security holes that could be introduced – PHP and Nginx have had many years getting it just right, but remember that what you put on your server may introduce other worrying holes in your security. I’m sure you’re aware of all of this anyway, besides the point.

How to install PHP-FPM

If you’re using Ubuntu the simplest way is to just install the latest version of PHP-FPM via Ondřej Surý’s Launchpad PPA. There are other ways of doing this if you don’t want to use this, or can’t use PPAs.

1
2
3
4
5
6
7
sudo apt update && sudo apt upgrade

# Install PHP:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.3-fpm

Now we can install PHP extensions. These are what I’ll be using for the project I’m working on, so yours may be different. For example:

1
sudo apt install php8.3-common php8.3-mysql php8.3-xml php8.3-xmlrpc php8.3-curl php8.3-gd php8.3-imagick php8.3-cli php8.3-dev php8.3-imap php8.3-mbstring php8.3-opcache php8.3-soap php8.3-zip php8.3-redis php8.3-intl

Then we can customize PHP’s settings to better suit your server:

1
sudo nano /etc/php/8.3/fpm/php.ini

I’ll be changing upload_max_filesize, post_max_size, memory_limit, max_execution_time, max_input_vars and max_input_time - the last is unlimited by default.

Then, restart your PHP installation.

1
sudo service php8.3-fpm restart

Verify installation

Finally, we need to make sure that Nginx is using the correct version.

In your /etc/nginx/sites-enabled for your website, you can enable PHP by adding the following to your website’s server block:

1
2
3
4
5
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}

This way we can manually set which websites can and can’t use PHP.

TroubleChute © Wesley Pyburn (TroubleChute)
Support Me Privacy Policy Cookies Policy Terms of Service Change privacy settings Contact