Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
170 lines (125 sloc) 5.94 KB
title date draft thumbnail categories tags readmore summarize
PHP Installation on Ubuntu
2020-01-23 18:52:25 UTC
false
v1579288674/2020/php.jpg
SysAdmin
PHP
Ubuntu
VPS
Read the tutorial
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.3.

I’m currently using Ubuntu 18.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 be able to install PHP 7.3 you first need to add a third party repository. You may not have the add-apt-repository command available, so we'll install software-properties-common first. To do both of these things, run the following commands:

# sudo apt-get install software-properties-common
# sudo add-apt-repository ppa:ondrej/php

With the new repository added, update the package index:

# sudo apt update

You can now install PHP 7.3 along with some of it most common extensions:

# sudo apt install php7.3 php7.3-fpm php7.3-pgsql php-common php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline php7.3-mbstring php7.3-xml php7.3-gd php7.3-curl

After the installation is concluded, start the PHP 7.3 service and enable it to auto-start on boot:

# sudo systemctl start php7.3-fpm
# sudo systemctl enable php7.3-fpm

At any moment you can check the PHP 7.3 service status running:

# systemctl status php7.3-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/emanuelpina.ml

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;

    server_name emanuelpina.ml www.emanuelpina.ml;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-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:

<?php
phpinfo();

Now visiting the page https://emanuelpina.ml/info.php 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.3 | 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.3/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.3-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 >}}

[^1]: PHP-FPM settings tutorial. max_servers, min_servers, etc.

You can’t perform that action at this time.