My blog built with Hugo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5.3 KiB

title date draft thumbnail categories tags readmore summarize update
PHP Installation on Ubuntu 2020-01-23T18:52:25Z false v1579288674/2020/php.jpg [SysAdmin] [PHP Ubuntu VPS] Read the tutorial true true

On the way to install Nextcloud we've already completed the initial setup of our VPS, the installation of Nginx and the installation of PostgreSQL.

I will now cover the installation of PHP 7.4.

I’m currently using Ubuntu 20.04, but these instructions are equally valid for other Ubuntu versions.

Install PHP

PHP (recursive acronym for PHP: Hypertext Preprocessor) is an open source server side scripting language, widely used to create dynamic interactive web pages.

To install PHP 7.4 along with some of it most common extensions run the following command:

# sudo apt install php7.4 php7.4-common php7.4-fpm php7.4-pgsql

And that's it. At any moment you can check the PHP 7.4 service status running:

# systemctl status php7.4-fpm

Test PHP Processing

To test PHP Processing you first need to configure Nginx to use it for dynamic content.

This is done at the server block level. For this example edit the server block you've created earlier. To do so, run:

# sudo nano /etc/nginx/sites-available/

And edit the server block to look like this:

server {
    listen 80;
    listen [::]:80;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;


    location / {
            try_files $uri $uri/ =404;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    location ~ /\.ht {
        deny all;

Test to make sure that there are no syntax errors in your Nginx files:

# sudo nginx -t

If there aren’t any issues, restart Nginx to enable the changes:

# sudo systemctl reload nginx

Now let's create a test PHP file in the website root folder:

# sudo nano /var/www/html/info.php

Add the following content to the file, which is a valid PHP code that generates a page containing information about the server:


Now visiting the page on your browser you should see a page generated by PHP containing structured information about your server.

If you see this page then Nginx is successfully communicating with PHP!

At the end of this test, don't forget to remove the PHP file you created:

# sudo rm /var/www/html/info.php

This is imporant because it exposes important information about your server and can make it vulnerable.

Tune PHP

Similar to what we did with PostgreSQL, you can tune some PHP settings depending on the hardware at your disposale.1

To proceed with this you need to know three things: how many CPU cores your server have; how much memory (RAM) you can dedicate to PHP; and how much memory the average PHP process consume.

To find out how many CPU cores your server has, run the following command:

# echo Cores = $(( $(lscpu | awk '/^Socket/{ print $2 }') * $(lscpu | awk '/^Core/{ print $4 }') ))

When determining how much memory you can dedicate to PHP, keep in mind that the server is also running Nginx and PostgreSQL. How much memory are these other processes consuming? For example, if you have 4GB of RAM and the other processes are consuming 1GB, that leaves you with 3GB – or 2GB if you want to play safe.

Finally, to get a general idea on how much memory each PHP process is consuming, run:

# ps --no-headers -o "rss,cmd" -C php-fpm7.4 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

The settigns you need to change are all in the www.conf file. To edit it, run:

# sudo nano /etc/php/7.4/fpm/pool.d/www.conf

Search for each one of the following settings and change it accordingly.

To get a good value for pm.max_children, take the memory that you want to allocate to PHP and divide it by the average memory that is consumed by each PHP process. For example, if you want to allocate 2GB (2000MB) and each process consumes about 50MB. Dividing 2000 by 50 we get around 40.
So, set pm.max_children to 40.

For pm.start_servers, multiply the number of cores that you have by 4.
If you have 2 cores: 2 x 4 = 8
So, set pm.start_servers to 8.

For pm.min_spare_servers, multiply the number of cores that you have by 2.
If you have 2 cores: 2 x 2 = 4
So, set pm.min_start_servers to 4.

For pm.max_spare_servers, multiply the number of cores on your server by 4.
If you have 2 cores: 2 x 4 = 8
So, set pm.max_start_servers to 8. The same used before for pm.start_servers.

To finish, just restart the PHP-FPM service:

# sudo systemctl restart php7.4-fpm

And that's all 😀

What's next?

With Nginx, PostgreSQL and PHP up and running you're now ready to install Nextcloud!

{{< call-for-contribution >}}