LEMP stack (Linux, Nginx, MySQL, and PHP) is a software collection comprising open source applications used to host PHP web applications. Nginx which is pronounced as “Engine-X” is a popular high-performance web server. MySQL is a popular relational database server and PHP is the backend programming language where dynamic processing is handled. This tutorial demonstrates how to install the LEMP stack on Ubuntu 22.04.
Also check: How to install LAMP on Ubuntu 22.04
Step 1: Update your system
First and foremost, log into your Ubuntu 22.04 server and update the system repositories and packages as shown.
$ sudo apt update && sudo apt upgrade
Step 2: Install Nginx web server
Nginx is a high-performance web server and reverse proxy used for hosting high-traffic websites. NGINX is available in the default Ubuntu repositories and you can Install it using the APT package manager as follows.
$ sudo apt install nginx
Nginx service runs automatically once installed. You can verify this using the command
$ sudo systemctl status nginx
The following output is sure proof that the web server is running. Additionally, you can confirm that the web server is up and running by browsing the server’s IP address or Fully qualified domain name:
http://server-ip or domain name
If you cannot see this page, chances are that you are running behind a firewall and therefore, you need to allow HTTP service. So, Run the following command to allow both HTTP and HTTPS traffic on UFW firewall
$ sudo ufw allow 'Nginx Full'
If you want to allow either ‘HTTP’ or ‘HTTPS’ specify ‘Nginx HTTP’ or ‘Nginx HTTPS’.
Then reload the firewall to apply the changes
$ sudo ufw reload
Step 3: Install MariaDB database server
MariaDB is an opensource relational database server that stores data in tables. To install MariaDB, run the command.
$ sudo apt install mariadb-server mariadb-client -y
Next, check the status of mariadb using the following command:
$ sudo systemctl status mariadb
The following output indicates that the database server is up and running.
If the MariaDB service is not running, start it with the following command:
$ sudo systemctl start mariadb
Then enable MariaDB to start automatically upon system boot, run the command:
$ sudo systemctl enable mariadb
The default settings of MariaDB are not secure and the database server is prone to system breaches. As such we need to go an extra step to secure it. Therefore, run the following script.
$ sudo mysql_secure_installation
Configure a root password for authentication
For the remaining prompts, simply type ‘Y’ to configure MariaDB to the recommended guidelines.
To log into MariaDB as root, run the command:
$ sudo mysql -u root -p
Step 4: Install PHP
By default, Ubuntu 22.04 provides PHP 8.1. It provides a myriad of enhancements and performance improvements over PHP8.0. So, run the following command to install PHP 8.1 and other extensions that are needed for it to work properly:
$ sudo apt install php8.1 php8.1-fpm php8.1-mysql php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl
Next, start PHP-FPM and enable it to start automatically at boot time:
$ sudo systemctl start php8.1-fpm $ sudo systemctl enable php8.1-fpm
Then confirm that it is running.
$ sudo systemctl status php8.1-fpm
Next, configure PHP-FPM by editing the following values in php.ini file for better performance
Open the php.ini file using the nano editor as follows:
$ sudo nano /etc/php/8.1/fpm/php.ini
Next, search for the following values in the file and update them accordingly.
upload_max_filesize = 32M post_max_size = 48M memory_limit = 256M max_execution_time = 600 max_input_vars = 3000 max_input_time = 1000
Once we have modified PHP settings, we need to restart PHP-FPM for the changes to take effect.
$ sudo systemctl restart php8.1-fpm
Step 5. Configure Nginx Virtualhost
You need to make changes to the default Nginx virtualhost file to configure Nginx to start using PHP. So access the configuration file.
$ sudo nano /etc/nginx/sites-enabled/default
Head over to the server block and ensure it looks as shown.
server { listen 80; root /var/www/html; index index.php; server_name example.com www.example.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
Save the changes and exit. Then verify that all the Nginx settings are okay using the command:
$ nginx -t
To apply the changes, restart Nginx and PHP-FPM servce.
$ sudo systemctl restart nginx php8.1-fpm
Step 6. Nginx PHP Processing on Ubuntu 22.04
Now that PHP is fully enabled on Nginx, you can test the Nginx PHP processing.
To test this, we are going to create a sample PHP info page
$ echo "<?php phpinfo(); ?>" > /var/www/html/info.php
After that, open your browser and navigate to the following address:
http://server-ip/info.php
You will see the following page:
And that’s it! We have successfully installed LEMP stack on Ubuntu 22.04.