{"id":1852,"date":"2025-07-08T09:04:00","date_gmt":"2025-07-08T06:04:00","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/step-by-step-guide-to-deploying-the-lemp-stack-for-high-performance-web-applications\/"},"modified":"2025-07-08T09:04:00","modified_gmt":"2025-07-08T06:04:00","slug":"step-by-step-guide-to-deploying-the-lemp-stack-for-high-performance-web-applications","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/step-by-step-guide-to-deploying-the-lemp-stack-for-high-performance-web-applications\/","title":{"rendered":"Step-by-Step Guide to Deploying the LEMP Stack for High-Performance Web Applications"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LEMP stack is a combination of multiple technologies that work together to create reliable and high-performance dynamic web applications. The word \u201cLEMP\u201d is an acronym for these technologies which are Linux, Nginx (pronounced Engine-X), MySQL, and PHP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The components in the LEMP stack are all open-source, and each of them plays a specific role in building the web application. The Linux operating system is the foundation where all the components are installed, Nginx is the web server responsible for handling user requests, MySQL is the relational database responsible for storing the application data, and PHP is the server-side programming language that executes application logic and generates dynamic content.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the most out of these technologies, you need an underlying infrastructure that delivers matching levels of scalability and performance. The <a href=\"https:\/\/upcloud.com\/global\/products\/cloud-servers\/\">UpCloud cloud servers<\/a> are built on enterprise-level <a href=\"https:\/\/upcloud.com\/global\/docs\/products\/cloud-servers\/system-architecture\/\">hardware and software components<\/a> that are designed for fault tolerance and speed, in addition to their <a href=\"https:\/\/upcloud.com\/global\/docs\/products\/block-storage\/tiers\/\">extreme performance block storage<\/a>, making them a perfect option for hosting the LEMP technology stack and delivering the needed application requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, we\u2019re going to cover the step-by-step process for installing and configuring the LEMP stack components on an Ubuntu cloud server on UpCloud. By the end of this guide, you\u2019ll be able to understand the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>How the LEMP stack works when handling a user request<\/li>\n\n\n\n<li>How to install MySQL and create a database for the web application<\/li>\n\n\n\n<li>How to install PHP and connect to the MySQL database<\/li>\n\n\n\n<li>How to install Nginx and configure it to serve the user requests<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To be able to follow along with this guide you need to prepare the following steps first:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Create an <\/strong><a href=\"https:\/\/signup.upcloud.com\/\"><strong>UpCloud account<\/strong><\/a><strong>:<\/strong> This gives you access to different UpCloud services using the GUI control panel, <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/upcloud-command-line-interface\/\">command-line interface<\/a>, or the <a href=\"https:\/\/upcloud.com\/global\/resources\/tools\/category\/api-clients\/\">API<\/a>.<\/li>\n\n\n\n<li><a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/deploy-server\/\"><strong>Deploy a Cloud server on UpCloud<\/strong><\/a><strong>:<\/strong> This is going to be our target server where we\u2019ll deploy our LEMP stack components, we\u2019ll use Ubuntu for this guide.<\/li>\n\n\n\n<li><strong>Familiarity with using the CLI:<\/strong> We\u2019ll be using some basic commands throughout this guide to install our tools, navigate project directories, and work with SSH.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How the LEMP Stack Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into the practical installation and configuration, let\u2019s first breakdown the role that each component plays in the LEMP stack and understand how they work together in more detail.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Linux:<\/strong> known for its stability, security, and flexibility, Linux is typically the operating system of choice for today\u2019s most web application technology stacks. It is the foundation for the LEMP stack that manages the server resources, handles communications between the different components, and supports their installation. It also provides developers with a lot of control over the configurations allowing them to fine tune and optimize their LEMP stack for maximum performance and reliability.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Nginx:<\/strong> Nginx is an open-source web server that is responsible for handling HTTP requests from the users. It can serve static content directly or serve dynamic content by communicating with <a href=\"https:\/\/www.php.net\/manual\/en\/install.fpm.php\" target=\"_blank\" rel=\"noopener\"><strong><em>PHP-FPM<\/em><\/strong><\/a> \u2013 a process manager that is responsible for spawning worker processes to execute PHP scripts \u2013 via FastCGI interface. Nginx is the differentiating component between the LEMP stack and the LAMP stack which uses Apache as the web server component. Generally, Nginx is less resource intensive than Apache and uses an event-driven, non-blocking architecture which improves scalability and responsiveness when handling higher simultaneous connections.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQL:<\/strong> The LEMP stack uses MySQL as the relational database management system for storing and managing the web application data. MySQL is fast, reliable, and can scale both vertically and horizontally, making it a crucial component in boosting the overall LEMP stack performance and stability. In a typical LEMP setup, data is organized, stored, and retrieved from the MySQL database by the backend PHP scripts using SQL language interface which offers a structured and more convenient way for managing the web application data.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PHP:<\/strong> PHP is the server side scripting language used in the LEMP stack for implementing the application logic and generating dynamic content. When a user requests a page that includes dynamic content, the corresponding PHP script is executed on the server side and returns the generated content which is then embedded into the page and returned to the user. The PHP script execution is handled using the PHP-FPM module which offers improved performance by creating long-running workers that process the scripts. The request is forwarded to the PHP-FPM through Nginx, and they communicate with each other using the FastCGI interface.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now let\u2019s explain how these components work together when handling a user request:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The user sends a request for the web page, which is sent from the browser as an HTTP request.<\/li>\n\n\n\n<li>As the web server, Nginx receives the request and determines the content that the user needs.<\/li>\n\n\n\n<li>If static content is needed in the request, Nginx can serve this directly in the response to the browser.<\/li>\n\n\n\n<li>If dynamic content is needed, Nginx will forward the request to the PHP-FPM module. Nginx and the PHP-FPM communicate using the FastCGI protocol.<\/li>\n\n\n\n<li>PHP-FPM will execute the required PHP scripts, which can access and retrieve data from the MySQL database if required.<\/li>\n\n\n\n<li>The generated dynamic content is sent back from PHP-FPM to the Nginx server, which returns it back to the user browser as the response.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Nginx<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we understand how the LEMP stack works, let\u2019s start installing and configuring the stack components on our cloud server. We\u2019ll begin by installing the Nginx web server with the following steps:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Connect to the cloud server using SSH.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">ssh -i [ssh-key] [username]@[server-IP]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the <strong><em>ssh-key<\/em><\/strong> with your SSH key file, the <strong><em>username<\/em><\/strong> with the user of the Ubuntu server, and the <strong><em>server-IP<\/em><\/strong> with the IP address of the server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. Update the package repository index.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo apt update<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Install Nginx.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo apt install nginx<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-224-1024x573.png\" alt=\"-\" class=\"wp-image-56872\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4. Verify that the Nginx service is running.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">systemctl status nginx<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-225-1024x359.png\" alt=\"-\" class=\"wp-image-56875\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">5. Nginx runs on port 80 by default, you can test accessing the default page from the browser using the server IP address.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-226-1024x472.png\" alt=\"-\" class=\"wp-image-56878\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This means that Nginx is running and serving content successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After we\u2019ve installed our Nginx web server, the next step is to install PHP for executing scripts and generating dynamic content. In the LEMP stack, we need to install two PHP modules that are necessary for the stack to work properly, which are the <strong><em>php-fpm<\/em><\/strong>, and the <strong><em>php-mysql<\/em><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><em>php-fpm<\/em><\/strong>, as mentioned before, communicates with the Nginx web server and receives the requests that require dynamic content, it spawns worker processes that executes the PHP scripts and returns back the response. The <strong><em>php-mysql<\/em><\/strong> package allows PHP to communicate with the MySQL database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Install PHP and the required modules.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo apt install php php-fpm php-mysql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Verify the PHP installation by checking the version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">php -v<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If PHP is installed successfully, the above command will print the version.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-227-1024x398.png\" alt=\"-\" class=\"wp-image-56879\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3. Check if the php-fpm service is enabled and running.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">systemctl status php8.3-fpm<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-228-1024x371.png\" alt=\"-\" class=\"wp-image-56882\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4. Test executing a PHP script. The PHP installation includes an interactive shell that you can use to run PHP code directly. Start the shell with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">php -a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see the interactive shell prompt<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-229.png\" alt=\"-\" class=\"wp-image-56884\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Type the following function to test the PHP execution:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">php &gt; phpinfo();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function should print the detailed information and configuration about the PHP installation.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-230-1024x368.png\" alt=\"-\" class=\"wp-image-56885\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This means that the PHP code execution is successful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing MySQL and Creating a Database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have Nginx and PHP installed and running, the last component we need in the LEMP stack is the MySQL database, which will be used to store and manage the application data:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Install the MySQL server package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo apt install mysql-server<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Check that MySQL service is enabled and running.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">systemctl status mysql<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-231-1024x355.png\" alt=\"-\" class=\"wp-image-56888\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3. MySQL uses port 3306 by default, we can check that it\u2019s listening correctly on this port with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">lsof -i tcp:3306<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-232-1024x298.png\" alt=\"-\" class=\"wp-image-56890\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4. Connect to the MySQL server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo mysql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see the mysql client prompt:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfHyLnyIvghQG-cAyWUiG_H9FLYbDmiYvPD9BDeIvaPR4d6s7RXGKBJEphxZ4bS1i6O8uMQ6OQ7eVLPMWLLLzm2b34VYz6d1uhkLto7_tAYFXE29wcBlgCn37jeyKMpYADczEXfzA?key=x7JtOwxwrKmB7n36BwsCuQ\" alt=\"-\"><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">5. Create a new database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">CREATE DATABASE [database-name];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the <strong><em>database-name<\/em><\/strong> with the name of the database you want to create.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-233.png\" alt=\"-\" class=\"wp-image-56896\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">6. Create a new user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <strong><em>username<\/em><\/strong> in the above command with the name of your user, and <strong><em>password<\/em><\/strong> with the password you want to set for the user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7. Grant the user full permissions on the created database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">GRANT ALL ON database_name.* TO 'username'@'localhost';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <strong><em>database_name<\/em><\/strong> with the name of the database you created previously, and <strong><em>username<\/em><\/strong> with the name of your user.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-234.png\" alt=\"-\" class=\"wp-image-56904\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">8. Test the new user permissions on the database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">mysql&gt; exit<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will exit the current MySQL connection.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">mysql -u username -p<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will connect to MySQL with the new user. Replace <strong><em>username<\/em><\/strong> with the name of your user. You\u2019ll get a prompt to enter the password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">mysql&gt; SHOW DATABASES;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will list the databases available to this user, you should be able to see the database you created.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-235-1024x670.png\" alt=\"-\" class=\"wp-image-56907\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Database Connectivity From PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After installing each component of the LEMP stack, it\u2019s time to test integrating these components together. We\u2019ll begin by testing a PHP script that connects to the MySQL database we created earlier:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Store the database connection details in environment variables. We\u2019ll be using the database connection details inside our PHP script to connect to the database, so it\u2019s more secure to read it from environment variables instead of hardcoding them into the script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">export DB_HOST=localhost\nexport DB_NAME=database-name\nexport DB_USER=username\nexport DB_PASSWORD=password<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <strong><em>database-name<\/em><\/strong>, <strong><em>username<\/em><\/strong>, and <strong><em>password<\/em><\/strong> with the values that you\u2019ve configured for your database and user.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-236.png\" alt=\"-\" class=\"wp-image-56909\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create a new file for the PHP script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">touch filename<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-237.png\" alt=\"-\" class=\"wp-image-56912\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3. Open the file using a text editor, and add the following code. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;?php\n\/\/ Read Env variables for connection details\n$server = getenv('DB_HOST');\n$username = getenv('DB_USER');\n$password = getenv('DB_PASSWORD');\n$db = getenv('DB_NAME');\n\/\/ Create connection\n$conn = new mysqli($server, $username, $password, $db);\n\/\/ Check connection status\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed. Reason: \" . $conn-&gt;connect_error);\n}\necho \"Connected!\\n\";\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Save the file, then execute it using PHP.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">php filename<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see the <strong>\u201cConnected\u201d<\/strong> message in the output if the database connection was successful.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-238.png\" alt=\"-\" class=\"wp-image-56915\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Configure Nginx with PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now our final step in integrating the LEMP stack components is to enable Nginx to work with PHP using the php-fpm. To do this, we\u2019ll configure a virtual host (also called server block in Nginx) that forwards requests for dynamic content to the php-fpm:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create a new directory under the <strong><em>\/var\/www\/<\/em><\/strong> path to be used as the document root for our site.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">cd \/var\/www\/\nsudo mkdir lemp_tutorial<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Change the ownership of the created directory to the current user with the <strong><em>$USER<\/em><\/strong> environment variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo chown -R $USER:$USER \/var\/www\/lemp_tutorial<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create a new PHP script file under the new directory and add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;?php\nphpinfo();\n?&gt;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-239.png\" alt=\"-\" class=\"wp-image-56917\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here we named the file <strong><em>info.php<\/em><\/strong>, but you can choose any name you want.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create a new Nginx configuration file under <strong><em>\/etc\/nginx\/sites-available\/<\/em><\/strong> path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">cd \/etc\/nginx\/sites-available\nsudo touch lemp_site<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5. Open the file with a text editor and add the following configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">server {\n    listen 80;\n    server_name [hostname];\n    root \/var\/www\/lemp_tutorial;\n\n    index index.html index.htm index.php;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n\n    location ~ \\.php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/var\/run\/php\/php8.3-fpm.sock;\n     }\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above configuration, we define a new server block that listens on port 80 and uses the site name specified in the <strong><em>server_name<\/em><\/strong> section; you can replace <strong><em>hostname<\/em><\/strong> with the name of the domain you want to configure for your site.<br><br>We specify the web document root from where Nginx will serve the content as <strong><em>\/var\/www\/lemp_tutorial\/<\/em><\/strong> which is the location of our previously created PHP script file.<br><br>In the <strong><em>location<\/em><\/strong> block, we tell Nginx to pass requests to the php-fpm for processing through the <strong><em>php8.3-fpm.sock<\/em><\/strong> socket.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-240-1024x509.png\" alt=\"-\" class=\"wp-image-56920\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">6. Enable your site by creating a symbolic link to the previous configuration file in the <strong><em>\/etc\/nginx\/sites-enabled\/<\/em><\/strong> path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo ln -s \/etc\/nginx\/sites-available\/lemp_site \/etc\/nginx\/sites-enabled\/<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-241-1024x288.png\" alt=\"-\" class=\"wp-image-56922\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">7. Unlink the default configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo unlink \/etc\/nginx\/sites-enabled\/default<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">8. Check the syntax of your configuration file with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo nginx -t<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see that the syntax is ok and the test is successful.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-242-1024x214.png\" alt=\"-\" class=\"wp-image-56925\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">9. Reload the Nginx service.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo systemctl reload nginx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">10. &nbsp;Add HTTPS support&nbsp;with Let\u2019s Encrypt (Certbot).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo apt install certbot python3-certbot-nginx\nsudo certbot --nginx -d&nbsp;<a href=\"https:\/\/www.google.com\/url?q=http:\/\/lemp_site.com&amp;sa=D&amp;source=docs&amp;ust=1751369402590628&amp;usg=AOvVaw3uZ1atS9L_iJC9yyO9LWnX\" target=\"_blank\" rel=\"noreferrer noopener\">lemp_site.com<\/a><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Certbot automates HTTPS setup by getting a free Let&#8217;s Encrypt SSL certificate. It briefly asks for your email (for alerts), ToS agreement, and domain confirmation. Then it handles everything: installing the certificate, configuring Nginx, and setting up auto-renewals.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">11. Test accessing the PHP content by pointing the browser to the site name set in the Nginx configuration file followed by the <strong><em>info.php<\/em><\/strong> file name. Note that the site name must be resolved to the IP address of the cloud server through DNS.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/image-243-1024x741.png\" alt=\"-\" class=\"wp-image-56926\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As we can see, the output of the info.php file code is shown in the browser which means that our application is serving dynamic content successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LEMP is a popular technology stack for building reliable and high-performance dynamic web applications. The LEMP stack consists of four components which are Linux, Nginx, MySQL, and PHP. Each of these components plays a specific role in building the web application from handling user requests to generating dynamic content and managing the application data.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, we covered how to install and configure the components of the LEMP stack on an UpCloud high performance cloud server. We started by installing the Nginx web server, then we installed PHP and its required modules, and finally we installed MySQL server and created a sample database. After that we started testing the integration between these different components by accessing the MySQL database through PHP, then configuring Nginx to serve dynamic content requests through the php-fpm module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ready to build your LEMP stack web application in the Cloud? <a href=\"https:\/\/signup.upcloud.com\/\">Try UpCloud<\/a> now and explore a set of Cloud services optimized for your web application deployment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>What is the LEMP stack used for?<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The LEMP stack is used for building high-performance web applications, leveraging Nginx for efficient request handling, MySQL for the database, and PHP for server-side scripting.<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>How do I install the LEMP stack on Ubuntu?<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Install Nginx, MySQL, and PHP step by step. Then configure the integration between these components. This guide provides detailed instructions for Ubuntu.<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Is the LEMP stack better than the LAMP stack?<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">LEMP uses Nginx, which is faster and more efficient than Apache in handling concurrent requests. This makes it ideal for high-traffic applications.<\/p>\n","protected":false},"author":85,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","community-category":[223,250],"class_list":["post-1852","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/1852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/85"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=1852"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/1852\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=1852"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=1852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}