The LEMP stack is a popular software stack for web development and hosting. It includes four major components: Linux, Nginx, MySQL, and PHP. Each component serves a specific purpose in powering dynamic websites and web applications.
- Linux is the operating system that serves as the LEMP stack’s foundation. In this case, Ubuntu is a Linux distribution known for its stability, security, and usability.
- Nginx is a high-performance web server and reverses proxy server (pronounced “engine-x”). It is well-known for its ability to handle concurrent connections and serve static and dynamic content efficiently. In the LEMP stack, Nginx serves as the front-end web server, handling incoming web requests and forwarding them to the appropriate backend components.
- MySQL is a popular open-source relational database management system (RDBMS). It provides a stable and scalable platform for storing and managing structured data. MySQL serves as the database server in the LEMP stack, storing and retrieving data for web applications.
- PHP is a server-side scripting language that was created for web development. It allows the server to run dynamic code, generate HTML pages, and interact with the database. PHP is a scripting language that is used to create dynamic web applications when used with Nginx and MySQL.
These components work together to form a powerful and flexible stack for hosting and developing web applications. The underlying operating system is Linux, the web server is Nginx, database operations are handled by MySQL, and PHP supports dynamic web content generation.
You can create a robust and efficient environment for hosting and developing web applications by installing and configuring the LEMP stack on Ubuntu 22.04.
Step 1: Update the package index #
The first step is to update the system. It’s a good idea to update the system’s package repository and upgrade existing packages to the latest versions before installing any packages. Open a terminal window and enter the following commands:
sudo apt update -y && sudo apt upgrade -y
Due to the complexity and scope of the changes being implemented, the process of upgrading can frequently take several minutes.
Step 2: Install Nginx #
Nginx, a high-performance web server, will be used to display web pages to your site visitors. Since we are using Ubuntu, we will use apt to install Nginx our first LEMP stack service:
sudo apt install nginx -y
After installation, run the following commands to start Nginx and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
When you enable a service, systemd will start it automatically at boot time or when the system enters a specific target or runlevel. This ensures that essential services are available and operational without the need for manual intervention.
To verify if Nginx was successfully installed on your Ubuntu server, enter your server’s IP address into your web browser to access Nginx’s default landing page:
Step 3: Install MySQL on Ubuntu #
Following the successful installation of your web server, the next step is to install a database system to handle data storage and management for your website. MySQL is a well-known relational database administration system. Run the following command to install MySQL our second LEMP stack service:
sudo apt install mysql-server -y
When the installation is complete, it is recommended that you run the security script that comes with MySQL. This script will disable access to your database system and remove some insecure default settings. Run the following command to launch the interactive script:
sudo mysql_secure_installation
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
All done!
After the installation, run the following commands to start MySQL and enable it to start on boot:
sudo systemctl start mysql
sudo systemctl enable mysql
Running sudo mysql
grants you administrative access to the MySQL server, allowing you to perform administrative tasks such as creating or modifying databases, managing users and permissions, executing SQL queries, and performing other administrative actions.
Step 4: Install PHP #
PHP is a server-side scripting language that is commonly used to create dynamic web content. You’ve set up Nginx to serve your content and MySQL to store and manage your data. Now it’s time to install PHP with the following command:
sudo sudo apt install php8.1-fpm php-mysql -y
Final Thoughts #
You have successfully installed the LEMP stack (Linux, Nginx, MySQL, and PHP) on Ubuntu 22.04. This powerful combination creates a dependable and efficient environment for hosting.