Skip to content

C) NGINX, MySQL, PHP Installation 3/4

Intro

The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications written in PHP. Background data is stored in a MySQL database and dynamic processing is handled by PHP. PHP (Hypertext Preprocessor) is a popular programming language for the implementation of websites. The PHP code is executed on the server before the web page is sent to the browser, which means that PHP does not require support from the browser and can be used to process files and databases on the server, for example.

Step 1 - MySQL installation

We will install a database system for storing and managing the site's information. MySQL is a popular database management system used in PHP environments.

Bash
sudo apt install mysql-server
When prompted, press Y and then ENTER to confirm the installation.

Step 2 - Nginx Installation

Nginx is already installed from previous sections. If it is not installed, run:

Bash
sudo apt install nginx

Step 3 – PHP installation

When Nginx is installed to serve www content and MySQL is installed to store and manage data in the database, PHP can be installed to process the code and create dynamic content on the web server.

While Apache embeds a PHP interpreter in every request, Nginx requires an external program to handle the PHP processing and act as a bridge between the PHP interpreter itself and the web server. This allows for better overall performance on most PHP-based websites, but requires additional configuration. You need to install php8.3-fpm, which stands for "PHP fastCGI Process Manager" and uses the current version of PHP, in order for Nginx to forward PHP requests to this software for processing. In addition, you need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. PHP core packages are automatically installed as dependencies.

Install the php8.3-fpm and php-mysql packages by running:

Tip

Remember what php version you did install to your machine

Bash
sudo apt install php8.3-fpm php-mysql

Step 4 — Configuring Nginx to use the PHP processor

Editing the previously created test.local server block (Server Block). Add the following configuration to the server block configuration file:

Bash
sudo nano /etc/nginx/sites-available/test.local
Nginx Configuration File
location ~ \.php$ {
        root /var/www/test.local/html;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
     }

End result should look like this:

Nginx Configuration File
server {
    listen 80;
    listen [::]:80;

    server_name test.local www.test.local;
    index index.html index.htm index.nginx-debian.html;
    location ~ \.php$ {
        root /var/www/test.local/html;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        }

     location / {
                try_files $uri $uri/ =404;
        }
}
§ What each of these directives and location blocks do:

listen Specifies which port Nginx listens on. In this case, it listens on port 80, which is the default port for HTTP.

root — Specifies the document root where the files provided by this website are stored.

index Specifies the order in which Nginx prioritizes directory files for this website. It is common practice to list index.html files with a higher priority than index.php files in order to quickly set up a maintenance landing page in PHP applications. These settings can be configured to better meet application needs.

server_name Specifies which domain names and/or IP addresses this server block should respond to. Let's point this instruction to the server's domain name or public IP address.

location / The first location block contains a try_files statement that checks for the existence of files or directories matching the URL request. If Nginx cannot find the correct resource, it will return a 404 error.

location ~ \.php$ This location block handles the actual PHP processing by pointing to Nginx's fastcgi-php.conf configuration file and the php8.3-fpm.sock file, which tells which socket is associated with php8.3-fpm .

location ~ /\.ht The last location block handles .htaccess files, which are not handled by Nginx. By adding a deny all statement, if any .htaccess files happen to find their way into the root of the document, they will not be displayed to visitors.

Check that there are no syntax errors in the files.

Bash
sudo nginx -t
sudo systemctl restart nginx

Step 5 –Testing the functionality of PHP

The system should now be fully installed. Let's test the system to make sure Nginx can pass .php files correctly to the PHP processor.

Create a PHP test file at the root of the document. Let's open a new file named info.php from the root of the file in the nano text editor:

Add the following lines to the new file. This is valid PHP code that returns data from your server:

Bash
nano /var/www/test.local/html/info.php
Bash
<?php
phpinfo();
?>

Let's test the functionality of the info.php file by going to the address test.local in the browser of the Demo-Ubuntu test machine

Bash
http://test.local/info.php 

Once PHP's operation is verified, the info.php file can be deleted for data security reasons. The file shows potentially security-threatening information from the installed information system.

Bash
sudo rm /var/www/test.local/html/info.php