LEMP - Nginx Web Server, MySQL / MariaDB Database, and PHP
sudo apt update
sudo apt upgrade
sudo apt install nginx
sudo apt install mariadb-server
Next, we’ll use a script (mysql_secure_installation) provided by the mariadb-server package to restrict access to the server and remove unused accounts because the default setup makes your MariaDB installation unsafe.
sudo mysql_secure_installation
After running the above command, you will be prompted to enter the MariaDB root password. Just leave the root password empty, and press the Enter key. For the rest, type Y and hit Enter.
We clarify that the password specified above for the MariaDB root accounts is only for remote users. To log in from the host we installed it on, you do not need to enter a password and will not be asked for one.
To check if the database server is functioning normally:
sudo mysql
The server console should come up. Then, run a simple query:
select version();
In response to your query, the MariaDB server should return its version. Finally, to exit the MariaDB shell and return to the system terminal, use the quit command.
sudo apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcache
Now that we’ve installed all of the LEMP components on our Ubuntu 22.04 system, we need to edit the default Nginx virtual host configuration file.
sudo vim /etc/nginx/sites-enabled/default
Add the following lines to the default server block to allow Nginx to process PHP files:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
Test the modified Nginx configuration file for syntax errors by entering the following command:
sudo nginx -t
When you are ready, restart Nginx to make the changes take effect:
sudo systemctl restart nginx
Finally, let’s create a test PHP file to verify that PHP-FPM works and is integrated with Nginx. In the default server block above, our site is being served from /var/www/html, so we’ll create a test file there:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php
Now, you can access “test.php” from a web browser, using your site’s domain or server’s IP address followed by “/test.php.” A web page with complete information about your PHP installation appear.