How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

The LEMP stack is a powerful combination of software that allows you to run dynamic websites and web applications. It consists of Linux as the operating system, Nginx as the web server, MySQL as the database management system, and PHP as the programming language. In this step-by-step guide, I will walk you through the process of installing and configuring the LEMP stack on Ubuntu 22.04.

Understanding the components of the LEMP stack

Before we dive into the installation process, it’s essential to understand the role of each component in the LEMP stack.

Linux is an open-source operating system that serves as the foundation for the LEMP stack. It provides the necessary tools and resources to run your web applications efficiently.

Nginx, pronounced “engine x,” is a high-performance web server that can handle a large number of concurrent connections. It excels at serving static content and handling requests quickly and efficiently.

MySQL is a popular relational database management system that allows you to store, manage, and retrieve data for your web applications. It provides a robust and scalable solution for storing and organizing your data.

PHP is a server-side scripting language that enables you to create dynamic web pages and interact with databases. It is widely used for web development and can be seamlessly integrated with Nginx and MySQL.

Preparing your Ubuntu 22.04 server

Before you begin the installation process, make sure you have a clean installation of Ubuntu 22.04 on your server. Ensure that your server meets the minimum system requirements and that you have administrative access.

Installing Linux on Ubuntu 22.04

To install Linux on your Ubuntu 22.04 server, follow these steps:

  1. Update the package list: sudo apt update
  2. Upgrade installed packages: sudo apt upgrade
  3. Install the Linux kernel: sudo apt install linux-generic
  4. Reboot your server: sudo reboot

Installing Nginx on Ubuntu 22.04

Nginx is not available in the default Ubuntu repositories. To install Nginx on Ubuntu 22.04, you need to add the Nginx repository and then install the package. Follow these steps:

  1. Add the Nginx repository: sudo add-apt-repository ppa:nginx/stable
  2. Update the package list: sudo apt update
  3. Install Nginx: sudo apt install nginx
  4. Start and enable Nginx: sudo systemctl start nginx andsudo systemctl enable nginx

Configuring Nginx for optimal performance

Once Nginx is installed, you can optimize its configuration for better performance. Here are some recommended configurations:

  1. Adjust the worker processes: Open the Nginx configuration file located at/etc/nginx/nginx.conf and modify theworker_processes directive to match the number of CPU cores on your server.
  2. Enable gzip compression: Uncomment thegzip directive in the configuration file to enable gzip compression and reduce the size of transmitted data.
  3. Configure caching: Implement caching directives to reduce the load on your server and improve response times for repeated requests.

Installing MySQL on Ubuntu 22.04

To install MySQL on Ubuntu 22.04, follow these steps:

  1. Update the package list: sudo apt update
  2. Install MySQL: sudo apt install mysql-server
  3. Secure the MySQL installation: sudo mysql_secure_installation

Securing MySQL installation

Securing your MySQL installation is crucial to protect your data from unauthorized access. Here are some security measures you can take:

  1. Set a strong root password: During the installation process, you will be prompted to set a root password. Make sure to choose a strong password that includes a combination of uppercase and lowercase letters, numbers, and special characters.
  2. Remove anonymous users: Run the following command to remove any anonymous users:sudo mysql -u root -p -e "DELETE FROM mysql.user WHERE User='';"
  3. Disallow remote root login: Edit the MySQL configuration file located at/etc/mysql/mysql.conf.d/mysqld.cnf and comment on thebind-address directive to restrict root login to the localhost.

Installing PHP on Ubuntu 22.04

To install PHP on Ubuntu 22.04, follow these steps:

  1. Update the package list:sudo apt update
  2. Install PHP and required extensions:sudo apt install php-fpm php-mysql
  3. Configure PHP-FPM: Open the PHP-FPM configuration file located at/etc/php/7.4/fpm/php.ini and make any necessary changes, such as adjusting thememory_limit orupload_max_filesize directives.

Configuring PHP for LEMP stack

Once PHP is installed, you need to configure Nginx to process PHP files. Here’s how you can do it:

  1. Open the Nginx default configuration file:sudo nano /etc/nginx/sites-available/default
  2. Locate thelocation ~ .php$ block and uncomment it.
  3. Modify thefastcgi_pass directive to point to the PHP-FPM socket:fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  4. Save the file and exit the editor.
  5. Restart Nginx:sudo systemctl restart nginx

Testing your LEMP stack installation

To test if your LEMP stack is functioning correctly, create a simple PHP file and access it through your web browser. Follow these steps:

  1. Create a new PHP file:sudo nano /var/www/html/info.php
  2. Add the following content to the file:<?php phpinfo(); ?>
  3. Save the file and exit the editor.
  4. Open your web browser and navigate tohttp://your_server_ip/info.php.
  5. If you see the PHP information page, congratulations! Your LEMP stack is up and running.

Troubleshooting common issues

If you encounter any issues during the installation or configuration process, here are some common problems and their solutions:

  1. Nginx failing to start: Check the Nginx error logs located /var/log/nginx/error.log for any error messages that could indicate the cause of the problem.
  2. PHP not processing files: Ensure that thefastcgi_pass directive in your Nginx configuration file points to the correct PHP-FPM socket.
  3. MySQL connection issues: Double-check your MySQL credentials and make sure that the MySQL server is running.


Congratulations! You have successfully installed the LEMP stack on your Ubuntu 22.04 server. The LEMP stack provides a robust and efficient platform for hosting dynamic websites and web applications. By following this step-by-step guide, you have learned how to install and configure each component of the LEMP stack. Now you can start building and deploying your web projects with confidence.

Remember, this guide is just the beginning, and there is much more you can explore and learn about the LEMP stack. Continue to experiment and expand your knowledge to take full advantage of this powerful web development stack.

CTA: If you found this article helpful, please share it with others who may benefit from it.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *