How to Install LEMP Stack (Nginx, MariaDB, PHP8.3) on Ubuntu 24.10

LEMP Stack is a combination of free, open source software. The acronym LEMP refers to the first letters of Linux (Operating system), Nginx Server, MySQL (database software), and PHP, PERL or Python, principal components to build a viable general purpose web server.

Updating the system

We first update the system to make sure that all our installed packages are up to date. Your Ubuntu system can be updated easily with the following command.

apt update
apt upgrade

Install Nginx

Start by installation of the Nginx web server. to complete the installation, use the below commands.

apt install nginx

Output:

root@server:~# apt install nginx -y
Installing:                     
  nginx

Installing dependencies:
  nginx-common

Suggested packages:
  fcgiwrap  nginx-doc  ssl-cert

Summary:
  Upgrading: 0, Installing: 2, Removing: 0, Not Upgrading: 0
  Download size: 631 kB
  Space needed: 1,811 kB / 44.9 GB available

Once the installation is complete, enable Nginx (to start automatically upon system boot), start the webserver, and verify the status using the commands below.

systemctl start nginx

systemctl enable nginx

systemctl status nginx

Output:

root@server:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-10-29 13:16:12 UTC; 47s ago
 Invocation: d69869c8c17049fc8dbd2d89ed6430fe
       Docs: man:nginx(8)
   Main PID: 1503 (nginx)
      Tasks: 3 (limit: 3995)
     Memory: 2.7M (peak: 2.9M)
        CPU: 31ms
     CGroup: /system.slice/nginx.service
             ├─1503 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─1504 "nginx: worker process"
             └─1505 "nginx: worker process"

Check the Nginx version,

nginx -v

Output:

root@server:~# nginx -v
nginx version: nginx/1.26.0 (Ubuntu)

We need to make the user Nginx the owner of the web directory. By default, it’s owned by the root user.

chown www-data:www-data /usr/share/nginx/html -R

Verify that the webserver is running and accessible by accessing your server’s IP address. From your browser,

http://IP_address

Note: Replace the4 IP_address with your actual IP Addres

images

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

apt install mariadb-server mariadb-client mariadb-client-compat

Output:

root@server:~# apt install mariadb-server mariadb-client mariadb-client-compat
Installing:                     
  mariadb-client  mariadb-client-compat  mariadb-server

Installing dependencies:
  galera-4                 libencode-locale-perl  libhttp-date-perl       liburi-perl                    mariadb-plugin-provider-lzo
  libcgi-fast-perl         libfcgi-bin            libhttp-message-perl    liburing2                      mariadb-plugin-provider-snappy
  libcgi-pm-perl           libfcgi-perl           libio-html-perl         mariadb-client-core            mariadb-server-core
  libclone-perl            libfcgi0t64            liblwp-mediatypes-perl  mariadb-common                 mysql-common
  libconfig-inifiles-perl  libhtml-parser-perl    libmariadb3             mariadb-plugin-provider-bzip2  pv
  libdbd-mysql-perl        libhtml-tagset-perl    libmysqlclient21        mariadb-plugin-provider-lz4    socat
  libdbi-perl              libhtml-template-perl  libtimedate-perl        mariadb-plugin-provider-lzma

Suggested packages:
  libmldbm-perl       libsql-statement-perl  libipc-sharedcache-perl     libbusiness-isbn-perl  libwww-perl  mariadb-test
  libnet-daemon-perl  libdata-dump-perl      libio-compress-brotli-perl  libregexp-ipv6-perl    mailx        doc-base

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the MariaDB, and verify the status using the commands below.

systemctl start mariadb

systemctl enable mariadb

systemctl status mariadb

Output:

root@server:~# systemctl status mariadb
● mariadb.service - MariaDB 11.4.3 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-10-29 13:22:10 UTC; 1min 5s ago
 Invocation: 54430cd0811d4f46b407a9a4f0e45343
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 1664 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 10 (limit: 26370)
     Memory: 84.5M (peak: 88.9M)
        CPU: 2.821s
     CGroup: /system.slice/mariadb.service
             └─1664 /usr/sbin/mariadbd

Next, MariaDB database security.

mysql_secure_installation

NOTE: In this step, you will be prompted with several questions.

Output:

root@server:~# mysql_secure_installation
/usr/bin/mysql_secure_installation: Deprecated program name. It will be removed in a future release, use 'mariadb-secure-installation' instead

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Once secured, you can login to the MySQL using below command,

mysql -u root -p

To exit from MariaDB.

exit

Check MySQL Version,

mysql --version

Output:

root@vps:~# mysql --version
mysql  Ver 15.1 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

Install PHP

PHP 8.3 is the default version of PHP that would be installed on Ubuntu 24.10.

Install PHP and Required Extensions using below command,

apt install php php-fpm php-mysql php-common php-cli php-common php-json php-opcache php-readline php-mbstring php-xml php-gd php-curl

Start and enable php8.3-fpm

systemctl start php8.3-fpm

systemctl enable php8.3-fpm

Check status of php8.3-fpm

root@server:~# systemctl status php8.3-fpm
● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-10-29 13:44:30 UTC; 25s ago
 Invocation: 5b651cc7ce714e08b5c467755efdff57
       Docs: man:php-fpm8.3(8)
   Main PID: 17273 (php-fpm8.3)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0.00req/sec"
      Tasks: 3 (limit: 3995)
     Memory: 11.3M (peak: 12.9M)
        CPU: 89ms
     CGroup: /system.slice/php8.3-fpm.service
             ├─17273 "php-fpm: master process (/etc/php/8.3/fpm/php-fpm.conf)"
             ├─17274 "php-fpm: pool www"
             └─17275 "php-fpm: pool www"

Setting Up Server Blocks

You need to remove the file default, located in /etc/nginx/sites-enabled.

rm /etc/nginx/sites-enabled/default

Create new server block file under /etc/nginx/conf.d/ directory.

nano /etc/nginx/conf.d/default.conf

Add the following text to the file.

server {
    listen 80;
    listen [::]:80;
    server_name _;
    root /var/www/html/;
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
      try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
      fastcgi_pass unix:/run/php/php8.3-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      include snippets/fastcgi-php.conf;
    }

    # A long browser cache lifetime can speed up repeat visits to your page
    location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
         access_log        off;
         log_not_found     off;
         expires           360d;
   }

   # disable access to hidden files
   location ~ /\.ht {
        access_log off;
        log_not_found off;
        deny all;
   }
   }

Next, test to make sure that there are no syntax errors in any of your Nginx files.

nginx -t

If there aren’t any problems, restart Nginx to enable your changes.

systemctl reload nginx

Test PHP-FPM with the Nginx Web server, we need to create an info.php file in the webroot directory.

nano /var/www/html/info.php

Add the following PHP code to the file.

<?php phpinfo(); ?>

Now access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to the below one.

images

This concludes the installation of LEMP Stack on Ubuntu 24.10 server.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS