LEMP is an open-source web application stack from Linux, Nginx, MariaDB/MySQL, and PHP. LEMP represents L for Linux, E for Nginx, M for MariaDB/MySQL, and P for PHP/Perl/Python. All components are used together to host a web application on the internet. LEMP is popular due to its scalability, high performance, open-source, flexibility, and security. If you are looking for an open-source stack for website deployment, then LEMP is your best choice.

This post will show you how to install a LEMP stack on Fedora 34.

Step 1 – Install Nginx

Nginx is the first component of the LEMP stack. You can install it by running the following command.

dnf install nginx -y

Once installed, start and enable the Nginx service with the following command.

systemctl start nginx
systemctl enable nginx

Next, check the active status of Nginx with the following command.

systemctl status nginx

You will get the following output.

● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
    Drop-In: /usr/lib/systemd/system/nginx.service.d
             └─php-fpm.conf
     Active: active (running) since Thu 2023-04-13 23:11:45 EDT; 32s ago
    Process: 6342 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 6343 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 6344 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 6345 (nginx)
      Tasks: 3 (limit: 4666)
     Memory: 3.3M
        CPU: 102ms
     CGroup: /system.slice/nginx.service
             ├─6345 nginx: master process /usr/sbin/nginx
             ├─6346 nginx: worker process
             └─6347 nginx: worker process

Step 2 – Install PHP and PHP-FPM

You will need to install PHP, PHP-FPM, and other PHP extensions to host PHP-based applications on your Nginx server. You can install all of them with the following command.

dnf install php php-fpm php-common php-mysqlnd php-json php-curl -y

Next, exit the PHP-FPM configuration file:

nano /etc/php-fpm.d/www.conf

Replace apache with nginx as shown below:

user = nginx 
group = nginx 

Save and close the file, then start and enable the PHP-FPM service:

systemctl start php-fpm
systemctl enable php-fpm

You can also verify the PHP version with the following command.

php -v

You should see the PHP version in the following output.

PHP 7.4.28 (cli) (built: Feb 15 2022 13:23:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies

Step 3 – Install MariaDB Database Server

You will also need to install MariaDB server as a database backend to your server. You can install it with the following command.

dnf install mariadb-server -y

Next, start and enable the MariaDB service using the following command.

systemctl start mariadb 
systemctl enable mariadb

Next, secure the MariaDB installation with the following command.

mysql_secure_installation

You will be asked to provide your current password:

Enter current password for root (enter for none): 

Just press the Enter key. You will be asked to change the root password.

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] n
 ... skipping.

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: 

Set your new password and press the Enter key. You will be asked to remove the anonymous users:

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

Type Y and press the Enter key. You will be asked to disallow root login remotely.

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

Type Y and press the Enter key. You will be asked to remove the test database.

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

Type Y and press the Enter key. You will be asked to reload the privilege tables.

 - 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

Type Y and press the Enter key to finish the process.

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

Step 4 – Host a PHP Website with Nginx

Next, create a simple info.php file inside the Nginx web root directory.

nano /var/www/html/info.php

Add the following code:

<?php phpinfo(); ?>

Save and close the file, then create an Nginx virtual server block to define the info.php file.

nano /etc/nginx/conf.d/phpinfo.conf

Add the following configurations:

server {

 listen 80;

 server_name phpinfo.example.com;
 root /var/www/html/;

  index info.php;

  location ~ \.php$ {
    fastcgi_pass  unix:/run/php-fpm/www.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
    include  fastcgi_params;
  }

}

Save and close the file, then edit the Nginx main configuration file.

nano /etc/nginx/nginx.conf

Add the following line below the line http{:

server_names_hash_bucket_size 64;

Save and close the file, then verify the Nginx configuration:

nginx -t

If everything is fine, you should see the following output.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the changes.

systemctl restart nginx

Step 5 – Access PHP Website Page

Now, open your web browser and access the PHP page using the URL http://phpinfo.example.com. You should see the PHP page on the following screen.

Verify PHP LEMP

Conclusion

In this post, you learned how to install the LEMP stack on Fedora 34. You also learned how to host a PHP-based website using the LEMP server. You can now easily host any PHP-based website with LEMP. Try to host a website with a LEMP stack on dedicated server hosting from Atlantic.Net!