Author – Vivek Chaugule, Cloud Engineer
LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.
LEMP is more suitable for Low End Boxes and cheap VPS machines, because it consumes far less resources than the LAMP stack (even under heavy load) and is 2.5 times faster according to benchmarks. We are going to install MariaDB instead of MySQL as it is much faster, but still fully compatible with the original MySQL.
An open source Unix-like operating system provides the base for the stack components. Many distributions include package management systems that make installing the desired applications easy. However, these repository versions will generally not be the most current. Two of the most commonly used Linux distributions in LEMP stacks are Debian and Ubuntu.
Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols. It also functions as a load balancer, HTTP cache, and web server (origin server). It has a strong focus on high concurrency, high performance and low memory usage. The HTML5 Boilerplate project has sample server configuration files to improve performance and security.
PHP is a server-side scripting language designed for web development. The scripting role can also be filled with Python or Perl. Servers such as Gunicorn or uWSGI can be used in conjunction with Nginx to serve these applications.
To follow this guide, you’ll need access to a CentOS 8 server as a sudo user.
1. Install Nginx
Install the nginx package with:
#sudo dnf install nginx
After the installation is finished, run the following command to enable and start the server:
#sudo systemctl start nginx
Now you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser. You’ll see a page like this:
2. Install MySQL Server
We’ll now install MariaDB, a community-developed fork of the original MySQL server by Oracle. To install this software, run:
#sudo dnf install mariadb-server
When the installation is finished, enable and start the MariaDB server with:
#sudo systemctl start mariadb
Apply security on newly installed MySQL server. This will also prompt you to change the temporary password with a new password.
# sudo mysql_secure_installation
Change the password for root? – y
Remove anonymous users? – y
Disallow root login remotely? – y
Remove test database and access to it? – y
Reload privilege tables now? – y
MySQL has been installed. Next we will install php
3. Install PHP
To install the php-fpm and php-mysql packages, run:
#sudo dnf install php-fpm php-mysqlnd
Now open the /etc/php-fpm.d/www.conf configuration file using nano or your editor of choice:
#sudo nano /etc/php-fpm.d/www.conf
Look for the user and group directives. If you are using nano, you can hit CTRL+W to search for these terms inside the open file. Make sure to change both values from apache to nginx:
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
Save and close the file when you’re done editing.
To enable and start the php-fpm service, run:
#sudo systemctl start php-fpm
Finally, restart the Nginx web server so that it loads the configuration files created by the php-fpm installation:
#sudo systemctl restart nginx
4. Test PHP with Nginx
Create a new PHP file called info.php at the /usr/share/nginx/html directory:
The following PHP code will display information about the current PHP environment running on the server:
Copy this content to your info.php file, and don’t forget to save it when you’re done.
Now we can test whether our web server can correctly display content generated by a PHP script. Go to your browser and access your server hostname or IP address, followed by /info.php:
You’ll see a page similar to this: