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

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@vps:~# apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  irqbalance libpython3.11-minimal libpython3.11-stdlib python3.11 python3.11-minimal
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  nginx-common
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 552 kB of archives.
After this operation, 1,596 kB of additional disk space will be used.
N: Ignoring file 'ubuntu.sources.curtin.old' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu noble/main amd64 nginx-common all 1.24.0-2ubuntu4 [31.2 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu noble/main amd64 nginx amd64 1.24.0-2ubuntu4 [521 kB]
Fetched 552 kB in 1s (877 kB/s)

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@vps:~# systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable nginx
root@vps:~# 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 Sat 2024-03-16 19:50:21 UTC; 26min ago
       Docs: man:nginx(8)
   Main PID: 17432 (nginx)
      Tasks: 3 (limit: 2188)
     Memory: 2.3M (peak: 2.5M)
        CPU: 24ms
     CGroup: /system.slice/nginx.service
             ├─17432 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─17433 "nginx: worker process"
             └─17434 "nginx: worker process"

Check the Nginx version,

nginx -v

Output:

root@vps:~# nginx -v
nginx version: nginx/1.24.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

image

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

Output:

root@vps:~# apt install mariadb-server mariadb-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  irqbalance libpython3.11-minimal libpython3.11-stdlib python3.11 python3.11-minimal
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libclone-perl libconfig-inifiles-perl libdaxctl1 libdbd-mysql-perl
  libdbi-perl libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0ldbl libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libmariadb3
  libmysqlclient21 libndctl6 libpmem1 libsnappy1v5 libtimedate-perl liburi-perl liburing2 mariadb-client-core
  mariadb-common mariadb-plugin-provider-bzip2 mariadb-plugin-provider-lz4 mariadb-plugin-provider-lzma
  mariadb-plugin-provider-lzo mariadb-plugin-provider-snappy mariadb-server-core mysql-common pv socat
Suggested packages:
  libmldbm-perl libnet-daemon-perl libsql-statement-perl libdata-dump-perl libipc-sharedcache-perl
  libio-compress-brotli-perl libbusiness-isbn-perl libregexp-ipv6-perl libwww-perl mailx mariadb-test doc-base
The following NEW packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libclone-perl libconfig-inifiles-perl libdaxctl1 libdbd-mysql-perl
  libdbi-perl libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0ldbl libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libmariadb3
  libmysqlclient21 libndctl6 libpmem1 libsnappy1v5 libtimedate-perl liburi-perl liburing2 mariadb-client
  mariadb-client-core mariadb-common mariadb-plugin-provider-bzip2 mariadb-plugin-provider-lz4
  mariadb-plugin-provider-lzma mariadb-plugin-provider-lzo mariadb-plugin-provider-snappy mariadb-server
  mariadb-server-core mysql-common pv socat
0 upgraded, 40 newly installed, 0 to remove and 0 not upgraded.
Need to get 19.0 MB of archives.
After this operation, 198 MB of additional disk space will be used.
N: Ignoring file 'ubuntu.sources.curtin.old' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension
Do you want to continue? [Y/n] y

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@vps:~# systemctl status mariadb
● mariadb.service - MariaDB 10.11.6 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: enabled)
     Active: active (running) since Sat 2024-03-16 20:26:50 UTC; 3min 33s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 19327 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 9 (limit: 2188)
     Memory: 78.6M (peak: 81.7M)
        CPU: 647ms
     CGroup: /system.slice/mariadb.service
             └─19327 /usr/sbin/mariadbd

Next, MariaDB database security.

mysql_secure_installation

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

Output:

root@vps:~# mysql_secure_installation

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.04.

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

systemctl status php8.3-fpm

Output:

root@vps:~# 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 Sat 2024-03-16 20:44:06 UTC; 11min ago
       Docs: man:php-fpm8.3(8)
   Main PID: 34159 (php-fpm8.3)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2188)
     Memory: 10.0M (peak: 10.9M)
        CPU: 154ms
     CGroup: /system.slice/php8.3-fpm.service
             ├─34159 "php-fpm: master process (/etc/php/8.3/fpm/php-fpm.conf)"
             ├─34160 "php-fpm: pool www"
             └─34161 "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.

image

This concludes the installation of LEMP Stack on Ubuntu 24.04 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