Set up LEMP (Linux, Nginx, MySQL, PHP) in Ubuntu Linux
In this guide, we will explain how to install the Linux, Nginx, MySQL, and PHP (LEMP) stack on an Ubuntu 22.04 server. You will set up a basic web application to test the stack's functionalities.
LEMP Stack Overview
The LEMP stack is a popular open-source software stack for hosting dynamic web applications. It is an acronym that outlines the primary components that make up this powerful combination:
Linux: The operating system that serves as the foundation for the stack.
Nginx: Pronounced "Engine-X," it acts as the web server, efficiently handling requests and responses.
MySQL: The relational database management system used for storing and managing the application's data.
PHP: A widely-used scripting language that enables dynamic content by processing code on the server side.
These components provide a robust platform for developing and deploying web applications.
Prerequisite:
It works on all Linux distributions.
Operating system used: Ubuntu 22.04
Update && Upgrade the server
Install Nginx web server
Once the Nginx is installed, start the Nginx service
Enable it to start when the system rebooted
To verify the current status of the service, follow these steps:
You can also verify the installed version of Nginx with the following command:
Check nginx default port
To verify that Nginx is running on its default port (80), execute the command below:
Adjusting The Firewall
To configure ufw
to allow Nginx connections, it's important to find a balance between security and traffic requirements. If your server isn't set up with SSL, you must allow HTTP traffic. To do this, enable connections on port
To verify your Nginx installation, navigate to http://your-server-ip
your web browser. If the installation is successful, you will be greeted by the Nginx test page.
Install MySQL
Install MySQL by typing the following command:
To secure the installation, MySQL comes with a script that will ask whether you want to modify some insecure defaults
Answer Y
for yes, or anything else to continue without enabling.
To change the root password, type Y
. To keep the current password, type N
If you agree, please follow these steps:
If you’ve enabled validation, the script will also ask you to select a level of password validation.
Next, you’ll be asked to submit and confirm a root password:
Logging Into MySQL
To access your MySQL database, use the following command:
To follow a command as a user, please execute the following steps:
Replace your_username
with your actual MySQL username. Upon entering this command, you will be prompted to input your password. After providing the correct password, you'll gain access to the MySQL shell, ready for you to execute SQL commands.
Install PHP and Configure Nginx to use the PHP
Nginx is now installed to serve your pages and MySQL is installed to store and manage your data. However, you still don’t have anything that can generate dynamic content. This is where PHP comes into play.
This is done on the server block level (server blocks are similar to Apache’s virtual hosts). To do this, create a new server block configuration file using your preferred text editor within the /etc/nginx/sites-available/
directory. In this example, we will be using nano/vi
and the new server block configuration file will say, so you can replace it with your information:
sudo vi /etc/nginx/sites-available/your_domain_name
<SERVER_IP>: Serve's public IP of the server
<PHP_VERSION_INSTALLED>: Please mention the PHP version as installed on the server, this can be checked by using the command $ php --version
as used in the example below.
After adding the content, save and exit the file. If you're using nano/vi
, save by pressing CTRL + X
, then press Y
followed by ENTER
. Next, activate your new server block by creating a symbolic link from your server block configuration file in /etc/nginx/sites-available/
to the /etc/nginx/sites-enabled/
directory:
Test your new configuration file for syntax errors:
If any errors are reported, go back and recheck your file before continuing.
When you are ready, reload Nginx to make the necessary changes:
This concludes the installation and configuration of your LEMP stack. However, it’s prudent to confirm that all of the components can communicate with one another.
Creating PHP file to test configuration
To do this, use your preferred text editor to create a test PHP file called info.php
in your document root:
When you are finished, save and close the file.
To view this page, navigate to your server's domain name or public IP address in your web browser, and append /info.php
to the URL.
http://your_Server or IP_address/info.php
Your browser will load a web page like the following that has been generated by PHP with information about your server:
Last updated