Install LEMP on a VPS (Nginx, MySQL, PHP)
The LEMP stack (Linux, Nginx, MySQL, PHP) is one of the most popular and efficient server configurations for hosting dynamic web applications and high-traffic sites. While LAMP uses Apache, LEMP stands out for its use of Nginx (pronounced "engine-x"), a web server known for its superior performance, ability to handle a large number of concurrent connections, and efficiency in resource consumption. If you are looking to optimize your hosting infrastructure on a PlatiniumHost VPS, installing LEMP is an excellent starting point. This guide will walk you step-by-step through the complete installation of Nginx, MySQL (MariaDB), and PHP on your server.
This configuration is ideal for developers, businesses, and any user who needs a robust and scalable server for their web projects, from personal blogs to complex enterprise applications. Unlike solutions with control panels like cPanel, which simplify administration but add a layer of abstraction and resources, a manual LEMP installation gives you total control and direct optimization over each component.
Prerequisites
- A VPS server with Ubuntu 20.04 or higher (although the commands are similar for other Debian-based distributions).
- SSH access to the server with a user with sudo privileges or as root.
- Basic knowledge of the Linux command line.
Step 1: Update the System
Before installing any new software, it's good practice to ensure all packages on your system are up to date. This ensures you are using the latest stable versions and have the most recent security fixes.
Step 2: Install Nginx
Nginx will be our web server, responsible for serving static files and forwarding PHP requests to PHP-FPM. Its asynchronous architecture makes it exceptionally fast and efficient.
Once installed, Nginx should start automatically. You can check its status and enable it to start with the system:
sudo ufw allow 'Nginx Full' or sudo ufw allow 80/tcp and sudo ufw allow 443/tcp.To confirm that Nginx is working, open your web browser and navigate to your VPS's IP address. You should see the default Nginx welcome page.
Step 3: Install MySQL (MariaDB)
MySQL is the most widely used database in the web environment. In many Linux distributions, MariaDB, a community fork of MySQL, is used as a replacement. It is fully compatible and offers performance improvements.
After installation, it is crucial to run the security script to protect your database server.
You can check the status of the MariaDB service with:
Step 4: Install PHP and PHP-FPM
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation that offers additional features for high-traffic sites, making it perfect for working with Nginx.
This command installs PHP-FPM and some common extensions that most PHP applications require. You can add more extensions according to your project's needs.
Check the status of PHP-FPM:
Step 5: Configure Nginx for PHP-FPM
Now we need to tell Nginx how to process PHP files. We will edit the default server block or create a new one for your site. For this example, we will modify the default Nginx configuration file.
Find the server section and make the following modifications. Uncomment the lines related to PHP and ensure the configuration is similar to this:
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; must point to the correct socket for your PHP-FPM version. If you installed PHP 8.x, it would be php8.x-fpm.sock.Save and close the file (Ctrl+X, Y, Enter). Then, test the Nginx configuration and restart it:
Step 6: Create a PHP Test File
To verify that PHP-FPM is working correctly with Nginx, we will create a simple info.php file in your website's root directory.
Add the following content to the file:
Save and close the file. Now, open your web browser and navigate to http://your_domain_or_ip/info.php. You should see the PHP information page, which confirms that LEMP has been installed and configured correctly. Once you have verified this, it is advisable to delete the info.php file for security reasons.
Conclusion
Congratulations! You have successfully installed the LEMP stack (Nginx, MySQL, PHP) on your VPS. You now have a solid, high-performance foundation for hosting your web applications. This environment offers you total control and great flexibility, ideal for projects that demand maximum performance and customization.
From here, you can configure your domains, install SSL certificates (e.g., with Let's Encrypt), deploy your web applications or CMS like WordPress, and continue optimizing your server. Remember that PlatiniumHost offers you the necessary VPS resources for your projects to thrive with this powerful configuration. If you prefer a simpler solution with a graphical interface, you can always opt for a VPS with cPanel pre-installed, but for maximum performance and customization, LEMP is the expert choice.